Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm2c: ensure force read constraints compile for clang on mips #2274

Merged
merged 1 commit into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/prebuilt/wasm2c_atomicops_source_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ R"w2c_template( TRAP(UNALIGNED); \
R"w2c_template( }
)w2c_template"
R"w2c_template(
#define DEFINE_ATOMIC_LOAD(name, t1, t2, t3) \
#define DEFINE_ATOMIC_LOAD(name, t1, t2, t3, force_read) \
)w2c_template"
R"w2c_template( static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
)w2c_template"
Expand All @@ -239,26 +239,26 @@ R"w2c_template( wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \
)w2c_template"
R"w2c_template( result = atomic_load_##t1(&mem->data[addr]); \
)w2c_template"
R"w2c_template( wasm_asm("" ::"r"(result)); \
R"w2c_template( force_read(result); \
)w2c_template"
R"w2c_template( return (t3)(t2)result; \
)w2c_template"
R"w2c_template( }
)w2c_template"
R"w2c_template(
DEFINE_ATOMIC_LOAD(i32_atomic_load, u32, u32, u32)
DEFINE_ATOMIC_LOAD(i32_atomic_load, u32, u32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load, u64, u64, u64)
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load, u64, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_ATOMIC_LOAD(i32_atomic_load8_u, u8, u32, u32)
R"w2c_template(DEFINE_ATOMIC_LOAD(i32_atomic_load8_u, u8, u32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load8_u, u8, u64, u64)
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load8_u, u8, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_ATOMIC_LOAD(i32_atomic_load16_u, u16, u32, u32)
R"w2c_template(DEFINE_ATOMIC_LOAD(i32_atomic_load16_u, u16, u32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load16_u, u16, u64, u64)
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load16_u, u16, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load32_u, u32, u64, u64)
R"w2c_template(DEFINE_ATOMIC_LOAD(i64_atomic_load32_u, u32, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(
#define DEFINE_ATOMIC_STORE(name, t1, t2) \
Expand Down
4 changes: 2 additions & 2 deletions src/prebuilt/wasm2c_simd_source_declarations.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const char* s_simd_source_declarations = R"w2c_template(#ifdef __x86_64__
const char* s_simd_source_declarations = R"w2c_template(#if defined(__GNUC__) && defined(__x86_64__)
)w2c_template"
R"w2c_template(#define SIMD_FORCE_READ(var) wasm_asm("" ::"x"(var));
R"w2c_template(#define SIMD_FORCE_READ(var) __asm__("" ::"x"(var));
)w2c_template"
R"w2c_template(#else
)w2c_template"
Expand Down
56 changes: 36 additions & 20 deletions src/prebuilt/wasm2c_source_declarations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,27 @@ R"w2c_template(#endif
R"w2c_template(
#ifdef __GNUC__
)w2c_template"
R"w2c_template(#define wasm_asm __asm__
R"w2c_template(#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
)w2c_template"
R"w2c_template(// Clang on Mips requires "f" constraints on floats
)w2c_template"
R"w2c_template(#if defined(__clang__) && \
)w2c_template"
R"w2c_template( (defined(mips) || defined(__mips__) || defined(__mips))
)w2c_template"
R"w2c_template(#define FORCE_READ_FLOAT(var) __asm__("" ::"f"(var));
)w2c_template"
R"w2c_template(#else
)w2c_template"
R"w2c_template(#define FORCE_READ_FLOAT(var) __asm__("" ::"r"(var));
)w2c_template"
R"w2c_template(#endif
)w2c_template"
R"w2c_template(#else
)w2c_template"
R"w2c_template(#define wasm_asm(X)
R"w2c_template(#define FORCE_READ_INT(var)
)w2c_template"
R"w2c_template(#define FORCE_READ_FLOAT(var)
)w2c_template"
R"w2c_template(#endif
)w2c_template"
Expand Down Expand Up @@ -135,7 +151,7 @@ R"w2c_template( load_data(&(m.data[m.size - o - s]), i, s); \
)w2c_template"
R"w2c_template( } while (0)
)w2c_template"
R"w2c_template(#define DEFINE_LOAD(name, t1, t2, t3) \
R"w2c_template(#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
)w2c_template"
R"w2c_template( static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
)w2c_template"
Expand All @@ -147,7 +163,7 @@ R"w2c_template( wasm_rt_memcpy(&result, &mem->data[mem->size - addr - sizeof(
)w2c_template"
R"w2c_template( sizeof(t1)); \
)w2c_template"
R"w2c_template( wasm_asm("" ::"r"(result)); \
R"w2c_template( force_read(result); \
)w2c_template"
R"w2c_template( return (t3)(t2)result; \
)w2c_template"
Expand Down Expand Up @@ -192,7 +208,7 @@ R"w2c_template( load_data(&(m.data[o]), i, s); \
)w2c_template"
R"w2c_template( } while (0)
)w2c_template"
R"w2c_template(#define DEFINE_LOAD(name, t1, t2, t3) \
R"w2c_template(#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
)w2c_template"
R"w2c_template( static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
)w2c_template"
Expand All @@ -202,7 +218,7 @@ R"w2c_template( t1 result; \
)w2c_template"
R"w2c_template( wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \
)w2c_template"
R"w2c_template( wasm_asm("" ::"r"(result)); \
R"w2c_template( force_read(result); \
)w2c_template"
R"w2c_template( return (t3)(t2)result; \
)w2c_template"
Expand All @@ -224,33 +240,33 @@ R"w2c_template( }
R"w2c_template(#endif
)w2c_template"
R"w2c_template(
DEFINE_LOAD(i32_load, u32, u32, u32)
DEFINE_LOAD(i32_load, u32, u32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load, u64, u64, u64)
R"w2c_template(DEFINE_LOAD(i64_load, u64, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(f32_load, f32, f32, f32)
R"w2c_template(DEFINE_LOAD(f32_load, f32, f32, f32, FORCE_READ_FLOAT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(f64_load, f64, f64, f64)
R"w2c_template(DEFINE_LOAD(f64_load, f64, f64, f64, FORCE_READ_FLOAT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i32_load8_s, s8, s32, u32)
R"w2c_template(DEFINE_LOAD(i32_load8_s, s8, s32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load8_s, s8, s64, u64)
R"w2c_template(DEFINE_LOAD(i64_load8_s, s8, s64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i32_load8_u, u8, u32, u32)
R"w2c_template(DEFINE_LOAD(i32_load8_u, u8, u32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load8_u, u8, u64, u64)
R"w2c_template(DEFINE_LOAD(i64_load8_u, u8, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i32_load16_s, s16, s32, u32)
R"w2c_template(DEFINE_LOAD(i32_load16_s, s16, s32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load16_s, s16, s64, u64)
R"w2c_template(DEFINE_LOAD(i64_load16_s, s16, s64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i32_load16_u, u16, u32, u32)
R"w2c_template(DEFINE_LOAD(i32_load16_u, u16, u32, u32, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load16_u, u16, u64, u64)
R"w2c_template(DEFINE_LOAD(i64_load16_u, u16, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load32_s, s32, s64, u64)
R"w2c_template(DEFINE_LOAD(i64_load32_s, s32, s64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_LOAD(i64_load32_u, u32, u64, u64)
R"w2c_template(DEFINE_LOAD(i64_load32_u, u32, u64, u64, FORCE_READ_INT)
)w2c_template"
R"w2c_template(DEFINE_STORE(i32_store, u32, u32)
)w2c_template"
Expand Down
48 changes: 28 additions & 20 deletions src/template/wasm2c.declarations.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
#endif

#ifdef __GNUC__
#define wasm_asm __asm__
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
// Clang on Mips requires "f" constraints on floats
shravanrn marked this conversation as resolved.
Show resolved Hide resolved
#if defined(__clang__) && \
(defined(mips) || defined(__mips__) || defined(__mips))
#define FORCE_READ_FLOAT(var) __asm__("" ::"f"(var));
#else
#define wasm_asm(X)
#define FORCE_READ_FLOAT(var) __asm__("" ::"r"(var));
#endif
#else
#define FORCE_READ_INT(var)
#define FORCE_READ_FLOAT(var)
#endif

#if WABT_BIG_ENDIAN
Expand All @@ -72,13 +80,13 @@ static inline void load_data(void* dest, const void* src, size_t n) {
RANGE_CHECK((&m), m.size - o - s, s); \
load_data(&(m.data[m.size - o - s]), i, s); \
} while (0)
#define DEFINE_LOAD(name, t1, t2, t3) \
#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
MEMCHECK(mem, addr, t1); \
t1 result; \
wasm_rt_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], \
sizeof(t1)); \
wasm_asm("" ::"r"(result)); \
force_read(result); \
return (t3)(t2)result; \
}

Expand All @@ -101,12 +109,12 @@ static inline void load_data(void* dest, const void* src, size_t n) {
RANGE_CHECK((&m), o, s); \
load_data(&(m.data[o]), i, s); \
} while (0)
#define DEFINE_LOAD(name, t1, t2, t3) \
#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
MEMCHECK(mem, addr, t1); \
t1 result; \
wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \
wasm_asm("" ::"r"(result)); \
force_read(result); \
return (t3)(t2)result; \
}

