Replace assert with assume for release build optimization (#1390)

This commit is contained in:
Sylvie
2024-04-02 11:09:31 -04:00
committed by GitHub
parent 1d39e5ed56
commit a234da42a6
26 changed files with 158 additions and 147 deletions

View File

@@ -3,7 +3,6 @@
#ifndef RGBDS_FILE_HPP
#define RGBDS_FILE_HPP
#include <assert.h>
#include <fcntl.h>
#include <fstream>
#include <ios>
@@ -13,6 +12,7 @@
#include <string>
#include <variant>
#include "helpers.hpp" // assume
#include "platform.hpp"
#include "gfx/main.hpp"
@@ -33,7 +33,7 @@ public:
if (path != "-") {
return _file.emplace<std::filebuf>().open(path, mode) ? this : nullptr;
} else if (mode & std::ios_base::in) {
assert(!(mode & std::ios_base::out));
assume(!(mode & std::ios_base::out));
_file.emplace<std::streambuf *>(std::cin.rdbuf());
if (setmode(STDIN_FILENO, (mode & std::ios_base::binary) ? O_BINARY : O_TEXT) == -1) {
fatal(
@@ -43,7 +43,7 @@ public:
);
}
} else {
assert(mode & std::ios_base::out);
assume(mode & std::ios_base::out);
_file.emplace<std::streambuf *>(std::cout.rdbuf());
}
return this;

View File

@@ -3,12 +3,12 @@
#ifndef HELPERS_H
#define HELPERS_H
// Ideally, we'd use `__has_attribute` and `__has_builtin`, but these were only introduced in GCC 9
// Ideally we'd use `std::unreachable`, but it has insufficient compiler support
#ifdef __GNUC__ // GCC or compatible
// In release builds, define "unreachable" as such, but trap in debug builds
#ifdef NDEBUG
#define unreachable_ __builtin_unreachable
#else
// In release builds, define "unreachable" as such, but trap in debug builds
#define unreachable_ __builtin_trap
#endif
#else
@@ -18,32 +18,52 @@
}
#endif
// Use builtins whenever possible, and shim them otherwise
// Ideally we'd use `[[assume()]]`, but it has insufficient compiler support
#ifdef NDEBUG
#ifdef _MSC_VER
#define assume(x) __assume(x)
#else
// `[[gnu::assume()]]` for GCC or compatible also has insufficient support (GCC 13+ only)
#define assume(x) \
do { \
if (!(x)) \
unreachable_(); \
} while (0)
#endif
#else
// In release builds, define "assume" as such, but `assert` in debug builds
#include <assert.h>
#define assume assert
#endif
// Ideally we'd use `std::bit_width`, but it has insufficient compiler support
#ifdef __GNUC__ // GCC or compatible
#define ctz __builtin_ctz
#define clz __builtin_clz
#elif defined(_MSC_VER)
#include <assert.h>
#include <intrin.h>
#pragma intrinsic(_BitScanReverse, _BitScanForward)
static inline int ctz(unsigned int x) {
unsigned long cnt;
assert(x != 0);
assume(x != 0);
_BitScanForward(&cnt, x);
return cnt;
}
static inline int clz(unsigned int x) {
unsigned long cnt;
assert(x != 0);
assume(x != 0);
_BitScanReverse(&cnt, x);
return 31 - cnt;
}
#else
#include <limits.h>
static inline int ctz(unsigned int x) {
int cnt = 0;

View File

@@ -3,10 +3,11 @@
#ifndef RGBDS_LINKDEFS_H
#define RGBDS_LINKDEFS_H
#include <assert.h>
#include <stdint.h>
#include <string>
#include "helpers.hpp" // assume
#define RGBDS_OBJECT_VERSION_STRING "RGBA"
#define RGBDS_OBJECT_REV 10U
@@ -93,7 +94,7 @@ extern struct SectionTypeInfo {
* @return `true` if the section's definition includes data
*/
static inline bool sect_HasData(SectionType type) {
assert(type != SECTTYPE_INVALID);
assume(type != SECTTYPE_INVALID);
return type == SECTTYPE_ROM0 || type == SECTTYPE_ROMX;
}

View File

@@ -3,7 +3,6 @@
#include "asm/fstack.hpp"
#include <sys/stat.h>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <memory>
@@ -50,28 +49,28 @@ static std::vector<std::string> includePaths = {""};
static std::string preIncludeName;
std::vector<uint32_t> &FileStackNode::iters() {
assert(std::holds_alternative<std::vector<uint32_t>>(data));
assume(std::holds_alternative<std::vector<uint32_t>>(data));
return std::get<std::vector<uint32_t>>(data);
}
std::vector<uint32_t> const &FileStackNode::iters() const {
assert(std::holds_alternative<std::vector<uint32_t>>(data));
assume(std::holds_alternative<std::vector<uint32_t>>(data));
return std::get<std::vector<uint32_t>>(data);
}
std::string &FileStackNode::name() {
assert(std::holds_alternative<std::string>(data));
assume(std::holds_alternative<std::string>(data));
return std::get<std::string>(data);
}
std::string const &FileStackNode::name() const {
assert(std::holds_alternative<std::string>(data));
assume(std::holds_alternative<std::string>(data));
return std::get<std::string>(data);
}
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
assert(parent); // REPT nodes use their parent's name
assume(parent); // REPT nodes use their parent's name
std::string const &lastName = parent->dump(lineNo);
fputs(" -> ", stderr);
fputs(lastName.c_str(), stderr);
@@ -270,7 +269,7 @@ static void newMacroContext(Symbol const &macro, std::shared_ptr<MacroArgs> macr
fileInfoName.append(macro.name);
auto fileInfo = std::make_shared<FileStackNode>(NODE_MACRO, fileInfoName);
assert(!contextStack.empty()); // The top level context cannot be a MACRO
assume(!contextStack.empty()); // The top level context cannot be a MACRO
fileInfo->parent = oldContext.fileInfo;
fileInfo->lineNo = lexer_GetLineNo();
@@ -295,7 +294,7 @@ static Context &newReptContext(int32_t reptLineNo, ContentSpan const &span, uint
}
auto fileInfo = std::make_shared<FileStackNode>(NODE_REPT, fileInfoIters);
assert(!contextStack.empty()); // The top level context cannot be a REPT
assume(!contextStack.empty()); // The top level context cannot be a REPT
fileInfo->parent = oldContext.fileInfo;
fileInfo->lineNo = reptLineNo;

View File

@@ -5,7 +5,6 @@
#include <sys/types.h>
#include <algorithm>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
@@ -20,7 +19,7 @@
#include <unistd.h>
#endif
#include "helpers.hpp" // QUOTEDSTRLEN
#include "helpers.hpp" // assume, QUOTEDSTRLEN
#include "util.hpp"
#include "asm/fixpoint.hpp"
@@ -477,14 +476,14 @@ LexerState::~LexerState() {
// scheduled at EOF; `lexerStateEOL` thus becomes a (weak) ref to that lexer state...
// It has been possible, due to a bug, that the corresponding fstack context gets popped
// before EOL, deleting the associated state... but it would still be switched to at EOL.
// This assertion checks that this doesn't happen again.
// This assumption checks that this doesn't happen again.
// It could be argued that deleting a state that's scheduled for EOF could simply clear
// `lexerStateEOL`, but there's currently no situation in which this should happen.
assert(this != lexerStateEOL);
assume(this != lexerStateEOL);
}
bool Expansion::advance() {
assert(offset <= size());
assume(offset <= size());
offset++;
return offset > size();
}
@@ -494,11 +493,11 @@ BufferedContent::~BufferedContent() {
}
void BufferedContent::advance() {
assert(offset < LEXER_BUF_SIZE);
assume(offset < LEXER_BUF_SIZE);
offset++;
if (offset == LEXER_BUF_SIZE)
offset = 0; // Wrap around if necessary
assert(size > 0);
assume(size > 0);
size--;
}
@@ -528,7 +527,7 @@ void BufferedContent::refill() {
size_t BufferedContent::readMore(size_t startIndex, size_t nbChars) {
// This buffer overflow made me lose WEEKS of my life. Never again.
assert(startIndex + nbChars <= LEXER_BUF_SIZE);
assume(startIndex + nbChars <= LEXER_BUF_SIZE);
ssize_t nbReadChars = read(fd, &buf[startIndex], nbChars);
if (nbReadChars == -1)
@@ -671,7 +670,7 @@ static std::shared_ptr<std::string> readMacroArg(char name) {
error("Invalid macro argument '\\0'\n");
return nullptr;
} else {
assert(name > '0' && name <= '9');
assume(name > '0' && name <= '9');
MacroArgs *macroArgs = fstk_GetCurrentMacroArgs();
if (!macroArgs) {
@@ -698,11 +697,11 @@ int LexerState::peekChar() {
if (view->offset < view->span.size)
return (uint8_t)view->span.ptr[view->offset];
} else {
assert(std::holds_alternative<BufferedContent>(content));
assume(std::holds_alternative<BufferedContent>(content));
auto &cbuf = std::get<BufferedContent>(content);
if (cbuf.size == 0)
cbuf.refill();
assert(cbuf.offset < LEXER_BUF_SIZE);
assume(cbuf.offset < LEXER_BUF_SIZE);
if (cbuf.size > 0)
return (uint8_t)cbuf.buf[cbuf.offset];
}
@@ -718,7 +717,7 @@ int LexerState::peekCharAhead() {
for (Expansion &exp : expansions) {
// An expansion that has reached its end will have `exp.offset` == `exp.size()`,
// and `.peekCharAhead()` will continue with its parent
assert(exp.offset <= exp.size());
assume(exp.offset <= exp.size());
if (exp.offset + distance < exp.size())
return (uint8_t)(*exp.contents)[exp.offset + distance];
distance -= exp.size() - exp.offset;
@@ -728,9 +727,9 @@ int LexerState::peekCharAhead() {
if (view->offset + distance < view->span.size)
return (uint8_t)view->span.ptr[view->offset + distance];
} else {
assert(std::holds_alternative<BufferedContent>(content));
assume(std::holds_alternative<BufferedContent>(content));
auto &cbuf = std::get<BufferedContent>(content);
assert(distance < LEXER_BUF_SIZE);
assume(distance < LEXER_BUF_SIZE);
if (cbuf.size <= distance)
cbuf.refill();
if (cbuf.size > distance)
@@ -816,7 +815,7 @@ restart:
if (auto *view = std::get_if<ViewedContent>(&lexerState->content); view) {
view->offset++;
} else {
assert(std::holds_alternative<BufferedContent>(lexerState->content));
assume(std::holds_alternative<BufferedContent>(lexerState->content));
auto &cbuf = std::get<BufferedContent>(lexerState->content);
cbuf.advance();
}
@@ -1785,7 +1784,7 @@ static Token yylex_NORMAL() {
return token;
// `token` is either an `ID` or a `LOCAL_ID`, and both have a `std::string` value.
assert(std::holds_alternative<std::string>(token.value));
assume(std::holds_alternative<std::string>(token.value));
// Local symbols cannot be string expansions
if (token.type == T_(ID) && lexerState->expandStrings) {
@@ -1795,7 +1794,7 @@ static Token yylex_NORMAL() {
if (sym && sym->type == SYM_EQUS) {
std::shared_ptr<std::string> str = sym->getEqus();
assert(str);
assume(str);
beginExpansion(str, sym->name);
continue; // Restart, reading from the new buffer
}
@@ -2172,7 +2171,7 @@ yy::parser::symbol_type yylex() {
} else if (auto *strValue = std::get_if<std::string>(&token.value); strValue) {
return yy::parser::symbol_type(token.type, *strValue);
} else {
assert(std::holds_alternative<std::monostate>(token.value));
assume(std::holds_alternative<std::monostate>(token.value));
return yy::parser::symbol_type(token.type);
}
}
@@ -2180,10 +2179,10 @@ yy::parser::symbol_type yylex() {
static Capture startCapture() {
// Due to parser internals, it reads the EOL after the expression before calling this.
// Thus, we don't need to keep one in the buffer afterwards.
// The following assertion checks that.
assert(lexerState->atLineStart);
// The following assumption checks that.
assume(lexerState->atLineStart);
assert(!lexerState->capturing && lexerState->captureBuf == nullptr);
assume(!lexerState->capturing && lexerState->captureBuf == nullptr);
lexerState->capturing = true;
lexerState->captureSize = 0;
@@ -2194,7 +2193,7 @@ static Capture startCapture() {
.lineNo = lineNo, .span = {.ptr = view->makeSharedContentPtr(), .size = 0}
};
} else {
assert(lexerState->captureBuf == nullptr);
assume(lexerState->captureBuf == nullptr);
lexerState->captureBuf = std::make_shared<std::vector<char>>();
// `.span.ptr == nullptr`; indicates to retrieve the capture buffer when done capturing
return {

View File

@@ -2,7 +2,6 @@
#include "asm/output.hpp"
#include <assert.h>
#include <deque>
#include <inttypes.h>
#include <stdio.h>
@@ -12,7 +11,7 @@
#include <vector>
#include "error.hpp"
#include "helpers.hpp" // Defer
#include "helpers.hpp" // assume, Defer
#include "asm/fstack.hpp"
#include "asm/lexer.hpp"
@@ -75,7 +74,7 @@ static uint32_t getSectIDIfAny(Section *sect) {
// Write a patch to a file
static void writepatch(Patch const &patch, FILE *file) {
assert(patch.src->ID != (uint32_t)-1);
assume(patch.src->ID != (uint32_t)-1);
putlong(patch.src->ID, file);
putlong(patch.lineNo, file);
putlong(patch.offset, file);
@@ -117,7 +116,7 @@ static void writesymbol(Symbol const &sym, FILE *file) {
if (!sym.isDefined()) {
putc(SYMTYPE_IMPORT, file);
} else {
assert(sym.src->ID != (uint32_t)-1);
assume(sym.src->ID != (uint32_t)-1);
putc(sym.isExported ? SYMTYPE_EXPORT : SYMTYPE_LOCAL, file);
putlong(sym.src->ID, file);

View File

@@ -2542,7 +2542,7 @@ static std::string strfmt(
} else if (auto *n = std::get_if<uint32_t>(&args[argIndex]); n) {
fmt.appendNumber(str, *n);
} else {
assert(std::holds_alternative<std::string>(args[argIndex]));
assume(std::holds_alternative<std::string>(args[argIndex]));
auto &s = std::get<std::string>(args[argIndex]);
fmt.appendString(str, s);
}

View File

@@ -2,7 +2,6 @@
#include "asm/rpn.hpp"
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
@@ -10,6 +9,7 @@
#include <string.h>
#include <string_view>
#include "helpers.hpp" // assume
#include "opmath.hpp"
#include "asm/output.hpp"
@@ -20,7 +20,7 @@
using namespace std::literals;
int32_t Expression::value() const {
assert(std::holds_alternative<int32_t>(data));
assume(std::holds_alternative<int32_t>(data));
return std::get<int32_t>(data);
}
@@ -116,7 +116,7 @@ void Expression::makeBankSymbol(std::string const &symName) {
data = 1;
} else {
sym = sym_Ref(symName);
assert(sym); // If the symbol didn't exist, it should have been created
assume(sym); // If the symbol didn't exist, it should have been created
if (sym->getSection() && sym->getSection()->bank != (uint32_t)-1) {
// Symbol's section is known and bank is fixed
@@ -217,7 +217,7 @@ static int32_t tryConstMask(Expression const &lhs, Expression const &rhs) {
Symbol const &sym = lhsIsSymbol ? *lhsSymbol : *rhsSymbol;
Expression const &expr = lhsIsSymbol ? rhs : lhs; // Opposite side of `sym`
assert(sym.isNumeric());
assume(sym.isNumeric());
if (!expr.isKnown())
return -1;
@@ -231,7 +231,7 @@ static int32_t tryConstMask(Expression const &lhs, Expression const &rhs) {
// `sym.getValue()` attempts to add the section's address, but that's "-1"
// because the section is floating (otherwise we wouldn't be here)
assert(sect.org == (uint32_t)-1);
assume(sect.org == (uint32_t)-1);
int32_t symbolOfs = sym.getValue() + 1;
return (symbolOfs + sect.alignOfs) & ~unknownBits;
@@ -502,8 +502,8 @@ void Expression::makeCheckRST() {
// Checks that an RPN expression's value fits within N bits (signed or unsigned)
void Expression::checkNBit(uint8_t n) const {
assert(n != 0); // That doesn't make sense
assert(n < CHAR_BIT * sizeof(int)); // Otherwise `1 << n` is UB
assume(n != 0); // That doesn't make sense
assume(n < CHAR_BIT * sizeof(int)); // Otherwise `1 << n` is UB
if (isKnown()) {
if (int32_t val = value(); val < -(1 << n) || val >= 1 << n)

View File

@@ -3,7 +3,6 @@
#include "asm/section.hpp"
#include <algorithm>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <optional>
@@ -125,7 +124,7 @@ Section *sect_FindSectionByName(std::string const &name) {
static unsigned int mergeSectUnion(
Section &sect, SectionType type, uint32_t org, uint8_t alignment, uint16_t alignOffset
) {
assert(alignment < 16); // Should be ensured by the caller
assume(alignment < 16); // Should be ensured by the caller
unsigned int nbSectErrors = 0;
// Unionized sections only need "compatible" constraints, and they end up with the strictest
@@ -177,7 +176,7 @@ static unsigned int mergeSectUnion(
static unsigned int
mergeFragments(Section &sect, uint32_t org, uint8_t alignment, uint16_t alignOffset) {
assert(alignment < 16); // Should be ensured by the caller
assume(alignment < 16); // Should be ensured by the caller
unsigned int nbSectErrors = 0;
// Fragments only need "compatible" constraints, and they end up with the strictest

View File

@@ -2,12 +2,12 @@
#include "asm/symbol.hpp"
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <unordered_map>
#include "error.hpp"
#include "helpers.hpp" // assume
#include "version.hpp"
#include "asm/fstack.hpp"
@@ -55,7 +55,7 @@ static int32_t CallbackPC() {
}
int32_t Symbol::getValue() const {
assert(std::holds_alternative<int32_t>(data) || std::holds_alternative<int32_t (*)()>(data));
assume(std::holds_alternative<int32_t>(data) || std::holds_alternative<int32_t (*)()>(data));
if (auto *value = std::get_if<int32_t>(&data); value) {
return type == SYM_LABEL ? *value + getSection()->org : *value;
}
@@ -73,12 +73,12 @@ int32_t Symbol::getOutputValue() const {
}
ContentSpan const &Symbol::getMacro() const {
assert((std::holds_alternative<ContentSpan>(data)));
assume((std::holds_alternative<ContentSpan>(data)));
return std::get<ContentSpan>(data);
}
std::shared_ptr<std::string> Symbol::getEqus() const {
assert(std::holds_alternative<std::shared_ptr<std::string>>(data));
assume(std::holds_alternative<std::shared_ptr<std::string>>(data));
return std::get<std::shared_ptr<std::string>>(data);
}
@@ -361,7 +361,7 @@ Symbol *sym_AddVar(std::string const &symName, int32_t value) {
* @return The created symbol
*/
static Symbol *addLabel(std::string const &symName) {
assert(!symName.starts_with('.')); // The symbol name must have been expanded prior
assume(!symName.starts_with('.')); // The symbol name must have been expanded prior
Symbol *sym = sym_FindExactSymbol(symName);
if (!sym) {
@@ -390,11 +390,11 @@ static Symbol *addLabel(std::string const &symName) {
// Add a local (`.name` or `Parent.name`) relocatable symbol
Symbol *sym_AddLocalLabel(std::string const &symName) {
// Assuming no dots in `labelScope` if defined
assert(!labelScope.has_value() || labelScope->find('.') == std::string::npos);
assume(!labelScope.has_value() || labelScope->find('.') == std::string::npos);
size_t dotPos = symName.find('.');
assert(dotPos != std::string::npos); // There should be at least one dot in `symName`
assume(dotPos != std::string::npos); // There should be at least one dot in `symName`
// Check for something after the dot
if (dotPos == symName.length() - 1) {

View File

@@ -3,7 +3,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
@@ -774,7 +773,7 @@ static uint8_t maxTitleLen() {
static ssize_t readBytes(int fd, uint8_t *buf, size_t len) {
// POSIX specifies that lengths greater than SSIZE_MAX yield implementation-defined results
assert(len <= SSIZE_MAX);
assume(len <= SSIZE_MAX);
ssize_t total = 0;
@@ -799,7 +798,7 @@ static ssize_t readBytes(int fd, uint8_t *buf, size_t len) {
static ssize_t writeBytes(int fd, uint8_t *buf, size_t len) {
// POSIX specifies that lengths greater than SSIZE_MAX yield implementation-defined results
assert(len <= SSIZE_MAX);
assume(len <= SSIZE_MAX);
ssize_t total = 0;
@@ -869,9 +868,9 @@ static void overwriteBytes(
static void processFile(int input, int output, char const *name, off_t fileSize) {
// Both of these should be true for seekable files, and neither otherwise
if (input == output)
assert(fileSize != 0);
assume(fileSize != 0);
else
assert(fileSize == 0);
assume(fileSize == 0);
uint8_t rom0[BANK_SIZE];
ssize_t rom0Len = readBytes(input, rom0, sizeof(rom0));
@@ -1037,9 +1036,9 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
}
nbBanks = 2;
} else {
assert(rom0Len == sizeof(rom0));
assume(rom0Len == sizeof(rom0));
}
assert(nbBanks >= 2);
assume(nbBanks >= 2);
// Alter number of banks to reflect required value
// x&(x-1) is zero iff x is a power of 2, or 0; we know for sure it's non-zero,
// so this is true (non-zero) when we don't have a power of 2
@@ -1063,7 +1062,7 @@ static void processFile(int input, int output, char const *name, off_t fileSize)
if (fixSpec & (FIX_GLOBAL_SUM | TRASH_GLOBAL_SUM)) {
// Computation of the global checksum does not include the checksum bytes
assert(rom0Len >= 0x14E);
assume(rom0Len >= 0x14E);
for (uint16_t i = 0; i < 0x14E; i++)
globalSum += rom0[i];
for (uint16_t i = 0x150; i < rom0Len; i++)

View File

@@ -3,7 +3,6 @@
#include "gfx/main.hpp"
#include <algorithm>
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <ios>
@@ -17,6 +16,7 @@
#include "extern/getopt.hpp"
#include "file.hpp"
#include "helpers.hpp" // assume
#include "platform.hpp"
#include "version.hpp"
@@ -633,7 +633,7 @@ int main(int argc, char *argv[]) {
if (musl_optind != curArgc) {
// This happens if `--` is passed, process the remaining arg(s) as positional
assert(musl_optind < curArgc);
assume(musl_optind < curArgc);
for (int i = musl_optind; i < curArgc; ++i) {
registerInput(argv[i]);
}
@@ -845,7 +845,7 @@ int main(int argc, char *argv[]) {
void Palette::addColor(uint16_t color) {
for (size_t i = 0; true; ++i) {
assert(i < colors.size()); // The packing should guarantee this
assume(i < colors.size()); // The packing should guarantee this
if (colors[i] == color) { // The color is already present
break;
} else if (colors[i] == UINT16_MAX) { // Empty slot

View File

@@ -3,7 +3,6 @@
#include "gfx/pal_packing.hpp"
#include <algorithm>
#include <assert.h>
#include <deque>
#include <inttypes.h>
#include <optional>
@@ -107,7 +106,7 @@ private:
return it;
}
reference operator*() const {
assert((*_iter).has_value());
assume((*_iter).has_value());
return **_iter;
}
pointer operator->() const {
@@ -308,7 +307,7 @@ static void decant(
// Build up the "component"...
colors.clear();
members.clear();
assert(members.empty()); // Compiler optimization hint
assume(members.empty()); // Compiler optimization hint
do {
ProtoPalette const &protoPal = protoPalettes[attrs->protoPalIndex];
// If this is the first proto-pal, or if at least one color matches, add it

View File

@@ -3,7 +3,6 @@
#include "gfx/pal_sorting.hpp"
#include <algorithm>
#include <assert.h>
#include "helpers.hpp"
@@ -55,7 +54,7 @@ void grayscale(
// This method is only applicable if there are at most as many colors as colors per palette, so
// we should only have a single palette.
assert(palettes.size() == 1);
assume(palettes.size() == 1);
Palette &palette = palettes[0];
std::fill(RANGE(palette.colors), Rgba::transparent);

View File

@@ -3,7 +3,6 @@
#include "gfx/pal_spec.hpp"
#include <algorithm>
#include <assert.h>
#include <charconv>
#include <fstream>
#include <inttypes.h>
@@ -26,13 +25,13 @@ using namespace std::string_view_literals;
constexpr uint8_t nibble(char c) {
if (c >= 'a') {
assert(c <= 'f');
assume(c <= 'f');
return c - 'a' + 10;
} else if (c >= 'A') {
assert(c <= 'F');
assume(c <= 'F');
return c - 'A' + 10;
} else {
assert(c >= '0' && c <= '9');
assume(c >= '0' && c <= '9');
return c - '0';
}
}
@@ -59,8 +58,8 @@ void parseInlinePalSpec(char const * const rawArg) {
auto parseError = [&rawArg, &arg](size_type ofs, size_type len, char const *msg) {
(void)arg; // With NDEBUG, `arg` is otherwise not used
assert(ofs <= arg.length());
assert(len <= arg.length());
assume(ofs <= arg.length());
assume(len <= arg.length());
errorMessage(msg);
fprintf(
@@ -178,7 +177,7 @@ void parseInlinePalSpec(char const * const rawArg) {
*/
template<size_t n>
static bool readMagic(std::filebuf &file, char const *magic) {
assert(strlen(magic) == n);
assume(strlen(magic) == n);
char magicBuf[n];
return file.sgetn(magicBuf, n) == n && memcmp(magicBuf, magic, n);

View File

@@ -3,7 +3,6 @@
#include "gfx/process.hpp"
#include <algorithm>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <optional>
@@ -48,7 +47,7 @@ public:
if (!slot.has_value()) {
slot.emplace(rgba);
} else if (*slot != rgba) {
assert(slot->cgbColor() != UINT16_MAX);
assume(slot->cgbColor() != UINT16_MAX);
return &*slot;
}
return nullptr;
@@ -270,7 +269,7 @@ public:
if (png_get_PLTE(png, info, &embeddedPal, &nbColors) != 0) {
if (png_get_tRNS(png, info, &transparencyPal, &nbTransparentEntries, nullptr)) {
assert(nbTransparentEntries <= nbColors);
assume(nbTransparentEntries <= nbColors);
}
options.verbosePrint(
@@ -326,16 +325,16 @@ public:
// Update `info` with the transformations
png_read_update_info(png, info);
// These shouldn't have changed
assert(png_get_image_width(png, info) == width);
assert(png_get_image_height(png, info) == height);
assume(png_get_image_width(png, info) == width);
assume(png_get_image_height(png, info) == height);
// These should have changed, however
assert(png_get_color_type(png, info) == PNG_COLOR_TYPE_RGBA);
assert(png_get_bit_depth(png, info) == 8);
assume(png_get_color_type(png, info) == PNG_COLOR_TYPE_RGBA);
assume(png_get_bit_depth(png, info) == 8);
// Now that metadata has been read, we can process the image data
size_t nbRowBytes = png_get_rowbytes(png, info);
assert(nbRowBytes != 0);
assume(nbRowBytes != 0);
DefaultInitVec<png_byte> row(nbRowBytes);
// Holds known-conflicting color pairs to avoid warning about them twice.
// We don't need to worry about transitivity, as ImagePalette slots are immutable once
@@ -394,7 +393,7 @@ public:
}
}
} else {
assert(interlaceType == PNG_INTERLACE_ADAM7);
assume(interlaceType == PNG_INTERLACE_ADAM7);
// For interlace to work properly, we must read the image `nbPasses` times
for (int pass = 0; pass < PNG_INTERLACE_ADAM7_PASSES; ++pass) {
@@ -549,7 +548,7 @@ static void generatePalSpec(Png const &png) {
// Fill in the palette spec
options.palSpec.emplace_back(); // A single palette, with `#00000000`s (transparent)
assert(options.palSpec.size() == 1);
assume(options.palSpec.size() == 1);
if (embPalSize > options.maxOpaqueColors()) { // Ignore extraneous colors if they are unused
embPalSize = options.maxOpaqueColors();
}
@@ -568,7 +567,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
// Run a "pagination" problem solver
// TODO: allow picking one of several solvers?
auto [mappings, nbPalettes] = packing::overloadAndRemove(protoPalettes);
assert(mappings.size() == protoPalettes.size());
assume(mappings.size() == protoPalettes.size());
if (options.verbosity >= Options::VERB_INTERM) {
fprintf(
@@ -647,7 +646,7 @@ static std::tuple<DefaultInitVec<size_t>, std::vector<Palette>>
});
if (iter == palettes.end()) {
assert(!protoPal.empty());
assume(!protoPal.empty());
error("Failed to fit tile colors [%s] in specified palettes", listColors(protoPal));
bad = true;
}
@@ -725,7 +724,7 @@ public:
for (uint32_t x = 0; x < 8; ++x) {
row <<= 1;
uint8_t index = palette.indexOf(tile.pixel(x, y).cgbColor());
assert(index < palette.size()); // The color should be in the palette
assume(index < palette.size()); // The color should be in the palette
if (index & 1) {
row |= 1;
}
@@ -803,7 +802,7 @@ public:
}
// If we have both (i.e. we have symmetry), default to vflip only
assert(hasVFlip || hasVHFlip);
assume(hasVFlip || hasVHFlip);
return hasVFlip ? MatchType::VFLIP : MatchType::VHFLIP;
}
friend bool operator==(TileData const &lhs, TileData const &rhs) {
@@ -854,7 +853,7 @@ static void outputTileData(
break;
}
}
assert(remainingTiles == 0);
assume(remainingTiles == 0);
}
static void outputMaps(
@@ -877,7 +876,7 @@ static void outputMaps(
uint8_t bank = 0;
for (auto attr : attrmap) {
if (tileID == options.maxNbTiles[bank]) {
assert(bank == 0);
assume(bank == 0);
bank = 1;
tileID = 0;
}
@@ -976,7 +975,7 @@ static void outputTileData(UniqueTiles const &tiles) {
uint16_t tileID = 0;
for (auto iter = tiles.begin(), end = tiles.end() - options.trim; iter != end; ++iter) {
TileData const *tile = *iter;
assert(tile->tileID == tileID);
assume(tile->tileID == tileID);
++tileID;
output->sputn(reinterpret_cast<char const *>(tile->data().data()), options.bitDepth * 8);
}

View File

@@ -3,7 +3,6 @@
#include "gfx/proto_palette.hpp"
#include <algorithm>
#include <assert.h>
#include "helpers.hpp"
@@ -41,8 +40,8 @@ bool ProtoPalette::add(uint16_t color) {
ProtoPalette::ComparisonResult ProtoPalette::compare(ProtoPalette const &other) const {
// This works because the sets are sorted numerically
assert(std::is_sorted(RANGE(_colorIndices)));
assert(std::is_sorted(RANGE(other._colorIndices)));
assume(std::is_sorted(RANGE(_colorIndices)));
assume(std::is_sorted(RANGE(other._colorIndices)));
auto ours = _colorIndices.begin(), theirs = other._colorIndices.begin();
bool weBigger = true, theyBigger = true;

View File

@@ -4,7 +4,6 @@
#include <algorithm>
#include <array>
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <optional>
@@ -14,6 +13,7 @@
#include "defaultinitalloc.hpp"
#include "file.hpp"
#include "helpers.hpp" // assume
#include "itertools.hpp"
#include "gfx/main.hpp"
@@ -42,7 +42,7 @@ static DefaultInitVec<uint8_t> readInto(std::string const &path) {
// Arbitrary, but if you got a better idea...
size_t newSize = oldSize != data.capacity() ? data.capacity() : oldSize * 2;
assert(oldSize != newSize);
assume(oldSize != newSize);
data.resize(newSize);
}
@@ -343,9 +343,9 @@ void reverse() {
tileID =
(*tilemap)[index] - options.baseTileIDs[bank] + bank * options.maxNbTiles[0];
}
assert(tileID < nbTileInstances); // Should have been checked earlier
assume(tileID < nbTileInstances); // Should have been checked earlier
size_t palID = palmap ? (*palmap)[index] : attribute & 0b111;
assert(palID < palettes.size()); // Should be ensured on data read
assume(palID < palettes.size()); // Should be ensured on data read
// We do not have data for tiles trimmed with `-x`, so assume they are "blank"
static std::array<uint8_t, 16> const trimmedTile{

View File

@@ -3,10 +3,11 @@
#include "gfx/rgba.hpp"
#include <algorithm>
#include <assert.h>
#include <math.h>
#include <stdint.h>
#include "helpers.hpp" // assume
#include "gfx/main.hpp" // options
/*
@@ -37,7 +38,7 @@ uint16_t Rgba::cgbColor() const {
if (isTransparent()) {
return transparent;
}
assert(isOpaque());
assume(isOpaque());
uint8_t r = red, g = green, b = blue;
if (options.useColorCurve) {
@@ -56,7 +57,7 @@ uint16_t Rgba::cgbColor() const {
}
uint8_t Rgba::grayIndex() const {
assert(isGray());
assume(isGray());
// Convert from [0; 256[ to [0; maxOpaqueColors[
return static_cast<uint16_t>(255 - red) * options.maxOpaqueColors() / 256;
}

View File

@@ -3,7 +3,6 @@
#include <sys/stat.h>
#include <sys/types.h>
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
@@ -12,6 +11,7 @@
#include "error.hpp"
#include "extern/getopt.hpp"
#include "helpers.hpp" // assume
#include "itertools.hpp"
#include "platform.hpp"
#include "script.hpp"
@@ -46,28 +46,28 @@ FILE *linkerScript;
static uint32_t nbErrors = 0;
std::vector<uint32_t> &FileStackNode::iters() {
assert(std::holds_alternative<std::vector<uint32_t>>(data));
assume(std::holds_alternative<std::vector<uint32_t>>(data));
return std::get<std::vector<uint32_t>>(data);
}
std::vector<uint32_t> const &FileStackNode::iters() const {
assert(std::holds_alternative<std::vector<uint32_t>>(data));
assume(std::holds_alternative<std::vector<uint32_t>>(data));
return std::get<std::vector<uint32_t>>(data);
}
std::string &FileStackNode::name() {
assert(std::holds_alternative<std::string>(data));
assume(std::holds_alternative<std::string>(data));
return std::get<std::string>(data);
}
std::string const &FileStackNode::name() const {
assert(std::holds_alternative<std::string>(data));
assume(std::holds_alternative<std::string>(data));
return std::get<std::string>(data);
}
std::string const &FileStackNode::dump(uint32_t curLineNo) const {
if (std::holds_alternative<std::vector<uint32_t>>(data)) {
assert(parent); // REPT nodes use their parent's name
assume(parent); // REPT nodes use their parent's name
std::string const &lastName = parent->dump(lineNo);
fputs(" -> ", stderr);
fputs(lastName.c_str(), stderr);
@@ -223,7 +223,7 @@ static void parseScrambleSpec(char const *spec) {
// indicating their scramble limit.
while (spec) {
// Invariant: we should not be pointing at whitespace at this point
assert(*spec != ' ' && *spec != '\t');
assume(*spec != ' ' && *spec != '\t');
// Remember where the region's name begins and ends
char const *regionName = spec;
@@ -232,7 +232,7 @@ static void parseScrambleSpec(char const *spec) {
int regionNamePrintLen = regionNameLen > INT_MAX ? INT_MAX : (int)regionNameLen;
ScrambledRegion region = SCRAMBLE_UNK;
// If this trips, `spec` must be pointing at a ',' or '=' (or NUL) due to the assert
// If this trips, `spec` must be pointing at a ',' or '=' (or NUL) due to the assumption
if (regionNameLen == 0) {
argErr('S', "Missing region name");
@@ -322,7 +322,7 @@ static void parseScrambleSpec(char const *spec) {
next: // Can't `continue` a `for` loop with this nontrivial iteration logic
if (spec) {
assert(*spec == ',' || *spec == '\0');
assume(*spec == ',' || *spec == '\0');
if (*spec == ',')
spec += 1 + strspn(&spec[1], " \t");
if (*spec == '\0')

View File

@@ -3,7 +3,6 @@
#include "link/output.hpp"
#include <algorithm>
#include <assert.h>
#include <deque>
#include <inttypes.h>
#include <stdio.h>
@@ -162,7 +161,7 @@ static void
if (bankSections) {
for (Section const *section : *bankSections) {
assert(section->offset == 0);
assume(section->offset == 0);
// Output padding up to the next SECTION
while (offset + baseOffset < section->org) {
putc(overlayFile ? getc(overlayFile) : padValue, outputFile);
@@ -407,7 +406,7 @@ static void writeMapBank(SortedSections const &sectList, SectionType type, uint3
Section const *sect = *pickedSection;
used += sect->size;
assert(sect->offset == 0);
assume(sect->offset == 0);
writeEmptySpace(prevEndAddr, sect->org);

View File

@@ -2,13 +2,13 @@
#include "link/patch.hpp"
#include <assert.h>
#include <deque>
#include <inttypes.h>
#include <stdint.h>
#include <variant>
#include <vector>
#include "helpers.hpp" // assume
#include "linkdefs.hpp"
#include "opmath.hpp"
@@ -54,7 +54,7 @@ static uint32_t getRPNByte(uint8_t const *&expression, int32_t &size, Patch cons
}
static Symbol const *getSymbol(std::vector<Symbol> const &symbolList, uint32_t index) {
assert(index != (uint32_t)-1); // PC needs to be handled specially, not here
assume(index != (uint32_t)-1); // PC needs to be handled specially, not here
Symbol const &symbol = symbolList[index];
// If the symbol is defined elsewhere...
@@ -297,7 +297,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
isError = true;
value = 1;
} else {
assert(sect->offset == 0);
assume(sect->offset == 0);
value = sect->org;
}
break;
@@ -377,7 +377,7 @@ static int32_t computeRPNExpr(Patch const &patch, std::vector<Symbol> const &fil
} else if (auto *label = std::get_if<Label>(&symbol->data); label) {
value = label->section->org + label->offset;
} else {
assert(std::holds_alternative<int32_t>(symbol->data));
assume(std::holds_alternative<int32_t>(symbol->data));
value = std::get<int32_t>(symbol->data);
}
}

View File

@@ -15,7 +15,6 @@
%code {
#include <algorithm>
#include <array>
#include <assert.h>
#include <bit>
#include <fstream>
#include <inttypes.h>
@@ -329,7 +328,7 @@ yy::parser::symbol_type yylex() {
std::string ident;
auto strUpperCmp = [](char cmp, char ref) {
// `locale::classic()` yields the "C" locale.
assert(!std::use_facet<std::ctype<char>>(std::locale::classic())
assume(!std::use_facet<std::ctype<char>>(std::locale::classic())
.is(std::ctype_base::lower, ref));
return std::use_facet<std::ctype<char>>(std::locale::classic()).toupper(cmp) == ref;
};
@@ -522,7 +521,7 @@ static void alignTo(uint32_t alignment, uint32_t alignOfs) {
return;
}
assert(pc >= typeInfo.startAddr);
assume(pc >= typeInfo.startAddr);
length %= alignSize;
}
@@ -556,7 +555,7 @@ static void pad(uint32_t length) {
auto const &typeInfo = sectionTypeInfo[activeType];
auto &pc = curAddr[activeType][activeBankIdx];
assert(pc >= typeInfo.startAddr);
assume(pc >= typeInfo.startAddr);
if (uint16_t offset = pc - typeInfo.startAddr; length + offset > typeInfo.size) {
scriptError(
context,
@@ -588,7 +587,7 @@ static void placeSection(std::string const &name, bool isOptional) {
}
auto const &typeInfo = sectionTypeInfo[activeType];
assert(section->offset == 0);
assume(section->offset == 0);
// Check that the linker script doesn't contradict what the code says.
if (section->type == SECTTYPE_INVALID) {
// SDCC areas don't have a type assigned yet, so the linker script is used to give them one.

View File

@@ -2,7 +2,6 @@
#include "link/sdas_obj.hpp"
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
#include <memory>
@@ -11,6 +10,7 @@
#include <tuple>
#include <variant>
#include "helpers.hpp" // assume
#include "linkdefs.hpp"
#include "platform.hpp"
@@ -255,7 +255,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
std::unique_ptr<Section> curSection = std::make_unique<Section>();
getToken(line.data(), "'A' line is too short");
assert(strlen(token) != 0); // This should be impossible, tokens are non-empty
assume(strlen(token) != 0); // This should be impossible, tokens are non-empty
// The following is required for fragment offsets to be reliably predicted
for (FileSection &entry : fileSections) {
if (!strcmp(token, entry.section->name.c_str()))
@@ -365,7 +365,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
// Symbols in sections are labels; their value is an offset
Section *section = fileSections.back().section.get();
if (section->isAddressFixed) {
assert(value >= section->org && value <= section->org + section->size);
assume(value >= section->org && value <= section->org + section->size);
value -= section->org;
}
// No need to set the `sectionID`, since we set the pointer
@@ -392,7 +392,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
auto checkSymbol = [](Symbol const &sym) -> std::tuple<Section *, int32_t> {
if (auto *label = std::get_if<Label>(&sym.data); label)
return {label->section, label->offset};
assert(std::holds_alternative<int32_t>(sym.data));
assume(std::holds_alternative<int32_t>(sym.data));
return {nullptr, std::get<int32_t>(sym.data)};
};
auto [symbolSection, symbolValue] = checkSymbol(symbol);
@@ -468,7 +468,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
areaIdx,
fileSections.size()
);
assert(!fileSections.empty()); // There should be at least one, from the above check
assume(!fileSections.empty()); // There should be at least one, from the above check
Section *section = fileSections[areaIdx].section.get();
uint16_t *writeIndex = &fileSections[areaIdx].writeIndex;
uint8_t writtenOfs = ADDR_SIZE; // Bytes before this have been written to `->data`
@@ -500,7 +500,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
*writeIndex
);
if (section->data.empty()) {
assert(section->size != 0);
assume(section->size != 0);
section->data.resize(section->size);
}
}
@@ -584,7 +584,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
uint8_t nbBaseBytes = patch.type == PATCHTYPE_BYTE ? ADDR_SIZE : 2;
uint32_t baseValue = 0;
assert(offset < data.size());
assume(offset < data.size());
if (data.size() - offset < nbBaseBytes)
fatal(
&where,
@@ -755,7 +755,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
patch.rpnExpression.push_back(RPN_AND);
}
} else if (flags & 1 << RELOC_ISPCREL) {
assert(patch.type == PATCHTYPE_WORD);
assume(patch.type == PATCHTYPE_WORD);
fatal(&where, lineNo, "16-bit PC-relative relocations are not supported");
} else if (flags & (1 << RELOC_EXPR16 | 1 << RELOC_EXPR24)) {
fatal(
@@ -769,7 +769,7 @@ void sdobj_ReadFile(FileStackNode const &where, FILE *file, std::vector<Symbol>
// If there is some data left to append, do so
if (writtenOfs != data.size()) {
assert(data.size() > writtenOfs);
assume(data.size() > writtenOfs);
if (*writeIndex + (data.size() - writtenOfs) > section->size)
fatal(
&where,

View File

@@ -2,7 +2,6 @@
#include "link/section.hpp"
#include <assert.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
@@ -183,7 +182,7 @@ static void mergeSections(Section &target, std::unique_ptr<Section> &&other, Sec
for (Patch &patch : other->patches)
patch.pcOffset += other->offset;
} else if (!target.data.empty()) {
assert(other->size == 0);
assume(other->size == 0);
}
break;

View File

@@ -5,18 +5,20 @@
#include <stdlib.h>
#include <unordered_map>
#include "helpers.hpp" // assume
#include "link/main.hpp"
#include "link/section.hpp"
std::unordered_map<std::string, Symbol *> symbols;
Label &Symbol::label() {
assert(std::holds_alternative<Label>(data));
assume(std::holds_alternative<Label>(data));
return std::get<Label>(data);
}
Label const &Symbol::label() const {
assert(std::holds_alternative<Label>(data));
assume(std::holds_alternative<Label>(data));
return std::get<Label>(data);
}