stdatomic: i386/x86_64 support

This commit is contained in:
Dmitry Selyutin 2021-03-16 23:59:48 +03:00
parent 806b3f987e
commit 9ed3de91a2
7 changed files with 136 additions and 2 deletions

View File

@ -42,8 +42,8 @@ $(X)BT_O += tcov.o
DSO_O = dsohandle.o DSO_O = dsohandle.o
I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BT_O) I386_O = libtcc1.o alloca86.o alloca86-bt.o $(BT_O) atomic-i386.o
X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o $(BT_O) X86_64_O = libtcc1.o alloca86_64.o alloca86_64-bt.o $(BT_O) atomic-x86_64.o
ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o fetch_and_add_arm.o $(BT_O) ARM_O = libtcc1.o armeabi.o alloca-arm.o armflush.o fetch_and_add_arm.o $(BT_O)
ARM64_O = lib-arm64.o fetch_and_add_arm64.o $(BT_O) ARM64_O = lib-arm64.o fetch_and_add_arm64.o $(BT_O)
RISCV64_O = lib-arm64.o fetch_and_add_riscv64.o $(BT_O) RISCV64_O = lib-arm64.o fetch_and_add_riscv64.o $(BT_O)

25
lib/atomic-gen32.c Normal file
View File

@ -0,0 +1,25 @@
#include "atomic.h"
ATOMIC_EXCHANGE(uint8_t, 1)
ATOMIC_EXCHANGE(uint16_t, 2)
ATOMIC_EXCHANGE(uint32_t, 4)
ATOMIC_FETCH_ADD(uint8_t, 1)
ATOMIC_FETCH_ADD(uint16_t, 2)
ATOMIC_FETCH_ADD(uint32_t, 4)
ATOMIC_FETCH_SUB(uint8_t, 1)
ATOMIC_FETCH_SUB(uint16_t, 2)
ATOMIC_FETCH_SUB(uint32_t, 4)
ATOMIC_FETCH_AND(uint8_t, 1)
ATOMIC_FETCH_AND(uint16_t, 2)
ATOMIC_FETCH_AND(uint32_t, 4)
ATOMIC_FETCH_OR(uint8_t, 1)
ATOMIC_FETCH_OR(uint16_t, 2)
ATOMIC_FETCH_OR(uint32_t, 4)
ATOMIC_FETCH_XOR(uint8_t, 1)
ATOMIC_FETCH_XOR(uint16_t, 2)
ATOMIC_FETCH_XOR(uint32_t, 4)

8
lib/atomic-gen64.c Normal file
View File

@ -0,0 +1,8 @@
#include "atomic.h"
ATOMIC_EXCHANGE(uint64_t, 8)
ATOMIC_FETCH_ADD(uint64_t, 8)
ATOMIC_FETCH_SUB(uint64_t, 8)
ATOMIC_FETCH_AND(uint64_t, 8)
ATOMIC_FETCH_OR(uint64_t, 8)
ATOMIC_FETCH_XOR(uint64_t, 8)

19
lib/atomic-i386.c Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <stdint.h>
#include "atomic-x86.h"
ATOMIC_X86_STORE(uint8_t, 1)
ATOMIC_X86_STORE(uint16_t, 2)
ATOMIC_X86_STORE(uint32_t, 4)
ATOMIC_X86_LOAD(uint8_t, 1)
ATOMIC_X86_LOAD(uint16_t, 2)
ATOMIC_X86_LOAD(uint32_t, 4)
ATOMIC_X86_COMPARE_EXCHANGE(uint8_t, 1, "b")
ATOMIC_X86_COMPARE_EXCHANGE(uint16_t, 2, "w")
ATOMIC_X86_COMPARE_EXCHANGE(uint32_t, 4, "l")
#include "atomic-gen32.c"

31
lib/atomic-x86.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#define ATOMIC_X86_COMPARE_EXCHANGE(TYPE, MODE, SUFFIX) \
bool __atomic_compare_exchange_##MODE(_Atomic(TYPE) *atom, TYPE *ref, TYPE xchg) \
{ \
TYPE rv; \
TYPE cmp = *ref; \
asm volatile( \
"lock cmpxchg" SUFFIX " %2,%1\n" \
: "=a" (rv), "+m" (*atom) \
: "q" (xchg), "0" (cmp) \
: "memory" \
); \
*ref = rv; \
return (rv == cmp); \
}
#define ATOMIC_X86_LOAD(TYPE, MODE) \
TYPE __atomic_load_##MODE(const _Atomic(TYPE) *atom) \
{ \
return *(volatile TYPE *)atom; \
}
#define ATOMIC_X86_STORE(TYPE, MODE) \
void __atomic_store_##MODE(_Atomic(TYPE) *atom, TYPE value) \
{ \
*(volatile TYPE *)atom = value; \
}

12
lib/atomic-x86_64.c Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <stdint.h>
#include "atomic-x86.h"
#include "atomic-i386.c"
ATOMIC_X86_STORE(uint64_t, 8)
ATOMIC_X86_LOAD(uint64_t, 8)
ATOMIC_X86_COMPARE_EXCHANGE(uint64_t, 8, "q")
#include "atomic-gen64.c"

39
lib/atomic.h Normal file
View File

@ -0,0 +1,39 @@
#pragma once
#include <stdbool.h>
#include <stdint.h>
#define ATOMIC_GEN_OP(TYPE, MODE, NAME, OP) \
TYPE __atomic_##NAME##_##MODE(_Atomic(TYPE) *atom, TYPE value) \
{ \
TYPE xchg; \
TYPE cmp = __atomic_load(atom, __ATOMIC_RELAXED); \
do { \
xchg = (OP); \
} while (!__atomic_compare_exchange(atom, &cmp, xchg, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)); \
return cmp; \
}
#ifndef ATOMIC_EXCHANGE
# define ATOMIC_EXCHANGE(TYPE, MODE) ATOMIC_GEN_OP(TYPE, MODE, exchange, value)
#endif
#ifndef ATOMIC_FETCH_ADD
# define ATOMIC_FETCH_ADD(TYPE, MODE) ATOMIC_GEN_OP(TYPE, MODE, fetch_add, (cmp + value))
#endif
#ifndef ATOMIC_FETCH_SUB
# define ATOMIC_FETCH_SUB(TYPE, MODE) ATOMIC_GEN_OP(TYPE, MODE, fetch_sub, (cmp - value))
#endif
#ifndef ATOMIC_FETCH_AND
# define ATOMIC_FETCH_AND(TYPE, MODE) ATOMIC_GEN_OP(TYPE, MODE, fetch_and, (cmp & value))
#endif
#ifndef ATOMIC_FETCH_OR
# define ATOMIC_FETCH_OR(TYPE, MODE) ATOMIC_GEN_OP(TYPE, MODE, fetch_or, (cmp | value))
#endif
#ifndef ATOMIC_FETCH_XOR
# define ATOMIC_FETCH_XOR(TYPE, MODE) ATOMIC_GEN_OP(TYPE, MODE, fetch_xor, (cmp ^ value))
#endif