Skip to content

Commit 989ebe1

Browse files
authored
cgen,slow_tests: add riscv64 inline asm support, add tests (#26050)
1 parent f21cce4 commit 989ebe1

3 files changed

Lines changed: 74 additions & 3 deletions

File tree

‎.github/workflows/riscv64_linux_ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ jobs:
5050
file ./v
5151
ls -la ./v
5252
./v test vlib/builtin vlib/os vlib/encoding/binary
53+
./v test vlib/v/slow_tests/assembly
5354
VTEST_ONLY=closure ./v test vlib/v/tests

‎vlib/v/gen/c/cgen.v‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,7 +3293,8 @@ fn (mut g Gen) asm_stmt(stmt ast.AsmStmt) {
32933293
}
32943294
// swap destination and operands for att syntax, not for arm64
32953295
if template.args.len != 0 && !template.is_directive && stmt.arch != .arm64
3296-
&& stmt.arch != .s390x && stmt.arch != .ppc64le && stmt.arch != .loongarch64 {
3296+
&& stmt.arch != .s390x && stmt.arch != .ppc64le && stmt.arch != .loongarch64
3297+
&& stmt.arch != .rv64 {
32973298
template.args.prepend(template.args.last())
32983299
template.args.delete(template.args.len - 1)
32993300
}
@@ -3370,7 +3371,8 @@ fn (mut g Gen) asm_arg(arg ast.AsmArg, stmt ast.AsmStmt) {
33703371
ast.IntegerLiteral {
33713372
if stmt.arch == .arm64 {
33723373
g.write('#${arg.val}')
3373-
} else if stmt.arch == .s390x || stmt.arch == .ppc64le || stmt.arch == .loongarch64 {
3374+
} else if stmt.arch == .s390x || stmt.arch == .ppc64le || stmt.arch == .loongarch64
3375+
|| stmt.arch == .rv64 {
33743376
g.write('${arg.val}')
33753377
} else {
33763378
g.write('\$${arg.val}')
@@ -3387,7 +3389,9 @@ fn (mut g Gen) asm_arg(arg ast.AsmArg, stmt ast.AsmStmt) {
33873389
g.write('\$${arg.val.str()}')
33883390
}
33893391
ast.AsmRegister {
3390-
if stmt.arch == .loongarch64 {
3392+
if stmt.arch == .rv64 {
3393+
g.write('${arg.name}')
3394+
} else if stmt.arch == .loongarch64 {
33913395
g.write('$${arg.name}')
33923396
} else {
33933397
if !stmt.is_basic {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
fn test_inline_asm_rv64() {
2+
a, mut b := i64(123), i64(0)
3+
asm rv64 {
4+
// op dst, src
5+
mv t0, a
6+
mv b, t0
7+
; +r (b)
8+
; r (a)
9+
; t0
10+
}
11+
assert a == b
12+
13+
mut c := 0
14+
asm rv64 {
15+
li c, 5
16+
; +r (c)
17+
}
18+
assert c == 5
19+
20+
d, e, mut f := 10, 2, 0
21+
asm rv64 {
22+
mv f, d
23+
add f, f, e
24+
addi f, f, 5
25+
; +r (f)
26+
; r (d)
27+
r (e)
28+
}
29+
assert d == 10
30+
assert e == 2
31+
assert f == 17
32+
33+
g, h, mut i := 2.3, 4.8, -3.5
34+
asm rv64 {
35+
fadd.d i, g, h
36+
; =f (i)
37+
; f (g)
38+
f (h)
39+
}
40+
assert g == 2.3
41+
assert h == 4.8
42+
assert i == 7.1
43+
44+
n1, n2, mut sum, mut prod := 3, 5, -1, -1
45+
asm rv64 {
46+
add '%0', '%2', '%3'
47+
mul '%1', '%2', '%3'
48+
; =&r (sum)
49+
=r (prod)
50+
; r (n1)
51+
r (n2)
52+
}
53+
assert sum == 8
54+
assert prod == 15
55+
56+
l := 5
57+
m := &l
58+
asm rv64 {
59+
li t0, 7
60+
sd t0, [m]
61+
; ; r (m)
62+
; memory
63+
t0
64+
}
65+
assert l == 7
66+
}

0 commit comments

Comments
 (0)