Expand All @@ -118,20 +126,20 @@ static inline void load_data(void* dest, const void* src, size_t n) {
}
#endif

DEFINE_LOAD(i32_load, u32, u32, u32)
DEFINE_LOAD(i64_load, u64, u64, u64)
DEFINE_LOAD(f32_load, f32, f32, f32)
DEFINE_LOAD(f64_load, f64, f64, f64)
DEFINE_LOAD(i32_load8_s, s8, s32, u32)
DEFINE_LOAD(i64_load8_s, s8, s64, u64)
DEFINE_LOAD(i32_load8_u, u8, u32, u32)
DEFINE_LOAD(i64_load8_u, u8, u64, u64)
DEFINE_LOAD(i32_load16_s, s16, s32, u32)
DEFINE_LOAD(i64_load16_s, s16, s64, u64)
DEFINE_LOAD(i32_load16_u, u16, u32, u32)
DEFINE_LOAD(i64_load16_u, u16, u64, u64)
DEFINE_LOAD(i64_load32_s, s32, s64, u64)
DEFINE_LOAD(i64_load32_u, u32, u64, u64)
DEFINE_LOAD(i32_load, u32, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load, u64, u64, u64, FORCE_READ_INT)
DEFINE_LOAD(f32_load, f32, f32, f32, FORCE_READ_FLOAT)
DEFINE_LOAD(f64_load, f64, f64, f64, FORCE_READ_FLOAT)
DEFINE_LOAD(i32_load8_s, s8, s32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load8_s, s8, s64, u64, FORCE_READ_INT)
DEFINE_LOAD(i32_load8_u, u8, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load8_u, u8, u64, u64, FORCE_READ_INT)
DEFINE_LOAD(i32_load16_s, s16, s32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load16_s, s16, s64, u64, FORCE_READ_INT)
DEFINE_LOAD(i32_load16_u, u16, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load16_u, u16, u64, u64, FORCE_READ_INT)
DEFINE_LOAD(i64_load32_s, s32, s64, u64, FORCE_READ_INT)
DEFINE_LOAD(i64_load32_u, u32, u64, u64, FORCE_READ_INT)
DEFINE_STORE(i32_store, u32, u32)
DEFINE_STORE(i64_store, u64, u64)
DEFINE_STORE(f32_store, f32, f32)
Expand Down
18 changes: 9 additions & 9 deletions src/template/wasm2c_atomicops.declarations.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,24 @@
TRAP(UNALIGNED); \
}

#define DEFINE_ATOMIC_LOAD(name, t1, t2, t3) \
#define DEFINE_ATOMIC_LOAD(name, t1, t2, t3, force_read) \
static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
MEMCHECK(mem, addr, t1); \
ATOMIC_ALIGNMENT_CHECK(addr, t1); \
t1 result; \
wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \
result = atomic_load_##t1(&mem->data[addr]); \
wasm_asm("" ::"r"(result)); \
force_read(result); \
return (t3)(t2)result; \
}

DEFINE_ATOMIC_LOAD(i32_atomic_load, u32, u32, u32)
DEFINE_ATOMIC_LOAD(i64_atomic_load, u64, u64, u64)
DEFINE_ATOMIC_LOAD(i32_atomic_load8_u, u8, u32, u32)
DEFINE_ATOMIC_LOAD(i64_atomic_load8_u, u8, u64, u64)
DEFINE_ATOMIC_LOAD(i32_atomic_load16_u, u16, u32, u32)
DEFINE_ATOMIC_LOAD(i64_atomic_load16_u, u16, u64, u64)
DEFINE_ATOMIC_LOAD(i64_atomic_load32_u, u32, u64, u64)
DEFINE_ATOMIC_LOAD(i32_atomic_load, u32, u32, u32, FORCE_READ_INT)
DEFINE_ATOMIC_LOAD(i64_atomic_load, u64, u64, u64, FORCE_READ_INT)
DEFINE_ATOMIC_LOAD(i32_atomic_load8_u, u8, u32, u32, FORCE_READ_INT)
DEFINE_ATOMIC_LOAD(i64_atomic_load8_u, u8, u64, u64, FORCE_READ_INT)
DEFINE_ATOMIC_LOAD(i32_atomic_load16_u, u16, u32, u32, FORCE_READ_INT)
DEFINE_ATOMIC_LOAD(i64_atomic_load16_u, u16, u64, u64, FORCE_READ_INT)
DEFINE_ATOMIC_LOAD(i64_atomic_load32_u, u32, u64, u64, FORCE_READ_INT)

