Skip to content

Commit f9042f3

Browse files
committedFeb 19, 2025·
add align
1 parent f54829d commit f9042f3

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed
 

‎test/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
HEADER=../xbyak_riscv/xbyak_riscv_mnemonic.hpp
22

3-
test: $(HEADER) jmp_test.exe
3+
test: $(HEADER) jmp_test.exe misc_test.exe
44
./jmp_test.exe
5+
./misc_test.exe
56
./test.sh
67
# ./test.sh svc
78

‎test/misc_test.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <cybozu/test.hpp>
2+
#include <xbyak_riscv/xbyak_riscv.hpp>
3+
4+
using namespace Xbyak_riscv;
5+
6+
CYBOZU_TEST_AUTO(align)
7+
{
8+
struct Code : CodeGenerator {
9+
Code()
10+
{
11+
nop();
12+
addr1 = getCurr();
13+
align(16);
14+
addr2 = getCurr();
15+
nop();
16+
nop();
17+
addr3 = getCurr();
18+
align(16);
19+
addr4 = getCurr();
20+
for (int i = 0; i < 4; i++) nop();
21+
addr5 = getCurr();
22+
// already aligned
23+
align(16);
24+
addr6 = getCurr();
25+
26+
}
27+
const uint8_t *addr1;
28+
const uint8_t *addr2;
29+
const uint8_t *addr3;
30+
const uint8_t *addr4;
31+
const uint8_t *addr5;
32+
const uint8_t *addr6;
33+
} c;
34+
c.ready();
35+
printf("addr1=%p\n", c.addr1);
36+
printf("addr2=%p\n", c.addr2);
37+
printf("addr3=%p\n", c.addr3);
38+
printf("addr4=%p\n", c.addr4);
39+
printf("addr5=%p\n", c.addr5);
40+
printf("addr6=%p\n", c.addr6);
41+
CYBOZU_TEST_ASSERT(size_t(c.addr1) % 16);
42+
CYBOZU_TEST_ASSERT((size_t(c.addr2) % 16) == 0);
43+
CYBOZU_TEST_ASSERT(size_t(c.addr3) % 16);
44+
CYBOZU_TEST_ASSERT((size_t(c.addr4) % 16) == 0);
45+
CYBOZU_TEST_ASSERT((size_t(c.addr5) % 16) == 0);
46+
CYBOZU_TEST_EQUAL(c.addr5, c.addr6);
47+
}
48+
49+

‎xbyak_riscv/xbyak_riscv.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ enum {
102102
ERR_CANT_ALLOC,
103103
ERR_BAD_PARAMETER,
104104
ERR_MUNMAP,
105+
ERR_BAD_ALIGN,
105106
ERR_INTERNAL // Put it at last.
106107
};
107108

@@ -123,6 +124,7 @@ inline const char *ConvertErrorToString(int err)
123124
"can't alloc",
124125
"bad parameter",
125126
"munmap",
127+
"bad align",
126128
"internal error"
127129
};
128130
assert(ERR_INTERNAL == sizeof(errTbl) / sizeof(*errTbl));
@@ -1349,6 +1351,19 @@ class CodeGenerator : public CodeArray {
13491351
// set read/exec
13501352
void readyRE() { return ready(PROTECT_RE); }
13511353

1354+
void align(size_t x)
1355+
{
1356+
if (x == 1) return;
1357+
if (x < 4 || (x & (x - 1))) XBYAK_RISCV_THROW(ERR_BAD_ALIGN)
1358+
size_t remain = size_t(getCurr()) % x;
1359+
if (remain % 4) XBYAK_RISCV_THROW(ERR_INTERNAL)
1360+
if (remain) {
1361+
for (size_t i = 0; i < (x - remain) / 4; i++) {
1362+
nop();
1363+
}
1364+
}
1365+
}
1366+
13521367
#include "xbyak_riscv_mnemonic.hpp"
13531368
#if defined(XBYAK_RISCV_V) && XBYAK_RISCV_V == 1
13541369
#include "xbyak_riscv_v.hpp"

0 commit comments

Comments
 (0)
Please sign in to comment.