[PATCH 1/2] Fix printf format strings on 64-bit architectures

25 views
Skip to first unread message

Pekka Enberg

unread,
Oct 13, 2011, 9:35:14 AM10/13/11
to lib...@googlegroups.com, Pekka Enberg, Gianluca Guida
Cc: Gianluca Guida <glg...@me.com>
Signed-off-by: Pekka Enberg <pen...@kernel.org>
---
CMakeLists.txt | 2 +-
arch/6502/6502_disasm.cpp | 4 +++-
arch/6502/6502_translate.cpp | 4 +++-
arch/arm/arm_translate.cpp | 4 +++-
arch/fapra/fapra_translate.cpp | 6 ++++--
arch/m68k/m68k_disasm.cpp | 26 ++++++++++++++------------
arch/m88k/m88k_translate.cpp | 4 +++-
arch/mips/mips_translate.cpp | 6 ++++--
libcpu/basicblock.cpp | 4 +++-
libcpu/idbg.cpp | 25 +++++++++++++------------
libcpu/interface.cpp | 11 ++++++-----
11 files changed, 57 insertions(+), 39 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 721cf2f..bc2353c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -72,7 +72,7 @@ ENDMACRO(TARGET_LINK_LLVM)
# Add extra warnings for gcc/g++.
#
IF(CMAKE_COMPILER_IS_GNUCXX)
- ADD_DEFINITIONS(-W -Wall -Wextra -Wno-unused -Werror)
+ ADD_DEFINITIONS(-W -Wall -Wextra -Wno-unused -Werror -D__STDC_FORMAT_MACROS=1)
ENDIF()

FOREACH(ARCHITECTURE ${GUEST_ARCHITECTURES})
diff --git a/arch/6502/6502_disasm.cpp b/arch/6502/6502_disasm.cpp
index e951752..071fa1d 100644
--- a/arch/6502/6502_disasm.cpp
+++ b/arch/6502/6502_disasm.cpp
@@ -10,6 +10,8 @@
#include "libcpu.h"
#include "6502_isa.h"