#define DEFINE_ATOMIC_STORE(name, t1, t2) \
static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \
Expand Down
4 changes: 2 additions & 2 deletions src/template/wasm2c_simd.declarations.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifdef __x86_64__
#define SIMD_FORCE_READ(var) wasm_asm("" ::"x"(var));
#if defined(__GNUC__) && defined(__x86_64__)
#define SIMD_FORCE_READ(var) __asm__("" ::"x"(var));
#else
#define SIMD_FORCE_READ(var)
#endif
Expand Down
48 changes: 28 additions & 20 deletions test/wasm2c/add.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,17 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
#endif

#ifdef __GNUC__
#define wasm_asm __asm__
#define FORCE_READ_INT(var) __asm__("" ::"r"(var));
// Clang on Mips requires "f" constraints on floats
#if defined(__clang__) && \
(defined(mips) || defined(__mips__) || defined(__mips))
#define FORCE_READ_FLOAT(var) __asm__("" ::"f"(var));
#else
#define wasm_asm(X)
#define FORCE_READ_FLOAT(var) __asm__("" ::"r"(var));
#endif
#else
#define FORCE_READ_INT(var)
#define FORCE_READ_FLOAT(var)
#endif

#if WABT_BIG_ENDIAN
Expand All @@ -139,13 +147,13 @@ static inline void load_data(void* dest, const void* src, size_t n) {
RANGE_CHECK((&m), m.size - o - s, s); \
load_data(&(m.data[m.size - o - s]), i, s); \
} while (0)
#define DEFINE_LOAD(name, t1, t2, t3) \
#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
MEMCHECK(mem, addr, t1); \
t1 result; \
wasm_rt_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], \
sizeof(t1)); \
wasm_asm("" ::"r"(result)); \
force_read(result); \
return (t3)(t2)result; \
}

Expand All @@ -168,12 +176,12 @@ static inline void load_data(void* dest, const void* src, size_t n) {
RANGE_CHECK((&m), o, s); \
load_data(&(m.data[o]), i, s); \
} while (0)
#define DEFINE_LOAD(name, t1, t2, t3) \
#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
MEMCHECK(mem, addr, t1); \
t1 result; \
wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \
wasm_asm("" ::"r"(result)); \
force_read(result); \
return (t3)(t2)result; \
}

Expand All @@ -185,20 +193,20 @@ static inline void load_data(void* dest, const void* src, size_t n) {
}
#endif

DEFINE_LOAD(i32_load, u32, u32, u32)
DEFINE_LOAD(i64_load, u64, u64, u64)
DEFINE_LOAD(f32_load, f32, f32, f32)
DEFINE_LOAD(f64_load, f64, f64, f64)
DEFINE_LOAD(i32_load8_s, s8, s32, u32)
DEFINE_LOAD(i64_load8_s, s8, s64, u64)
DEFINE_LOAD(i32_load8_u, u8, u32, u32)
DEFINE_LOAD(i64_load8_u, u8, u64, u64)
DEFINE_LOAD(i32_load16_s, s16, s32, u32)
DEFINE_LOAD(i64_load16_s, s16, s64, u64)
DEFINE_LOAD(i32_load16_u, u16, u32, u32)
DEFINE_LOAD(i64_load16_u, u16, u64, u64)
DEFINE_LOAD(i64_load32_s, s32, s64, u64)
DEFINE_LOAD(i64_load32_u, u32, u64, u64)
DEFINE_LOAD(i32_load, u32, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load, u64, u64, u64, FORCE_READ_INT)
DEFINE_LOAD(f32_load, f32, f32, f32, FORCE_READ_FLOAT)
DEFINE_LOAD(f64_load, f64, f64, f64, FORCE_READ_FLOAT)
DEFINE_LOAD(i32_load8_s, s8, s32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load8_s, s8, s64, u64, FORCE_READ_INT)
DEFINE_LOAD(i32_load8_u, u8, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load8_u, u8, u64, u64, FORCE_READ_INT)
DEFINE_LOAD(i32_load16_s, s16, s32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load16_s, s16, s64, u64, FORCE_READ_INT)
DEFINE_LOAD(i32_load16_u, u16, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load16_u, u16, u64, u64, FORCE_READ_INT)
DEFINE_LOAD(i64_load32_s, s32, s64, u64, FORCE_READ_INT)
DEFINE_LOAD(i64_load32_u, u32, u64, u64, FORCE_READ_INT)
DEFINE_STORE(i32_store, u32, u32)
DEFINE_STORE(i64_store, u64, u64)
DEFINE_STORE(f32_store, f32, f32)
Expand Down
Loading
Loading