ref: 5c24de3dc4e62c74fcd4f2a6a5055df73ee41a0a
parent: b299f6fb3b8fcf24dc339caf0074707befdbfbce
author: James Larrowe <[email protected]>
date: Wed May 6 12:46:14 EDT 2020
Use inttypes for stdint types This should help make RGBDS portable to systems with 16-bit integers, like DOS. For kicks, use the macros for 16-bit and 8-bit integers. Fix other miscellaneous things, like #include ordering and other printf-format related things. Reduce repitition in math.c while I'm there.
--- a/include/linkdefs.h
+++ b/include/linkdefs.h
@@ -13,8 +13,8 @@
#include <stdint.h>
#define RGBDS_OBJECT_VERSION_STRING "RGB%1hhu"
-#define RGBDS_OBJECT_VERSION_NUMBER (uint8_t)9
-#define RGBDS_OBJECT_REV 4
+#define RGBDS_OBJECT_VERSION_NUMBER 9
+#define RGBDS_OBJECT_REV 4U
enum AssertionType {
ASSERT_WARN,
--- a/src/asm/asmy.y
+++ b/src/asm/asmy.y
@@ -9,6 +9,7 @@
%{
#include <ctype.h>
#include <errno.h>
+#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -79,7 +80,7 @@
strncpy(dest, write_ptr, maxLength + 1);
} else {
fullLength = snprintf(dest, maxLength + 1,
- mode ? mode : "$%X",
+ mode ? mode : "$%" PRIX32,
value);
}
--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -10,14 +10,15 @@
* FileStack routines
*/
-#include <sys/types.h>
-#include <sys/stat.h>
#include <errno.h>
+#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
#include "asm/fstack.h"
@@ -69,7 +70,7 @@
struct sContext **ppFileStack;
if (++nFileStackDepth > nMaxRecursionDepth)
- fatalerror("Recursion limit (%d) exceeded", nMaxRecursionDepth);
+ fatalerror("Recursion limit (%u) exceeded", nMaxRecursionDepth);
ppFileStack = &pFileStack;
while (*ppFileStack)
@@ -271,12 +272,12 @@
pLastFile = pFileStack;
while (pLastFile) {
- fprintf(stderr, "%s(%d) -> ", pLastFile->tzFileName,
+ fprintf(stderr, "%s(%" PRId32 ") -> ", pLastFile->tzFileName,
pLastFile->nLine);
pLastFile = pLastFile->pNext;
}
- fprintf(stderr, "%s(%d)", tzCurrentFileName, nLineNo);
+ fprintf(stderr, "%s(%" PRId32 ")", tzCurrentFileName, nLineNo);
}
void fstk_DumpToStr(char *buf, size_t buflen)
@@ -286,7 +287,7 @@
size_t len = buflen;
while (pLastFile) {
- retcode = snprintf(&buf[buflen - len], len, "%s(%d) -> ",
+ retcode = snprintf(&buf[buflen - len], len, "%s(%" PRId32 ") -> ",
pLastFile->tzFileName, pLastFile->nLine);
if (retcode < 0)
fatalerror("Failed to dump file stack to string: %s",
@@ -298,8 +299,8 @@
pLastFile = pLastFile->pNext;
}
- retcode = snprintf(&buf[buflen - len], len, "%s(%d)", tzCurrentFileName,
- nLineNo);
+ retcode = snprintf(&buf[buflen - len], len, "%s(%" PRId32 ")",
+ tzCurrentFileName, nLineNo);
if (retcode < 0)
fatalerror("Failed to dump file stack to string: %s",
strerror(errno));
--- a/src/asm/lexer.c
+++ b/src/asm/lexer.c
@@ -7,12 +7,13 @@
*/
#include <assert.h>
+#include <ctype.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
-#include <ctype.h>
#include "asm/asm.h"
#include "asm/fstack.h"
@@ -120,7 +121,7 @@
void lex_BeginStringExpansion(const char *tzName)
{
if (++nNbStringExpansions > nMaxRecursionDepth)
- fatalerror("Recursion limit (%d) exceeded", nMaxRecursionDepth);
+ fatalerror("Recursion limit (%u) exceeded", nMaxRecursionDepth);
struct sStringExpansionPos *pNewStringExpansion =
malloc(sizeof(*pNewStringExpansion));
@@ -371,7 +372,7 @@
bool lex_CheckCharacterRange(uint16_t start, uint16_t end)
{
if (start > end || start < 1 || end > 127) {
- yyerror("Invalid character range (start: %u, end: %u)",
+ yyerror("Invalid character range (start: %" PRIu16 ", end: %" PRIu16 ")",
start, end);
return false;
}
@@ -672,7 +673,7 @@
* so it's handled differently
*/
static const char * const formatSpecifiers[] = {
- "", "%x", "%X", "%d"
+ "", "%" PRIx32, "%" PRIX32, "%" PRId32
};
/* Prevent reading out of bounds! */
const char *designatedMode;
--- a/src/asm/macro.c
+++ b/src/asm/macro.c
@@ -1,5 +1,6 @@
#include <assert.h>
+#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -82,7 +83,7 @@
uniqueIDPtr = NULL;
} else {
/* The buffer is guaranteed to be the correct size */
- sprintf(uniqueIDBuf, "_%u", id);
+ sprintf(uniqueIDBuf, "_%" PRIu32, id);
uniqueIDPtr = uniqueIDBuf;
}
}
--- a/src/asm/main.c
+++ b/src/asm/main.c
@@ -167,7 +167,7 @@
/* fallthrough */
case 'p':
if (strlen(&s[1]) <= 2) {
- int32_t result;
+ int result;
unsigned int fillchar;
result = sscanf(&s[1], "%x", &fillchar);
@@ -551,10 +551,11 @@
fclose(dependfile);
if (nIFDepth != 0)
- errx(1, "Unterminated IF construct (%u levels)!", nIFDepth);
+ errx(1, "Unterminated IF construct (%" PRIu32 " levels)!",
+ nIFDepth);
if (nUnionDepth != 0) {
- errx(1, "Unterminated UNION construct (%u levels)!",
+ errx(1, "Unterminated UNION construct (%" PRIu32 " levels)!",
nUnionDepth);
}
@@ -564,8 +565,9 @@
timespent = ((double)(nEndClock - nStartClock))
/ (double)CLOCKS_PER_SEC;
if (verbose) {
- printf("Success! %u lines in %d.%02d seconds ", nTotalLines,
- (int)timespent, ((int)(timespent * 100.0)) % 100);
+ printf("Success! %" PRIu32 " lines in %d.%02d seconds ",
+ nTotalLines, (int)timespent,
+ ((int)(timespent * 100.0)) % 100);
if (timespent < FLT_MIN_EXP)
printf("(INFINITY lines/minute)\n");
else
--- a/src/asm/math.c
+++ b/src/asm/math.c
@@ -10,6 +10,7 @@
* Fixedpoint math routines
*/
+#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
@@ -37,12 +38,16 @@
*/
void math_Print(int32_t i)
{
- if (i >= 0)
- printf("%d.%05d", i >> 16,
- ((int32_t)(fx2double(i) * 100000 + 0.5)) % 100000);
- else
- printf("-%d.%05d", (-i) >> 16,
- ((int32_t)(fx2double(-i) * 100000 + 0.5)) % 100000);
+ uint32_t u = i;
+ const char *sign = "";
+
+ if (i < 0) {
+ u = -u;
+ sign = "-";
+ }
+
+ printf("%s%" PRIu32 ".%05" PRIu32, sign, u >> 16,
+ ((uint32_t)(fx2double(u) * 100000 + 0.5)) % 100000);
}
/*
--- a/src/asm/rpn.c
+++ b/src/asm/rpn.c
@@ -12,6 +12,7 @@
#include <assert.h>
#include <errno.h>
+#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -50,8 +51,8 @@
* To avoid generating humongous object files, cap the
* size of RPN expressions
*/
- fatalerror("RPN expression cannot grow larger than %d bytes",
- MAXRPNLEN);
+ fatalerror("RPN expression cannot grow larger than %lu bytes",
+ (unsigned long)MAXRPNLEN);
else if (expr->nRPNCapacity > MAXRPNLEN / 2)
expr->nRPNCapacity = MAXRPNLEN;
else
@@ -222,7 +223,8 @@
/* That range is valid, but only keep the lower byte */
expr->nVal &= 0xFF;
} else if (expr->nVal < 0 || expr->nVal > 0xFF) {
- yyerror("Source address $%x not in $FF00 to $FFFF", expr->nVal);
+ yyerror("Source address $%" PRIx32 " not between $FF00 to $FFFF",
+ expr->nVal);
}
}
@@ -233,7 +235,8 @@
if (rpn_isKnown(expr)) {
/* A valid RST address must be masked with 0x38 */
if (expr->nVal & ~0x38)
- yyerror("Invalid address $%x for RST", expr->nVal);
+ yyerror("Invalid address $%" PRIx32 " for RST",
+ expr->nVal);
/* The target is in the "0x38" bits, all other bits are set */
expr->nVal |= 0xC7;
} else {
@@ -260,7 +263,7 @@
if (amount >= 0) {
// Left shift
if (amount >= 32) {
- warning(WARNING_SHIFT_AMOUNT, "Shifting left by large amount %d",
+ warning(WARNING_SHIFT_AMOUNT, "Shifting left by large amount %" PRId32,
amount);
return 0;
@@ -276,7 +279,7 @@
// Right shift
amount = -amount;
if (amount >= 32) {
- warning(WARNING_SHIFT_AMOUNT, "Shifting right by large amount %d",
+ warning(WARNING_SHIFT_AMOUNT, "Shifting right by large amount %" PRId32,
amount);
return shiftee < 0 ? -1 : 0;
@@ -289,7 +292,7 @@
* undefined, so use a left shift manually sign-extended
*/
return (uint32_t)shiftee >> amount
- | -((uint32_t)1 << (32 - amount));
+ | -(UINT32_C(1) << (32 - amount));
}
}
}
@@ -370,7 +373,7 @@
break;
case RPN_SHL:
if (src2->nVal < 0)
- warning(WARNING_SHIFT_AMOUNT, "Shifting left by negative amount %d",
+ warning(WARNING_SHIFT_AMOUNT, "Shifting left by negative amount %" PRId32,
src2->nVal);
expr->nVal = shift(src1->nVal, src2->nVal);
@@ -377,11 +380,11 @@
break;
case RPN_SHR:
if (src1->nVal < 0)
- warning(WARNING_SHIFT, "Shifting negative value %d",
+ warning(WARNING_SHIFT, "Shifting negative value %" PRId32,
src1->nVal);
if (src2->nVal < 0)
- warning(WARNING_SHIFT_AMOUNT, "Shifting right by negative amount %d",
+ warning(WARNING_SHIFT_AMOUNT, "Shifting right by negative amount %" PRId32,
src2->nVal);
expr->nVal = shift(src1->nVal, -src2->nVal);
@@ -421,7 +424,7 @@
case RPN_RST:
case RPN_CONST:
case RPN_SYM:
- fatalerror("%d is no binary operator", op);
+ fatalerror("%d is not a binary operator", op);
}
} else if (op == RPN_SUB && isDiffConstant(src1, src2)) {
--- a/src/asm/section.c
+++ b/src/asm/section.c
@@ -1,9 +1,10 @@
#include <errno.h>
+#include <inttypes.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdbool.h>
#include "asm/fstack.h"
#include "asm/main.h"
@@ -64,7 +65,7 @@
* A check at the linking stage is still necessary.
*/
if (newSize > maxSize)
- fatalerror("Section '%s' is too big (max size = 0x%X bytes, reached 0x%X).",
+ fatalerror("Section '%s' is too big (max size = 0x%" PRIX32 " bytes, reached 0x%" PRIX32 ").",
pCurrentSection->pzName, maxSize, newSize);
}
@@ -100,14 +101,14 @@
yyerror("BANK only allowed for ROMX, WRAMX, SRAM, or VRAM sections");
else if (bank < bankranges[type][0]
|| bank > bankranges[type][1])
- yyerror("%s bank value $%x out of range ($%x to $%x)",
+ yyerror("%s bank value $%" PRIx32 " out of range ($%" PRIx32 " to $%" PRIx32 ")",
typeNames[type], bank,
bankranges[type][0], bankranges[type][1]);
}
if (alignOffset >= 1 << alignment) {
- yyerror("Alignment offset must not be greater than alignment (%u < %u)",
- alignOffset, 1 << alignment);
+ yyerror("Alignment offset must not be greater than alignment (%" PRIu16 " < %u)",
+ alignOffset, 1U << alignment);
alignOffset = 0;
}
@@ -128,7 +129,7 @@
if (org != -1) {
if (org < startaddr[type] || org > endaddr(type))
- yyerror("Section \"%s\"'s fixed address %#x is outside of range [%#x; %#x]",
+ yyerror("Section \"%s\"'s fixed address %#" PRIx32 " is outside of range [%#" PRIx16 "; %#" PRIx16 "]",
pzName, org, startaddr[type], endaddr(type));
}
@@ -167,13 +168,13 @@
if (org != -1) {
/* If both are fixed, they must be the same */
if (pSect->nOrg != -1 && pSect->nOrg != org)
- fail("Section \"%s\" already declared as fixed at different address $%x",
+ fail("Section \"%s\" already declared as fixed at different address $%" PRIx32,
pSect->pzName, pSect->nOrg);
else if (pSect->nAlign != 0
&& (mask(pSect->nAlign)
& (org - pSect->alignOfs)))
- fail("Section \"%s\" already declared as aligned to %u bytes (offset %u)",
- pSect->pzName, 1 << pSect->nAlign,
+ fail("Section \"%s\" already declared as aligned to %u bytes (offset %" PRIu16 ")",
+ pSect->pzName, 1U << pSect->nAlign,
pSect->alignOfs);
else
/* Otherwise, just override */
@@ -183,7 +184,7 @@
if (pSect->nOrg != -1) {
if ((pSect->nOrg - alignOffset)
& mask(alignment))
- fail("Section \"%s\" already declared as fixed at incompatible address $%x",
+ fail("Section \"%s\" already declared as fixed at incompatible address $%" PRIx32,
pSect->pzName,
pSect->nOrg);
/* Check if alignment offsets are compatible */
@@ -190,7 +191,7 @@
} else if ((alignOffset & mask(pSect->nAlign))
!= (pSect->alignOfs
& mask(alignment))) {
- fail("Section \"%s\" already declared with incompatible %u-byte alignment (offset %u)",
+ fail("Section \"%s\" already declared with incompatible %" PRIu8 "-byte alignment (offset %" PRIu16 ")",
pSect->pzName, pSect->nAlign,
pSect->alignOfs);
} else if (alignment > pSect->nAlign) {
@@ -207,7 +208,7 @@
pSect->nBank = bank;
/* If both specify a bank, it must be the same one */
else if (bank != -1 && pSect->nBank != bank)
- fail("Section \"%s\" already declared with different bank %u",
+ fail("Section \"%s\" already declared with different bank %" PRIu32,
pSect->pzName, pSect->nBank);
} else {
if (pSect->isUnion)
@@ -218,7 +219,7 @@
fail("Section \"%s\" already declared as floating",
pSect->pzName);
else
- fail("Section \"%s\" already declared as fixed at $%x",
+ fail("Section \"%s\" already declared as fixed at $%" PRIx32,
pSect->pzName, pSect->nOrg);
}
if (bank != pSect->nBank) {
@@ -226,7 +227,7 @@
fail("Section \"%s\" already declared as floating bank",
pSect->pzName);
else
- fail("Section \"%s\" already declared as fixed at bank %u",
+ fail("Section \"%s\" already declared as fixed at bank %" PRIu32,
pSect->pzName, pSect->nBank);
}
if (alignment != pSect->nAlign) {
@@ -235,7 +236,8 @@
pSect->pzName);
else
fail("Section \"%s\" already declared as aligned to %u bytes",
- pSect->pzName, 1 << pSect->nAlign);
+ pSect->pzName,
+ 1U << pSect->nAlign);
}
}
@@ -359,12 +361,12 @@
if (sect->nOrg != -1) {
if ((sym_GetPCValue() - offset) % (1 << alignment))
- yyerror("Section's fixed address fails required alignment (PC = $%04x)",
+ yyerror("Section's fixed address fails required alignment (PC = $%04" PRIx32 ")",
sym_GetPCValue());
} else if (sect->nAlign != 0) {
if ((((sect->alignOfs + curOffset) % (1 << sect->nAlign))
- offset) % (1 << alignment)) {
- yyerror("Section's alignment fails required alignment (offset from section start = $%04x)",
+ yyerror("Section's alignment fails required alignment (offset from section start = $%04" PRIx32 ")",
curOffset);
} else if (alignment > sect->nAlign) {
sect->nAlign = alignment;
@@ -560,7 +562,7 @@
int16_t offset = expr->nVal - address;
if (offset < -128 || offset > 127) {
- yyerror("jr target out of reach (expected -129 < %d < 128)",
+ yyerror("jr target out of reach (expected -129 < %" PRId16 " < 128)",
offset);
writebyte(0);
} else {
@@ -616,12 +618,13 @@
void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
{
if (start_pos < 0) {
- yyerror("Start position cannot be negative (%d)", start_pos);
+ yyerror("Start position cannot be negative (%" PRId32 ")",
+ start_pos);
start_pos = 0;
}
if (length < 0) {
- yyerror("Number of bytes to read cannot be negative (%d)",
+ yyerror("Number of bytes to read cannot be negative (%" PRId32 ")",
length);
length = 0;
}
@@ -676,7 +679,7 @@
yyerror("Error reading INCBIN file '%s': %s", s,
strerror(errno));
} else {
- yyerror("Premature end of file (%d bytes left to read)",
+ yyerror("Premature end of file (%" PRId32 " bytes left to read)",
todo + 1);
}
}
--- a/src/asm/symbol.c
+++ b/src/asm/symbol.c
@@ -12,6 +12,7 @@
#include <assert.h>
#include <errno.h>
+#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -286,7 +287,7 @@
if (!symbol)
symbol = createsymbol(symbolName);
else if (sym_IsDefined(symbol))
- yyerror("'%s' already defined at %s(%u)", symbolName,
+ yyerror("'%s' already defined at %s(%" PRIu32 ")", symbolName,
symbol->fileName, symbol->fileLine);
return symbol;
@@ -346,8 +347,8 @@
if (sym == NULL)
sym = createsymbol(symName);
else if (sym_IsDefined(sym) && sym->type != SYM_SET)
- yyerror("'%s' already defined as %s at %s(%u)", symName,
- sym->type == SYM_LABEL ? "label" : "constant",
+ yyerror("'%s' already defined as %s at %s(%" PRIu32 ")",
+ symName, sym->type == SYM_LABEL ? "label" : "constant",
sym->fileName, sym->fileLine);
else
/* TODO: can the scope be incorrect when talking over refs? */
@@ -407,7 +408,7 @@
if (!sym)
sym = createsymbol(symName);
else if (sym_IsDefined(sym))
- yyerror("'%s' already defined in %s(%d)", symName,
+ yyerror("'%s' already defined in %s(%" PRIu32 ")", symName,
sym->fileName, sym->fileLine);
/* If the symbol already exists as a ref, just "take over" it */
--- a/src/gfx/main.c
+++ b/src/gfx/main.c
@@ -210,7 +210,7 @@
if (opts.trim &&
opts.trim > (raw_image->width / 8) * (raw_image->height / 8) - 1) {
- errx(1, "Trim (%i) for input raw_image file '%s' too large (max: %i)",
+ errx(1, "Trim (%d) for input raw_image file '%s' too large (max: %u)",
opts.trim, opts.infile,
(raw_image->width / 8) * (raw_image->height / 8) - 1);
}
--- a/src/gfx/makepng.c
+++ b/src/gfx/makepng.c
@@ -38,7 +38,7 @@
if (img.depth != depth) {
if (opts->verbose) {
- warnx("Image bit depth is not %i (is %i).",
+ warnx("Image bit depth is not %d (is %d).",
depth, img.depth);
}
}
--- a/src/link/assign.c
+++ b/src/link/assign.c
@@ -6,6 +6,7 @@
* SPDX-License-Identifier: MIT
*/
+#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@@ -51,7 +52,7 @@
memory[type][bank].next =
malloc(sizeof(*memory[type][0].next));
if (!memory[type][bank].next)
- err(1, "Failed to init free space for region %d bank %u",
+ err(1, "Failed to init free space for region %d bank %" PRIu32,
type, bank);
memory[type][bank].next->address = startaddr[type];
memory[type][bank].next->size = maxsize[type];
@@ -304,19 +305,22 @@
if (section->isBankFixed && nbbanks(section->type) != 1) {
if (section->isAddressFixed)
- snprintf(where, 64, "at $%02x:%04x",
+ snprintf(where, 64, "at $%02" PRIx32 ":%04" PRIx16,
section->bank, section->org);
else if (section->isAlignFixed)
- snprintf(where, 64, "in bank $%02x with align mask %x",
- section->bank, ~section->alignMask);
+ snprintf(where, 64, "in bank $%02" PRIx32 " with align mask %" PRIx16,
+ section->bank, (uint16_t)~section->alignMask);
else
- snprintf(where, 64, "in bank $%02x", section->bank);
+ snprintf(where, 64, "in bank $%02" PRIx32,
+ section->bank);
} else {
if (section->isAddressFixed)
- snprintf(where, 64, "at address $%04x", section->org);
+ snprintf(where, 64, "at address $%04" PRIx16,
+ section->org);
else if (section->isAlignFixed)
- snprintf(where, 64, "with align mask %x and offset %u",
- ~section->alignMask, section->alignOfs);
+ snprintf(where, 64, "with align mask %" PRIx16 " and offset %" PRIx16,
+ (uint16_t)~section->alignMask,
+ section->alignOfs);
else
strcpy(where, "anywhere");
}
@@ -327,7 +331,7 @@
section->name, typeNames[section->type], where);
/* If the section just can't fit the bank, report that */
else if (section->org + section->size > endaddr(section->type) + 1)
- errx(1, "Unable to place \"%s\" (%s section) %s: section runs past end of region ($%04x > $%04x)",
+ errx(1, "Unable to place \"%s\" (%s section) %s: section runs past end of region ($%04" PRIx16 " > $%04" PRIx16 ")",
section->name, typeNames[section->type], where,
section->org + section->size, endaddr(section->type) + 1);
/* Otherwise there is overlap with another section */
@@ -418,7 +422,7 @@
/* Overlaying requires only fully-constrained sections */
verbosePrint("Assigning other sections...\n");
if (overlayFileName)
- errx(1, "All sections must be fixed when using an overlay file; %lu %sn't",
+ errx(1, "All sections must be fixed when using an overlay file; %" PRIu64 " %sn't",
nbSectionsToAssign, nbSectionsToAssign == 1 ? "is" : "are");
/* Assign all remaining sections by decreasing constraint order */
--- a/src/link/main.c
+++ b/src/link/main.c
@@ -6,12 +6,13 @@
* SPDX-License-Identifier: MIT
*/
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <stdio.h>
+#include <inttypes.h>
#include <stdbool.h>
+#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/types.h>
#include "link/object.h"
#include "link/symbol.h"
@@ -65,7 +66,7 @@
if (nbErrors != UINT32_MAX)
nbErrors++;
- fprintf(stderr, "Linking aborted after %u error%s\n", nbErrors,
+ fprintf(stderr, "Linking aborted after %" PRIu32 " error%s\n", nbErrors,
nbErrors != 1 ? "s" : "");
exit(1);
}
@@ -245,8 +246,8 @@
/* and finally output the result. */
patch_ApplyPatches();
if (nbErrors) {
- fprintf(stderr, "Linking failed with %u error%s\n", nbErrors,
- nbErrors != 1 ? "s" : "");
+ fprintf(stderr, "Linking failed with %" PRIu32 " error%s\n",
+ nbErrors, nbErrors != 1 ? "s" : "");
exit(1);
}
out_WriteFiles();
--- a/src/link/object.c
+++ b/src/link/object.c
@@ -6,12 +6,13 @@
* SPDX-License-Identifier: MIT
*/
+#include <errno.h>
+#include <inttypes.h>
+#include <limits.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
-#include <errno.h>
-#include <limits.h>
#include "link/assign.h"
#include "link/main.h"
@@ -212,25 +213,25 @@
struct Section *fileSections[])
{
tryReadstr(patch->fileName, file,
- "%s: Unable to read \"%s\"'s patch #%u's name: %s",
+ "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s name: %s",
fileName, sectName, i);
tryReadlong(patch->offset, file,
- "%s: Unable to read \"%s\"'s patch #%u's offset: %s",
+ "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s offset: %s",
fileName, sectName, i);
tryReadlong(patch->pcSectionID, file,
- "%s: Unable to read \"%s\"'s patch #%u's PC offset: %s",
+ "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName, i);
patch->pcSection = patch->pcSectionID == -1
? NULL
: fileSections[patch->pcSectionID];
tryReadlong(patch->pcOffset, file,
- "%s: Unable to read \"%s\"'s patch #%u's PC offset: %s",
+ "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName, i);
tryGetc(patch->type, file,
- "%s: Unable to read \"%s\"'s patch #%u's type: %s",
+ "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s",
fileName, sectName, i);
tryReadlong(patch->rpnSize, file,
- "%s: Unable to read \"%s\"'s patch #%u's RPN size: %s",
+ "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s",
fileName, sectName, i);
uint8_t *rpnExpression =
@@ -239,7 +240,7 @@
patch->rpnSize, file);
if (nbElementsRead != patch->rpnSize)
- errx(1, "%s: Cannot read \"%s\"'s patch #%u's RPN expression: %s",
+ errx(1, "%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s",
fileName, sectName, i,
feof(file) ? "Unexpected end of file" : strerror(errno));
patch->rpnExpression = rpnExpression;
@@ -262,8 +263,8 @@
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s' size: %s",
fileName, section->name);
if (tmp < 0 || tmp > UINT16_MAX)
- errx(1, "\"%s\"'s section size (%d) is invalid", section->name,
- tmp);
+ errx(1, "\"%s\"'s section size (%" PRId32 ") is invalid",
+ section->name, tmp);
section->size = tmp;
tryGetc(byte, file, "%s: Cannot read \"%s\"'s type: %s",
fileName, section->name);
@@ -273,7 +274,8 @@
fileName, section->name);
section->isAddressFixed = tmp >= 0;
if (tmp > UINT16_MAX) {
- error("\"%s\"'s org is too large (%d)", section->name, tmp);
+ error("\"%s\"'s org is too large (%" PRId32 ")",
+ section->name, tmp);
tmp = UINT16_MAX;
}
section->org = tmp;
@@ -288,7 +290,7 @@
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s",
fileName, section->name);
if (tmp > UINT16_MAX) {
- error("\"%s\"'s alignment offset is too large (%d)",
+ error("\"%s\"'s alignment offset is too large (%" PRId32 ")",
section->name, tmp);
tmp = UINT16_MAX;
}
@@ -371,7 +373,7 @@
{
char assertName[sizeof("Assertion #" EXPAND_AND_STR(UINT32_MAX))];
- snprintf(assertName, sizeof(assertName), "Assertion #%u", i);
+ snprintf(assertName, sizeof(assertName), "Assertion #%" PRIu32, i);
readPatch(file, &assert->patch, fileName, assertName, 0, fileSections);
tryReadstr(assert->message, file, "%s: Cannot read assertion's message: %s",
@@ -417,7 +419,7 @@
tryReadlong(revNum, file, "%s: Cannot read revision number: %s",
fileName);
if (revNum != RGBDS_OBJECT_REV)
- errx(1, "%s is a revision 0x%04x object file, only 0x%04x is supported",
+ errx(1, "%s is a revision 0x%04" PRIx32 " object file; only 0x%04x is supported",
fileName, revNum, RGBDS_OBJECT_REV);
uint32_t nbSymbols;
@@ -450,7 +452,7 @@
memset(nbSymPerSect, 0, sizeof(nbSymPerSect));
- verbosePrint("Reading %u symbols...\n", nbSymbols);
+ verbosePrint("Reading %" PRIu32 " symbols...\n", nbSymbols);
for (uint32_t i = 0; i < nbSymbols; i++) {
/* Read symbol */
struct Symbol *symbol = malloc(sizeof(*symbol));
@@ -469,7 +471,7 @@
/* This file's sections, stored in a table to link symbols to them */
struct Section *fileSections[nbSections ? nbSections : 1];
- verbosePrint("Reading %u sections...\n", nbSections);
+ verbosePrint("Reading %" PRIu32 " sections...\n", nbSections);
for (uint32_t i = 0; i < nbSections; i++) {
/* Read section */
fileSections[i] = malloc(sizeof(*fileSections[i]));
@@ -511,7 +513,7 @@
tryReadlong(nbAsserts, file, "%s: Cannot read number of assertions: %s",
fileName);
- verbosePrint("Reading %u assertions...\n", nbAsserts);
+ verbosePrint("Reading %" PRIu32 " assertions...\n", nbAsserts);
for (uint32_t i = 0; i < nbAsserts; i++) {
struct Assertion *assertion = malloc(sizeof(*assertion));
--- a/src/link/output.c
+++ b/src/link/output.c
@@ -6,8 +6,9 @@
* SPDX-License-Identifier: MIT
*/
-#include <stdlib.h>
+#include <inttypes.h>
#include <stdint.h>
+#include <stdlib.h>
#include "link/output.h"
#include "link/main.h"
@@ -51,7 +52,7 @@
uint32_t minNbBanks = targetBank + 1;
if (minNbBanks > maxNbBanks[section->type])
- errx(1, "Section \"%s\" has invalid bank range (%u > %u)",
+ errx(1, "Section \"%s\" has an invalid bank range (%" PRIu32 " > %" PRIu32 ")",
section->name, section->bank,
maxNbBanks[section->type] - 1);
@@ -283,7 +284,7 @@
minSectList = &zlSectList;
}
- fprintf(symFile, "%02x:%04x %s\n",
+ fprintf(symFile, "%02" PRIx32 ":%04" PRIx16 " %s\n",
minSectList->sect->bank, minSectList->addr,
minSectList->sym->name);
minSectList->i++;
@@ -304,7 +305,7 @@
struct SortedSection const *section = sectList->sections;
struct SortedSection const *zeroLenSection = sectList->zeroLenSections;
- fprintf(mapFile, "%s bank #%u:\n", typeNames[type],
+ fprintf(mapFile, "%s bank #%" PRIu32 ":\n", typeNames[type],
bank + bankranges[type][0]);
uint16_t slack = maxsize[type];
@@ -317,16 +318,16 @@
slack -= sect->size;
if (sect->size != 0)
- fprintf(mapFile, " SECTION: $%04x-$%04x ($%04x byte%s) [\"%s\"]\n",
+ fprintf(mapFile, " SECTION: $%04" PRIx16 "-$%04" PRIx16 " ($%04" PRIx16 " byte%s) [\"%s\"]\n",
sect->org, sect->org + sect->size - 1,
sect->size, sect->size == 1 ? "" : "s",
sect->name);
else
- fprintf(mapFile, " SECTION: $%04x (0 bytes) [\"%s\"]\n",
+ fprintf(mapFile, " SECTION: $%04" PRIx16 " (0 bytes) [\"%s\"]\n",
sect->org, sect->name);
for (size_t i = 0; i < sect->nbSymbols; i++)
- fprintf(mapFile, " $%04x = %s\n",
+ fprintf(mapFile, " $%04" PRIx32 " = %s\n",
sect->symbols[i]->offset + sect->org,
sect->symbols[i]->name);
@@ -336,7 +337,7 @@
if (slack == maxsize[type])
fputs(" EMPTY\n\n", mapFile);
else
- fprintf(mapFile, " SLACK: $%04x byte%s\n\n", slack,
+ fprintf(mapFile, " SLACK: $%04" PRIx16 " byte%s\n\n", slack,
slack == 1 ? "" : "s");
}
--- a/src/link/patch.c
+++ b/src/link/patch.c
@@ -6,8 +6,9 @@
* SPDX-License-Identifier: MIT
*/
-#include <stdlib.h>
+#include <inttypes.h>
#include <limits.h>
+#include <stdlib.h>
#include <string.h>
#include "link/patch.h"
@@ -316,7 +317,7 @@
if (value < 0
|| (value > 0xFF && value < 0xFF00)
|| value > 0xFFFF)
- error("%s: Value %d is not in HRAM range",
+ error("%s: Value %" PRId32 " is not in HRAM range",
patch->fileName, value);
value &= 0xFF;
break;
@@ -327,7 +328,7 @@
* They can be easily checked with a bitmask
*/
if (value & ~0x38)
- error("%s: Value %d is not a RST vector",
+ error("%s: Value %" PRId32 " is not a RST vector",
patch->fileName, value);
value |= 0xC7;
break;
@@ -370,7 +371,7 @@
}
if (stack.size > 1)
- error("%s: RPN stack has %lu entries on exit, not 1",
+ error("%s: RPN stack has %zu entries on exit, not 1",
patch->fileName, stack.size);
return popRPN();
@@ -440,7 +441,7 @@
int16_t offset = value - address;
if (offset < -128 || offset > 127)
- error("%s: jr target out of reach (expected -129 < %d < 128)",
+ error("%s: jr target out of reach (expected -129 < %" PRId16 " < 128)",
patch->fileName, offset);
section->data[patch->offset] = offset & 0xFF;
} else {
@@ -457,10 +458,10 @@
if (value < types[patch->type].min
|| value > types[patch->type].max)
- error("%s: Value %#x%s is not %u-bit",
+ error("%s: Value %#" PRIx32 "%s is not %u-bit",
patch->fileName, value,
value < 0 ? " (maybe negative?)" : "",
- types[patch->type].size * 8);
+ types[patch->type].size * 8U);
for (uint8_t i = 0; i < types[patch->type].size; i++) {
section->data[patch->offset + i] = value & 0xFF;
value >>= 8;
--- a/src/link/script.c
+++ b/src/link/script.c
@@ -6,11 +6,12 @@
* SPDX-License-Identifier: MIT
*/
-#include <stdlib.h>
+#include <ctype.h>
+#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
+#include <stdlib.h>
#include <string.h>
-#include <ctype.h>
#include "link/main.h"
#include "link/script.h"
@@ -35,7 +36,7 @@
static void pushFile(char *newFileName)
{
if (fileStackIndex == UINT32_MAX)
- errx(1, "%s(%u): INCLUDE recursion limit reached",
+ errx(1, "%s(%" PRIu32 "): INCLUDE recursion limit reached",
linkerScriptName, lineNo);
if (fileStackIndex == fileStackSize) {
@@ -45,7 +46,7 @@
fileStack = realloc(fileStack,
sizeof(*fileStack) * fileStackSize);
if (!fileStack)
- err(1, "%s(%u): Internal INCLUDE error",
+ err(1, "%s(%" PRIu32 "): Internal INCLUDE error",
linkerScriptName, lineNo);
}
@@ -56,7 +57,7 @@
linkerScript = fopen(newFileName, "r");
if (!linkerScript)
- err(1, "%s(%u): Could not open \"%s\"",
+ err(1, "%s(%" PRIu32 "): Could not open \"%s\"",
linkerScriptName, lineNo, newFileName);
lineNo = 1;
linkerScriptName = newFileName;
@@ -177,8 +178,8 @@
int curchar = getc(file);
if (curchar == EOF && ferror(file))
- err(1, "%s(%u): Unexpected error in %s", linkerScriptName,
- lineNo, __func__);
+ err(1, "%s(%" PRIu32 "): Unexpected error in %s",
+ linkerScriptName, lineNo, __func__);
return curchar;
}
@@ -223,7 +224,7 @@
do {
curchar = readChar(linkerScript);
if (curchar == EOF || isNewline(curchar))
- errx(1, "%s(%u): Unterminated string",
+ errx(1, "%s(%" PRIu32 "): Unterminated string",
linkerScriptName, lineNo);
else if (curchar == '"')
/* Quotes force a string termination */
@@ -302,7 +303,7 @@
if (tryParseNumber(str, &token.attr.number))
token.type = TOKEN_NUMBER;
else
- errx(1, "%s(%u): Unknown token \"%s\"",
+ errx(1, "%s(%" PRIu32 "): Unknown token \"%s\"",
linkerScriptName, lineNo, str);
}
@@ -330,7 +331,7 @@
}
if (arg < *pc)
- errx(1, "%s(%u): `%s` cannot be used to go backwards",
+ errx(1, "%s(%" PRIu32 "): `%s` cannot be used to go backwards",
linkerScriptName, lineNo, commands[command]);
*pc = arg;
}
@@ -379,11 +380,11 @@
if (type != SECTTYPE_INVALID) {
if (curaddr[type][bankID] > endaddr(type) + 1)
- errx(1, "%s(%u): Sections would extend past the end of %s ($%04hx > $%04hx)",
+ errx(1, "%s(%" PRIu32 "): Sections would extend past the end of %s ($%04" PRIx16 " > $%04" PRIx16 ")",
linkerScriptName, lineNo, typeNames[type],
curaddr[type][bankID], endaddr(type));
if (curaddr[type][bankID] < startaddr[type])
- errx(1, "%s(%u): PC underflowed ($%04hx < $%04hx)",
+ errx(1, "%s(%" PRIu32 "): PC underflowed ($%04" PRIx16 " < $%04" PRIx16 ")",
linkerScriptName, lineNo,
curaddr[type][bankID], startaddr[type]);
}
@@ -404,7 +405,7 @@
break;
case TOKEN_NUMBER:
- errx(1, "%s(%u): stray number \"%u\"",
+ errx(1, "%s(%" PRIu32 "): stray number \"%" PRIu32 "\"",
linkerScriptName, lineNo,
token->attr.number);
@@ -417,13 +418,13 @@
parserState = PARSER_LINEEND;
if (type == SECTTYPE_INVALID)
- errx(1, "%s(%u): Didn't specify a location before the section",
+ errx(1, "%s(%" PRIu32 "): Didn't specify a location before the section",
linkerScriptName, lineNo);
section.section =
sect_GetSection(token->attr.string);
if (!section.section)
- errx(1, "%s(%u): Unknown section \"%s\"",
+ errx(1, "%s(%" PRIu32 "): Unknown section \"%s\"",
linkerScriptName, lineNo,
token->attr.string);
section.org = curaddr[type][bankID];
@@ -452,10 +453,10 @@
if (tokType == TOKEN_COMMAND) {
if (type == SECTTYPE_INVALID)
- errx(1, "%s(%u): Didn't specify a location before the command",
+ errx(1, "%s(%" PRIu32 "): Didn't specify a location before the command",
linkerScriptName, lineNo);
if (!hasArg)
- errx(1, "%s(%u): Command specified without an argument",
+ errx(1, "%s(%" PRIu32 "): Command specified without an argument",
linkerScriptName, lineNo);
processCommand(attr.command, arg,
@@ -467,16 +468,16 @@
* specifying the number is optional.
*/
if (!hasArg && nbbanks(type) != 1)
- errx(1, "%s(%u): Didn't specify a bank number",
+ errx(1, "%s(%" PRIu32 "): Didn't specify a bank number",
linkerScriptName, lineNo);
else if (!hasArg)
arg = bankranges[type][0];
else if (arg < bankranges[type][0])
- errx(1, "%s(%u): specified bank number is too low (%u < %u)",
+ errx(1, "%s(%" PRIu32 "): specified bank number is too low (%" PRIu32 " < %" PRIu32 ")",
linkerScriptName, lineNo,
arg, bankranges[type][0]);
else if (arg > bankranges[type][1])
- errx(1, "%s(%u): specified bank number is too high (%u > %u)",
+ errx(1, "%s(%" PRIu32 "): specified bank number is too high (%" PRIu32 " > %" PRIu32 ")",
linkerScriptName, lineNo,
arg, bankranges[type][1]);
bank = arg;
@@ -496,7 +497,7 @@
case PARSER_INCLUDE:
if (token->type != TOKEN_STRING)
- errx(1, "%s(%u): Expected a file name after INCLUDE",
+ errx(1, "%s(%" PRIu32 "): Expected a file name after INCLUDE",
linkerScriptName, lineNo);
/* Switch to that file */
@@ -516,7 +517,7 @@
return NULL;
parserState = PARSER_LINEEND;
} else if (token->type != TOKEN_NEWLINE)
- errx(1, "%s(%u): Unexpected %s at the end of the line",
+ errx(1, "%s(%" PRIu32 "): Unexpected %s at the end of the line",
linkerScriptName, lineNo,
tokenTypes[token->type]);
break;
--- a/src/link/section.c
+++ b/src/link/section.c
@@ -6,6 +6,7 @@
* SPDX-License-Identifier: MIT
*/
+#include <inttypes.h>
#include <stdbool.h>
#include "link/main.h"
@@ -45,11 +46,11 @@
if (other->isAddressFixed) {
if (target->isAddressFixed) {
if (target->org != other->org)
- errx(1, "Section \"%s\" is defined with conflicting addresses $%x and $%x",
+ errx(1, "Section \"%s\" is defined with conflicting addresses $%" PRIx16 " and $%" PRIx16,
other->name, target->org, other->org);
} else if (target->isAlignFixed) {
if ((other->org - target->alignOfs) & target->alignMask)
- errx(1, "Section \"%s\" is defined with conflicting %u-byte alignment (offset %u) and address $%x",
+ errx(1, "Section \"%s\" is defined with conflicting %" PRIu16 "-byte alignment (offset %" PRIu16 ") and address $%" PRIx16,
other->name, target->alignMask + 1,
target->alignOfs, other->org);
}
@@ -58,13 +59,13 @@
} else if (other->isAlignFixed) {
if (target->isAddressFixed) {
if ((target->org - other->alignOfs) & other->alignMask)
- errx(1, "Section \"%s\" is defined with conflicting address $%x and %u-byte alignment (offset %u)",
+ errx(1, "Section \"%s\" is defined with conflicting address $%" PRIx16 " and %" PRIu16 "-byte alignment (offset %" PRIu16 ")",
other->name, target->org,
other->alignMask + 1, other->alignOfs);
} else if (target->isAlignFixed
&& (other->alignMask & target->alignOfs)
!= (target->alignMask & other->alignOfs)) {
- errx(1, "Section \"%s\" is defined with conflicting %u-byte alignment (offset %u) and %u-byte alignment (offset %u)",
+ errx(1, "Section \"%s\" is defined with conflicting %" PRIu16 "-byte alignment (offset %" PRIu16 ") and %" PRIu16 "-byte alignment (offset %" PRIu16 ")",
other->name, target->alignMask + 1,
target->alignOfs, other->alignMask + 1,
other->alignOfs);
@@ -80,7 +81,7 @@
target->isBankFixed = true;
target->bank = other->bank;
} else if (target->bank != other->bank) {
- errx(1, "Section \"%s\" is defined with conflicting banks %u and %u",
+ errx(1, "Section \"%s\" is defined with conflicting banks %" PRIu32 " and %" PRIu32,
other->name, target->bank, other->bank);
}
}
@@ -171,7 +172,7 @@
/* Too large an alignment may not be satisfiable */
if (section->isAlignFixed
&& (section->alignMask & startaddr[section->type]))
- fail("%s: %s sections cannot be aligned to $%x bytes",
+ fail("%s: %s sections cannot be aligned to $%" PRIx16 " bytes",
section->name, typeNames[section->type],
section->alignMask + 1);
@@ -181,13 +182,13 @@
if (section->isBankFixed && section->bank < minbank
&& section->bank > maxbank)
fail(minbank == maxbank
- ? "Cannot place section \"%s\" in bank %d, it must be %d"
- : "Cannot place section \"%s\" in bank %d, it must be between %d and %d",
+ ? "Cannot place section \"%s\" in bank %" PRIu32 ", it must be %" PRIu32
+ : "Cannot place section \"%s\" in bank %" PRIu32 ", it must be between %" PRIu32 " and %" PRIu32,
section->name, section->bank, minbank, maxbank);
/* Check if section has a chance to be placed */
if (section->size > maxsize[section->type])
- fail("Section \"%s\" is bigger than the max size for that type: %#x > %#x",
+ fail("Section \"%s\" is bigger than the max size for that type: %#" PRIx16 " > %#" PRIx16,
section->name, section->size, maxsize[section->type]);
/* Translate loose constraints to strong ones when they're equivalent */
@@ -218,12 +219,12 @@
/* Ensure the target address is valid */
if (section->org < startaddr[section->type]
|| section->org > endaddr(section->type))
- fail("Section \"%s\"'s fixed address %#x is outside of range [%#x; %#x]",
+ fail("Section \"%s\"'s fixed address %#" PRIx16 " is outside of range [%#" PRIx16 "; %#" PRIx16 "]",
section->name, section->org,
startaddr[section->type], endaddr(section->type));
if (section->org + section->size > endaddr(section->type) + 1)
- fail("Section \"%s\"'s end address %#x is greater than last address %#x",
+ fail("Section \"%s\"'s end address %#" PRIx16 " is greater than last address %#" PRIx16,
section->name, section->org + section->size,
endaddr(section->type) + 1);
}
--- a/src/link/symbol.c
+++ b/src/link/symbol.c
@@ -6,6 +6,7 @@
* SPDX-License-Identifier: MIT
*/
+#include <inttypes.h>
#include <stdbool.h>
#include "link/symbol.h"
@@ -40,7 +41,7 @@
struct Symbol *other = hash_GetElement(symbols, symbol->name);
if (other)
- errx(1, "\"%s\" both in %s from %s(%d) and in %s from %s(%d)",
+ errx(1, "\"%s\" both in %s from %s(%" PRId32 ") and in %s from %s(%" PRId32 ")",
symbol->name,
symbol->objFileName, symbol->fileName, symbol->lineNo,
other->objFileName, other->fileName, other->lineNo);
--- a/test/asm/pc-bank.err
+++ b/test/asm/pc-bank.err
@@ -1,5 +1,5 @@
ERROR: pc-bank.asm(2):
- Source address $2a00 not in $FF00 to $FFFF
+ Source address $2a00 not between $FF00 to $FFFF
ERROR: pc-bank.asm(11):
Expected constant expression: Current section's bank is not known
error: Assembly aborted (2 errors)!
--- a/test/link/section-union/no-room.out
+++ b/test/link/section-union/no-room.out
@@ -1,2 +1,2 @@
-error: Unable to place "test" (WRAMX section) in bank $02 with align mask ffffffc0
+error: Unable to place "test" (WRAMX section) in bank $02 with align mask ffc0
---