+#include <inttypes.h>
+
static const char *addmode_template[] = {
/*[ADDMODE_ABS]*/ "$%04X",
/*[ADDMODE_ABSX]*/ "$%04X,X",
@@ -98,7 +100,7 @@ arch_6502_disasm_instr(cpu_t *cpu, addr_t pc, char *line, unsigned int max_line)
int addmode = get_addmode(opcode);

if (addmode == ADDMODE_BRA) {
- snprintf(line2, sizeof(line2), "$%02llX", pc+2 + (int8_t)cpu->RAM[pc+1]);
+ snprintf(line2, sizeof(line2), "$%02" PRIX64, pc+2 + (int8_t)cpu->RAM[pc+1]);
} else {
switch (get_length(addmode)) {
case 1:
diff --git a/arch/6502/6502_translate.cpp b/arch/6502/6502_translate.cpp
index cad5c92..5bc8b85 100644
--- a/arch/6502/6502_translate.cpp
+++ b/arch/6502/6502_translate.cpp
@@ -11,6 +11,8 @@
#include "6502_isa.h"
#include "frontend.h"

+#include <inttypes.h>
+
#define A 0
#define X 1
#define Y 2
@@ -105,7 +107,7 @@ arch_6502_trap(cpu_t *cpu, addr_t pc, BasicBlock *bb)
Value *
arch_6502_translate_cond(cpu_t *cpu, addr_t pc, BasicBlock *bb) {
uint8_t opcode = cpu->RAM[pc];
-LOG("%s:%d pc=%llx opcode=%x\n", __func__, __LINE__, pc, opcode);
+LOG("%s:%d pc=%" PRIx64 " opcode=%x\n", __func__, __LINE__, pc, opcode);

switch (get_instr(opcode)) {
case INSTR_BEQ: /* Z */ return CC_EQ;
diff --git a/arch/arm/arm_translate.cpp b/arch/arm/arm_translate.cpp
index 52d9a10..141aa1e 100644
--- a/arch/arm/arm_translate.cpp
+++ b/arch/arm/arm_translate.cpp
@@ -13,6 +13,8 @@
#include "arm_types.h"
#include "tag.h"

+#include <inttypes.h>
+
using namespace llvm;

#define ptr_N ((ccarm_t*)cpu->feptr)->ptr_N
@@ -186,7 +188,7 @@ setsub(cpu_t *cpu, Value *op1, Value *op2, BasicBlock *bb)
#define LINK LET32(14, CONST((uint64_t)(sint64_t)(sint32_t)pc+8))

int arch_arm_translate_instr(cpu_t *cpu, addr_t pc, BasicBlock *bb) {
-LOG("%s:%d pc=%llx\n", __func__, __LINE__, pc);
+LOG("%s:%d pc=%" PRIx64 "\n", __func__, __LINE__, pc);
uint32_t instr = *(uint32_t*)&cpu->RAM[pc];

// int cond = instr >> 28;
diff --git a/arch/fapra/fapra_translate.cpp b/arch/fapra/fapra_translate.cpp
index 00bdc44..f6d6730 100644
--- a/arch/fapra/fapra_translate.cpp
+++ b/arch/fapra/fapra_translate.cpp
@@ -7,6 +7,8 @@
#include "frontend.h"
#include "fapra_internal.h"

+#include <inttypes.h>
+
using namespace llvm;

//////////////////////////////////////////////////////////////////////
@@ -155,7 +157,7 @@ arch_fapra_translate_cond(cpu_t *cpu, addr_t pc, BasicBlock *bb)
{
uint32_t instr = INSTR(pc);

- LOG("cond (%08llx) %08x\n", pc, instr);
+ LOG("cond (%08" PRIx64 ") %08x\n", pc, instr);

switch (opc(instr)) {
default:
@@ -179,7 +181,7 @@ arch_fapra_translate_instr(cpu_t *cpu, addr_t pc, BasicBlock *bb)

uint32_t instr = INSTR(pc);

- LOG("translating (%08llx) %08x\n", pc, instr);
+ LOG("translating (%08" PRIx64 ") %08x\n", pc, instr);

switch (opc(instr)) {
case PERM:
diff --git a/arch/m68k/m68k_disasm.cpp b/arch/m68k/m68k_disasm.cpp
index 33c0be6..fb4cac1 100644
--- a/arch/m68k/m68k_disasm.cpp
+++ b/arch/m68k/m68k_disasm.cpp
@@ -4,6 +4,8 @@
#include "libcpu.h"
#include "m68k_isa.h"

+#include <inttypes.h>
+
enum {
TASK_DIS,
TASK_REC,
@@ -132,7 +134,7 @@ decodeimm(cpu_t *cpu, addr_t pc, int size) {
case SIZE_L:
return RAM32(pc+2);
}
- printf("decodeimm pc=%llx", pc);
+ printf("decodeimm pc=%" PRIx64, pc);
exit(1);
return 0;
}
@@ -200,8 +202,8 @@ decodemodreg(int task, cpu_t *cpu, addr_t pc, int size, int mod, int reg, char *
return;
}
if (reg == 2) {
- DIS snprintf(op1, maxop, "0x%08llx", RAM16(pc+2)+pc+2);
- REC snprintf(op1, maxop, "RAM%d,0x%llx", sizenum[size], RAM16(pc+2)+pc+2);
+ DIS snprintf(op1, maxop, "0x%08" PRIx64, RAM16(pc+2)+pc+2);
+ REC snprintf(op1, maxop, "RAM%d,0x%" PRIx64, sizenum[size], RAM16(pc+2)+pc+2);
REC *rm=IS_MEM;
return;
}
@@ -343,19 +345,19 @@ disreclen(int task, cpu_t *cpu, addr_t pc, char *line, unsigned int max_line, ad
decodemodreg(task, cpu, pc, SIZE_L, MOD0, REG0, op1, sizeof(op1), &attr, &rm);
DIS snprintf(line, max_line, "jsr %s", op1);
len = 2+lengthmodreg(MOD0, REG0, 0);
- REC snprintf(line, max_line, "call (READ_%s,0x%08llx);", op1, pc+len);
+ REC snprintf(line, max_line, "call (READ_%s,0x%08" PRIx64 ");", op1, pc+len);
break;
} else if (bits(opcode,0,7) == 0x56) { /* LINKW */ // 68030?
decodemodreg(task, cpu, pc, SIZE_L, MOD0, REG0, op1, sizeof(op1), &attr, &rm);
DIS snprintf(line, max_line, "linkw %s, %04x", op1, decodeimm(cpu, pc, SIZE_W));
len = 4+lengthmodreg(MOD0, REG0, 0);
- REC snprintf(line, max_line, "linkw (READ_%s,0x%08llx);", op1, pc+len);
+ REC snprintf(line, max_line, "linkw (READ_%s,0x%08" PRIx64 ");", op1, pc+len);
break;
} else if (bits(opcode,0,7) == 0x5e) { /* UNLK */ // 68030?
decodemodreg(task, cpu, pc, SIZE_L, MOD0, REG0, op1, sizeof(op1), &attr, &rm);
DIS snprintf(line, max_line, "unlk %s", op1);
len = 2+lengthmodreg(MOD0, REG0, 0);
- REC snprintf(line, max_line, "linkw (READ_%s,0x%08llx);", op1, pc+len);
+ REC snprintf(line, max_line, "linkw (READ_%s,0x%08" PRIx64 ");", op1, pc+len);
break;
} else {
// DIS snprintf(line, max_line, "??? opcode %04x (%x)", opcode, bits(opcode,12,15));
@@ -394,13 +396,13 @@ disreclen(int task, cpu_t *cpu, addr_t pc, char *line, unsigned int max_line, ad
case 5:
if (bits(opcode,6,7)==3) {
if (MOD0==1) { /* DBcc */
- DIS snprintf(line, max_line, "db%s d%d, 0x%08llx", condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
- REC snprintf(line, max_line, "db%s (d[%d], l%llX);", condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
+ DIS snprintf(line, max_line, "db%s d%d, 0x%08" PRIx64, condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
+ REC snprintf(line, max_line, "db%s (d[%d], l%" PRIX64 ");", condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
len = 4;
}
else if (MOD0==7) { /* TRAPcc */
- DIS snprintf(line, max_line, "trap%s d%d, 0x%08llx", condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
- REC snprintf(line, max_line, "trap%s (d[%d], l%llX);", condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
+ DIS snprintf(line, max_line, "trap%s d%d, 0x%08" PRIx64, condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
+ REC snprintf(line, max_line, "trap%s (d[%d], l%" PRIX64 ");", condstr_db[CONDITION], REG0, pc+2+RAM16(pc+2));
// TODO: decode OPMODE etc.! (p. 8.16 68kPM), optional word or long word
len = 2;
}
@@ -418,8 +420,8 @@ disreclen(int task, cpu_t *cpu, addr_t pc, char *line, unsigned int max_line, ad
break;
case 6: /* Bcc */
disp = arch_disasm_get_disp(cpu, pc, opcode);
- DIS snprintf(line, max_line, "b%s 0x%08llx", condstr[CONDITION], pc+2+disp);
- DIS snprintf(line, max_line, "b%s (l%llX);", condstr[CONDITION], pc+2+disp);
+ DIS snprintf(line, max_line, "b%s 0x%08" PRIx64, condstr[CONDITION], pc+2+disp);
+ DIS snprintf(line, max_line, "b%s (l%" PRIX64 ");", condstr[CONDITION], pc+2+disp);
len = lengthdisp(cpu, pc, opcode);
break;
case 7:
diff --git a/arch/m88k/m88k_translate.cpp b/arch/m88k/m88k_translate.cpp
index 665c1c8..ccb3086 100644
--- a/arch/m88k/m88k_translate.cpp
+++ b/arch/m88k/m88k_translate.cpp
@@ -6,6 +6,8 @@
#include "m88k_internal.h"
#include "m88k_insn.h"

+#include <inttypes.h>
+
using namespace llvm;

#define ptr_PSR ptr_xr[0]
@@ -737,7 +739,7 @@ arch_m88k_xmem(cpu_t *cpu, bool byte, m88k_reg_t rd, Value *src1, Value *src2, B
int
arch_m88k_translate_instr(cpu_t *cpu, addr_t pc, BasicBlock *bb)
{
-#define BAD do { printf("%s:%u: BAD INSTRUCTION AT PC %llx\n", __func__, __LINE__, pc); exit(1); } while(0)
+#define BAD do { printf("%s:%u: BAD INSTRUCTION AT PC %" PRIu64 "\n", __func__, __LINE__, pc); exit(1); } while(0)
#define LOGX LOG("%s:%d PC=%llx\n", __func__, __LINE__, pc);
m88k_insn instr = INSTR(pc);
m88k_opcode_t opc = instr.opcode();
diff --git a/arch/mips/mips_translate.cpp b/arch/mips/mips_translate.cpp
index 6e33817..2c2f3a3 100644
--- a/arch/mips/mips_translate.cpp
+++ b/arch/mips/mips_translate.cpp
@@ -7,6 +7,8 @@
#include "frontend.h"
#include "mips_internal.h"

+#include <inttypes.h>
+
using namespace llvm;

//////////////////////////////////////////////////////////////////////
@@ -288,7 +290,7 @@ arch_mips_translate_cond(cpu_t *cpu, addr_t pc, BasicBlock *bb)
{
uint32_t instr = INSTR(pc);

- LOG("cond (%08llx) %08x\n", pc, instr);
+ LOG("cond (%08" PRIx64 ") %08x\n", pc, instr);

switch(instr >> 26) {
case 0x01: /* INCPU_REGIMM */
@@ -328,7 +330,7 @@ arch_mips_translate_instr(cpu_t *cpu, addr_t pc, BasicBlock *bb)

uint32_t instr = INSTR(pc);

- LOG("translating (%08llx) %08x\n", pc, instr);
+ LOG("translating (%08" PRIx64 ") %08x\n", pc, instr);

switch(instr >> 26) {
case 0x00: /* INCPU_SPECIAL */
diff --git a/libcpu/basicblock.cpp b/libcpu/basicblock.cpp
index e765b06..2436e1c 100644
--- a/libcpu/basicblock.cpp
+++ b/libcpu/basicblock.cpp
@@ -13,6 +13,8 @@
#include "basicblock.h"
#include "tag.h"

+#include <inttypes.h>
+
bool
is_start_of_basicblock(cpu_t *cpu, addr_t a)
{
@@ -63,7 +65,7 @@ lookup_basicblock(cpu_t *cpu, Function* f, addr_t pc, BasicBlock *bb_ret, uint8_
if (i != bb_addr.end())
return i->second;

- LOG("basic block %c%08llx not found in function %p - creating return basic block!\n", bb_type, pc, f);
+ LOG("basic block %c%08" PRIx64 " not found in function %p - creating return basic block!\n", bb_type, pc, f);
BasicBlock *new_bb = create_basicblock(cpu, pc, cpu->cur_func, BB_TYPE_EXTERNAL);
emit_store_pc_return(cpu, new_bb, pc, bb_ret);

diff --git a/libcpu/idbg.cpp b/libcpu/idbg.cpp
index 4792973..a4255ff 100644
--- a/libcpu/idbg.cpp
+++ b/libcpu/idbg.cpp
@@ -49,6 +49,7 @@
#include <assert.h>
#include <ctype.h>
#include <stddef.h>
+#include <inttypes.h>

#ifdef USE_READLINE
#define Function FunctionX // XXX clash in readline.
@@ -143,11 +144,11 @@ idbg_print_int_value(uint64_t v, unsigned bits, int align, unsigned mode)
case M_SIGNED:
if (bits != 64 && (v & (1ULL << (bits - 1))) != 0)
v |= -1ULL << bits;
- fprintf(stdout, "%lld", (int64_t)v);
+ fprintf(stdout, "%" PRId64 , (int64_t)v);
break;

case M_UNSIGNED:
- fprintf(stdout, "%llu", v);
+ fprintf(stdout, "%" PRIu64, v);
break;

case M_BIN:
@@ -158,18 +159,18 @@ idbg_print_int_value(uint64_t v, unsigned bits, int align, unsigned mode)
if (align < 0)
align = ALIGN8(bits) / 3;
if (align == 0)
- fprintf(stdout, "0%llo", v);
+ fprintf(stdout, "0%" PRIo64, v);
else
- fprintf(stdout, "0%.*llo", align, v);
+ fprintf(stdout, "0%.*" PRIo64, align, v);
break;

case M_HEX:
if (align < 0)
align = ALIGN8(bits) >> 2;
if (align == 0)
- fprintf(stdout, "0x%llx", v);
+ fprintf(stdout, "0x%" PRIx64, v);
else
- fprintf(stdout, "0x%.*llx", align, v);
+ fprintf(stdout, "0x%.*" PRIx64, align, v);
break;
}
}
@@ -186,7 +187,7 @@ idbg_print_qword(uint64_t const *v, unsigned mode, int)
idbg_print_bits(v[0], 64);
idbg_print_bits(v[1], 64);
} else
- fprintf(stdout, "0x%016llx%016llx", v[0], v[1]);
+ fprintf(stdout, "0x%016" PRIx64 "%016" PRIx64 "", v[0], v[1]);
}

static void
@@ -252,7 +253,7 @@ static inline void
idbg_print_float64(uint64_t v, bool hex)
{
if (hex)
- fprintf(stdout, "%f [%016llx]", *(double *)&v, v);
+ fprintf(stdout, "%f [%016" PRIx64 "]", *(double *)&v, v);
else
fprintf(stdout, "%f", *(double *)&v);
}
@@ -260,13 +261,13 @@ idbg_print_float64(uint64_t v, bool hex)
static inline void
idbg_print_float80(uint64_t const *v, bool hex)
{
- fprintf(stdout, "[%04llx%016llx]", v[0], v[1]);
+ fprintf(stdout, "[%04" PRIx64 "%016" PRIx64 "]", v[0], v[1]);
}

static inline void
idbg_print_float128(uint64_t const *v, bool hex)
{
- fprintf(stdout, "[%016llx%016llx]", v[0], v[1]);
+ fprintf(stdout, "[%016" PRIx64 "%016" PRIx64 "]", v[0], v[1]);
}

//////////////////////////////////////////////////////////////////////////////
@@ -719,7 +720,7 @@ idbg_diff_registers(idbg_t *ctx)
idbg_print_address(ctx, ctx->saved_regs[n]);
fprintf(stdout, " -> ");
idbg_print_address(ctx, v);
- fprintf(stdout, " (delta %lld)\n",
+ fprintf(stdout, " (delta %" PRId64 ")\n",
v - ctx->saved_regs[n]);
}
}
@@ -833,7 +834,7 @@ idbg_examine(idbg_t *ctx, addr_t address, size_t count,

ptrdiff_t bytes = idbg_examine_one(ctx, address, format, mode);
if (bytes < 0) {
- fprintf(stderr, "\nIDBG ERROR: Cannot access address %llx.\n",
+ fprintf(stderr, "\nIDBG ERROR: Cannot access address %" PRIx64 ".\n",
address);
break;
}
diff --git a/libcpu/interface.cpp b/libcpu/interface.cpp
index 1f6c3db..cdb4054 100644
--- a/libcpu/interface.cpp
+++ b/libcpu/interface.cpp
@@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <inttypes.h>

#include "llvm/Analysis/Verifier.h"
#include "llvm/ExecutionEngine/JIT.h"
@@ -376,7 +377,7 @@ cpu_run(cpu_t *cpu, debug_function_t debug_function)
}
}
if (!success) {
- LOG("{%llx}", pc);
+ LOG("{%" PRIx64 "}", pc);
cpu_tag(cpu, pc);
do_translate = true;
}
@@ -402,9 +403,9 @@ cpu_flush(cpu_t *cpu)
void
cpu_print_statistics(cpu_t *cpu)
{
- printf("tag = %8lld\n", cpu->timer_total[TIMER_TAG]);
- printf("fe = %8lld\n", cpu->timer_total[TIMER_FE]);
- printf("be = %8lld\n", cpu->timer_total[TIMER_BE]);
- printf("run = %8lld\n", cpu->timer_total[TIMER_RUN]);
+ printf("tag = %8" PRId64 "\n", cpu->timer_total[TIMER_TAG]);
+ printf("fe = %8" PRId64 "\n", cpu->timer_total[TIMER_FE]);
+ printf("be = %8" PRId64 "\n", cpu->timer_total[TIMER_BE]);
+ printf("run = %8" PRId64 "\n", cpu->timer_total[TIMER_RUN]);
}
//printf("%s:%d\n", __func__, __LINE__);
--
1.7.6.4

Pekka Enberg

unread,
Oct 13, 2011, 9:35:15 AM10/13/11
to lib...@googlegroups.com, Pekka Enberg, Gianluca Guida
There's no llvm/ModuleProvider.h include in Fedora 15 LLVM package so kill the
include to fix broken build.

Cc: Gianluca Guida <glg...@me.com>
Signed-off-by: Pekka Enberg <pen...@kernel.org>
---

libcpu/interface.cpp | 1 -
libcpu/optimize.cpp | 1 -
2 files changed, 0 insertions(+), 2 deletions(-)

diff --git a/libcpu/interface.cpp b/libcpu/interface.cpp
index cdb4054..29d25d9 100644
--- a/libcpu/interface.cpp
+++ b/libcpu/interface.cpp
@@ -12,7 +12,6 @@
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/Module.h"
-#include "llvm/ModuleProvider.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetSelect.h"

diff --git a/libcpu/optimize.cpp b/libcpu/optimize.cpp
index b027aac..1bbf937 100644
--- a/libcpu/optimize.cpp
+++ b/libcpu/optimize.cpp
@@ -5,7 +5,6 @@
*/

#include "llvm/ExecutionEngine/ExecutionEngine.h"
-#include "llvm/ModuleProvider.h"
#include "llvm/Module.h"
#include "llvm/PassManager.h"
#include "llvm/Support/StandardPasses.h"
--
1.7.6.4

Gianluca Guida

unread,
Oct 13, 2011, 9:57:57 AM10/13/11
to lib...@googlegroups.com, Pekka Enberg

On Oct 13, 2011, at 3:35 PM, Pekka Enberg wrote:

> Cc: Gianluca Guida <glg...@me.com>
> Signed-off-by: Pekka Enberg <pen...@kernel.org>
> ---
> CMakeLists.txt | 2 +-
> arch/6502/6502_disasm.cpp | 4 +++-
> arch/6502/6502_translate.cpp | 4 +++-
> arch/arm/arm_translate.cpp | 4 +++-
> arch/fapra/fapra_translate.cpp | 6 ++++--
> arch/m68k/m68k_disasm.cpp | 26 ++++++++++++++------------
> arch/m88k/m88k_translate.cpp | 4 +++-
> arch/mips/mips_translate.cpp | 6 ++++--
> libcpu/basicblock.cpp | 4 +++-
> libcpu/idbg.cpp | 25 +++++++++++++------------
> libcpu/interface.cpp | 11 ++++++-----
> 11 files changed, 57 insertions(+), 39 deletions(-)

Much better! :)
Perhaps systems not defining uint64_t will have problems with PRI?64 as well, but we can define that later in case we encounter problems.

Gianluca

Gianluca Guida

unread,
Oct 13, 2011, 9:59:51 AM10/13/11
to Pekka Enberg, lib...@googlegroups.com

On Oct 13, 2011, at 3:35 PM, Pekka Enberg wrote:

> There's no llvm/ModuleProvider.h include in Fedora 15 LLVM package so kill the
> include to fix broken build.

ACK.

Gianluca

Reply all
Reply to author
Forward
0 new messages