Merge pull request #520 from JL2210/inttypes-stdint

Use inttypes for stdint types
This commit is contained in:
Eldred Habert
2020-05-07 17:31:06 +02:00
committed by GitHub
22 changed files with 180 additions and 150 deletions

View File

@@ -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 @@ size_t symvaluetostring(char *dest, size_t maxLength, char *symName,
strncpy(dest, write_ptr, maxLength + 1);
} else {
fullLength = snprintf(dest, maxLength + 1,
mode ? mode : "$%X",
mode ? mode : "$%" PRIX32,
value);
}

View File

@@ -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 @@ static void pushcontext(void)
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 @@ void fstk_Dump(void)
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 @@ void fstk_DumpToStr(char *buf, size_t buflen)
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 @@ void fstk_DumpToStr(char *buf, size_t buflen)
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));

View File

@@ -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 yyunputstr(const char *s)
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 @@ uint32_t lex_FloatAlloc(const struct sLexFloat *token)
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 @@ size_t yylex_ReadBracketedSymbol(char *dest, size_t index)
* so it's handled differently
*/
static const char * const formatSpecifiers[] = {
"", "%x", "%X", "%d"
"", "%" PRIx32, "%" PRIX32, "%" PRId32
};
/* Prevent reading out of bounds! */
const char *designatedMode;

View File

@@ -1,5 +1,6 @@
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -82,7 +83,7 @@ void macro_SetUniqueID(uint32_t id)
uniqueIDPtr = NULL;
} else {
/* The buffer is guaranteed to be the correct size */
sprintf(uniqueIDBuf, "_%u", id);
sprintf(uniqueIDBuf, "_%" PRIu32, id);
uniqueIDPtr = uniqueIDBuf;
}
}

View File

@@ -167,7 +167,7 @@ void opt_Parse(char *s)
/* 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 @@ int main(int argc, char *argv[])
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 @@ int main(int argc, char *argv[])
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

View File

@@ -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_DefinePI(void)
*/
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);
}
/*

View File

@@ -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 @@ static uint8_t *reserveSpace(struct Expression *expr, uint32_t size)
* 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 @@ void rpn_CheckHRAM(struct Expression *expr, const struct Expression *src)
/* 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 @@ void rpn_CheckRST(struct Expression *expr, const struct Expression *src)
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 @@ static int32_t shift(int32_t shiftee, int32_t amount)
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 @@ static int32_t shift(int32_t shiftee, int32_t amount)
// 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 @@ static int32_t shift(int32_t shiftee, int32_t amount)
* 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,18 +373,18 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
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);
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 @@ void rpn_BinaryOp(enum RPNCommand op, struct Expression *expr,
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)) {

View File

@@ -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 @@ static void reserveSpace(uint32_t delta_size)
* 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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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,14 +184,14 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 */
} 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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 @@ static struct Section *getSection(char const *pzName, enum SectionType type,
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 @@ void sect_AlignPC(uint8_t alignment, uint16_t offset)
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 @@ void out_PCRelByte(struct Expression *expr)
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_BinaryFile(char const *s)
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 @@ void out_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
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);
}
}

View File

@@ -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 @@ static struct Symbol *createNonrelocSymbol(char const *symbolName)
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 @@ struct Symbol *sym_AddSet(char const *symName, int32_t value)
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 @@ struct Symbol *sym_AddReloc(char const *symName)
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 */