mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-19 17:10:30 +00:00
Add atomics to chibicc
This change also fixes #434 and makes the chibicc assembler better.
This commit is contained in:
parent
5ddf43332e
commit
a988896048
21 changed files with 650 additions and 445 deletions
21
third_party/chibicc/test/asm_test.c
vendored
21
third_party/chibicc/test/asm_test.c
vendored
|
@ -83,6 +83,24 @@ void testFlagOutputs(void) {
|
|||
ASSERT(false, sf);
|
||||
}
|
||||
|
||||
void testAugmentLoByte_onlyModifiesLowerBits(void) {
|
||||
int x, y;
|
||||
x = 0x01020304;
|
||||
y = 0x00000005;
|
||||
asm("sub\t%b1,%b0" : "+q"(x) : "q"(y));
|
||||
ASSERT(0x010203ff, x);
|
||||
ASSERT(0x00000005, y);
|
||||
}
|
||||
|
||||
void testAugmentHiByte_onlyModifiesHigherBits(void) {
|
||||
int x, y;
|
||||
x = 0x01020304;
|
||||
y = 0x00000400;
|
||||
asm("sub\t%h1,%h0" : "+Q"(x) : "Q"(y));
|
||||
ASSERT(0x0102ff04, x);
|
||||
ASSERT(0x00000400, y);
|
||||
}
|
||||
|
||||
int main() {
|
||||
ASSERT(50, asm_fn1());
|
||||
ASSERT(55, asm_fn2());
|
||||
|
@ -135,5 +153,8 @@ int main() {
|
|||
ASSERT(1, !strcmp(p, "hello"));
|
||||
}
|
||||
|
||||
testAugmentLoByte_onlyModifiesLowerBits();
|
||||
testAugmentHiByte_onlyModifiesHigherBits();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
41
third_party/chibicc/test/atomic_test.c
vendored
Normal file
41
third_party/chibicc/test/atomic_test.c
vendored
Normal file
|
@ -0,0 +1,41 @@
|
|||
#include "third_party/chibicc/test/test.h"
|
||||
|
||||
_Atomic(int) lock;
|
||||
|
||||
main() {
|
||||
int x;
|
||||
|
||||
ASSERT(0, __atomic_exchange_n(&lock, 1, __ATOMIC_SEQ_CST));
|
||||
__atomic_load(&lock, &x, __ATOMIC_SEQ_CST);
|
||||
ASSERT(1, x);
|
||||
ASSERT(1, __atomic_exchange_n(&lock, 2, __ATOMIC_SEQ_CST));
|
||||
ASSERT(2, __atomic_exchange_n(&lock, 3, __ATOMIC_SEQ_CST));
|
||||
ASSERT(3, __atomic_load_n(&lock, __ATOMIC_SEQ_CST));
|
||||
__atomic_store_n(&lock, 0, __ATOMIC_SEQ_CST);
|
||||
ASSERT(0, __atomic_fetch_xor(&lock, 3, __ATOMIC_SEQ_CST));
|
||||
ASSERT(3, __atomic_fetch_xor(&lock, 1, __ATOMIC_SEQ_CST));
|
||||
ASSERT(2, __atomic_load_n(&lock, __ATOMIC_SEQ_CST));
|
||||
|
||||
// CAS success #1
|
||||
x = 2;
|
||||
ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 3, 0, __ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
ASSERT(2, x);
|
||||
ASSERT(3, lock);
|
||||
|
||||
// CAS success #2
|
||||
x = 3;
|
||||
ASSERT(1, __atomic_compare_exchange_n(&lock, &x, 4, 0, __ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
ASSERT(3, x);
|
||||
ASSERT(4, lock);
|
||||
|
||||
// CAS fail
|
||||
x = 3;
|
||||
ASSERT(0, __atomic_compare_exchange_n(&lock, &x, 7, 0, __ATOMIC_SEQ_CST,
|
||||
__ATOMIC_SEQ_CST));
|
||||
ASSERT(4, x);
|
||||
ASSERT(4, lock);
|
||||
|
||||
//
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue