Skip to content

Commit

Permalink
nil
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwayforever committed Sep 14, 2024
1 parent 2d5ed98 commit 0792a9c
Showing 1 changed file with 85 additions and 42 deletions.
127 changes: 85 additions & 42 deletions aarch64_tutorial.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>Table of Contents</h2>
<li><a href="#org000010e">1.2.1. 普通的参数和返回值</a></li>
<li><a href="#org0000110">1.2.2. 返回结构体</a></li>
<li><a href="#org00000f2">1.2.3. 参数为结构体</a></li>
<li><a href="#org00000f5">1.2.4. 变长参数</a></li>
<li><a href="#org0000118">1.2.4. 变长参数</a></li>
</ul>
</li>
<li><a href="#org0000104">1.3. Assembler</a>
Expand Down Expand Up @@ -105,6 +105,7 @@ <h4 id="org0000001"><span class="section-number-4">1.1.1.</span> Register</h4>
<li>X0~X7 用作函数调用时的参数</li>
<li>X8 称为 XR, 在函数调用时用来保存要返回的结构体的地址</li>
<li>X9~X15 是 caller saved</li>
<li>X16, X17 称为 IP0, IP1, linker 生成的 veneer 会使用它做为 scratch</li>
<li>X19~X28 是 callee saved</li>
<li>X29 是 FP</li>
<li>X30 是 LR</li>
Expand Down Expand Up @@ -173,7 +174,7 @@ <h4 id="org0000001"><span class="section-number-4">1.1.1.</span> Register</h4>
</p>

<p>
在函数调用时 d0/s0~d7/s7 用作参数, d8~d31 是 callee-saved register
在函数调用时 {h,s,d}0~7 用作浮点数参数, 8~15 是 callee-saved register
</p></li>
</ul>
</div>
Expand All @@ -183,8 +184,8 @@ <h4 id="org0000001"><span class="section-number-4">1.1.1.</span> Register</h4>
<h4 id="org00000d8"><span class="section-number-4">1.1.2.</span> instructions</h4>
<div class="outline-text-4" id="text-1-1-2">
</div>
<div id="outline-container-org00000ca" class="outline-5">
<h5 id="org00000ca"><span class="section-number-5">1.1.2.1.</span> arith</h5>
<div id="outline-container-org00000cb" class="outline-5">
<h5 id="org00000cb"><span class="section-number-5">1.1.2.1.</span> arith</h5>
<div class="outline-text-5" id="text-1-1-2-1">
</div>
<div id="outline-container-org000001f" class="outline-6">
Expand Down Expand Up @@ -1030,18 +1031,59 @@ <h6 id="org0000074"><span class="section-number-6">1.1.2.7.2.</span> BR/BLR</h6>
4b8: 91002210 add x16, x16, #0x8
4bc: d61f0220 br x17
</pre>

<p>
RISC-V 中针对函数调用会生成 auipc+jalr, 依赖 <a href="toolchain/linker_relaxation.html#ID-b861e156-7ced-4e25-b047-455b5b075c1e">Relaxation</a> 把它转换为 jal, AArch64
针对函数调用则会生成 bl (而不是 adrp+blr), 靠 linker 生成 veneer 来处理过远的函数调用
</p>

<pre class="example" id="org000008a">
$&gt; cat test.S
.text
.global main
main:
bl foo
ret

.data
.rep 1&lt;&lt;25
.dword 0
.endr
foo:
ret

$&gt; arm-objdum a.out
...
00000000004006d4 &lt;main&gt;:
4006d4: 940159d7 bl 456e30 &lt;___veneer&gt;
4006d8: d65f03c0 ret
4006dc: d503201f nop
...
0000000000456e30 &lt;___veneer&gt;:
456e30: 900801f0 adrp x16, 10492000 &lt;__dso_handle+0xfffffc0&gt;
456e34: 91012210 add x16, x16, #0x48
456e38: d61f0200 br x16
...

Disassembly of section .data:

...
0000000010492048 &lt;foo&gt;:
10492048: d65f03c0 ret
1049204c: 00000000 udf #0
</pre>
</div>
</div>

<div id="outline-container-org000008e" class="outline-6">
<h6 id="org000008e"><span class="section-number-6">1.1.2.7.3.</span> B.cond</h6>
<div id="outline-container-org000008f" class="outline-6">
<h6 id="org000008f"><span class="section-number-6">1.1.2.7.3.</span> B.cond</h6>
<div class="outline-text-6" id="text-1-1-2-7-3">
<p>
<code>B.&lt;cond&gt; &lt;label&gt;</code>
</p>


<div id="org000008d" class="figure">
<div id="org000008e" class="figure">
<p><img src="extra/nzcv.png" alt="nzcv.png" />
</p>
</div>
Expand Down Expand Up @@ -1079,14 +1121,14 @@ <h6 id="org000007e"><span class="section-number-6">1.1.2.7.5.</span> TBZ/TBNZ</h
</div>
</div>

<div id="outline-container-org000009b" class="outline-5">
<h5 id="org000009b"><span class="section-number-5">1.1.2.8.</span> CSEL/CINC/CSINC/CSINV/CSNEG/CSET</h5>
<div id="outline-container-org000009c" class="outline-5">
<h5 id="org000009c"><span class="section-number-5">1.1.2.8.</span> CSEL/CINC/CSINC/CSINV/CSNEG/CSET</h5>
<div class="outline-text-5" id="text-1-1-2-8">
<p>
<code>CSEL &lt;Xd&gt;, &lt;Xn&gt;, &lt;Xm&gt;, &lt;cond&gt;</code>
</p>

<pre class="example" id="org000009a">
<pre class="example" id="org000009b">
int foo(int x) {
if (x &gt; 0) {
return 4;
Expand All @@ -1104,12 +1146,12 @@ <h5 id="org000009b"><span class="section-number-5">1.1.2.8.</span> CSEL/CINC/CSI
</div>
</div>

<div id="outline-container-org000009c" class="outline-5">
<h5 id="org000009c"><span class="section-number-5">1.1.2.9.</span> Load</h5>
<div id="outline-container-org00000b2" class="outline-5">
<h5 id="org00000b2"><span class="section-number-5">1.1.2.9.</span> Load</h5>
<div class="outline-text-5" id="text-1-1-2-9">
</div>
<div id="outline-container-org000009f" class="outline-6">
<h6 id="org000009f"><span class="section-number-6">1.1.2.9.1.</span> LDR(immediate)</h6>
<div id="outline-container-org00000a0" class="outline-6">
<h6 id="org00000a0"><span class="section-number-6">1.1.2.9.1.</span> LDR(immediate)</h6>
<div class="outline-text-6" id="text-1-1-2-9-1">
<ul class="org-ul">
<li><p>
Expand Down Expand Up @@ -1177,7 +1219,7 @@ <h6 id="org000009f"><span class="section-number-6">1.1.2.9.1.</span> LDR(immedia
</p></li>
</ul>

<pre class="example" id="org000009e">
<pre class="example" id="org000009f">
void foo(int a) { bar(); }

foo:
Expand All @@ -1194,8 +1236,8 @@ <h6 id="org000009f"><span class="section-number-6">1.1.2.9.1.</span> LDR(immedia
</div>
</div>

<div id="outline-container-org00000a3" class="outline-6">
<h6 id="org00000a3"><span class="section-number-6">1.1.2.9.2.</span> LDR(literal)</h6>
<div id="outline-container-org00000a4" class="outline-6">
<h6 id="org00000a4"><span class="section-number-6">1.1.2.9.2.</span> LDR(literal)</h6>
<div class="outline-text-6" id="text-1-1-2-9-2">
<p>
<code>LDR &lt;Xt&gt;, &lt;label&gt;</code>
Expand Down Expand Up @@ -1226,7 +1268,7 @@ <h6 id="org00000a3"><span class="section-number-6">1.1.2.9.2.</span> LDR(literal
</pre>
</div>

<pre class="example" id="org00000a2">
<pre class="example" id="org00000a3">
Disassembly of section .text:

0000000000000000 &lt;main&gt;:
Expand All @@ -1242,8 +1284,8 @@ <h6 id="org00000a3"><span class="section-number-6">1.1.2.9.2.</span> LDR(literal
</div>
</div>

<div id="outline-container-org00000a7" class="outline-6">
<h6 id="org00000a7"><span class="section-number-6">1.1.2.9.3.</span> LDR (register)</h6>
<div id="outline-container-org00000a8" class="outline-6">
<h6 id="org00000a8"><span class="section-number-6">1.1.2.9.3.</span> LDR (register)</h6>
<div class="outline-text-6" id="text-1-1-2-9-3">
<p>
<code>LDR &lt;Xt&gt;, [&lt;Xn|SP&gt;, &lt;R&gt;&lt;m&gt;{, &lt;extend&gt; {&lt;amount&gt;}}]</code>
Expand All @@ -1253,7 +1295,7 @@ <h6 id="org00000a7"><span class="section-number-6">1.1.2.9.3.</span> LDR (regist
基址变址寻址
</p>

<pre class="example" id="org00000a6">
<pre class="example" id="org00000a7">
int foo (int64_t * x, int y) {
return *(x + y);
}
Expand All @@ -1279,10 +1321,10 @@ <h6 id="org0000093"><span class="section-number-6">1.1.2.9.4.</span> LDRB/LDRH/L
</div>
</div>

<div id="outline-container-org00000a4" class="outline-6">
<h6 id="org00000a4"><span class="section-number-6">1.1.2.9.5.</span> LDP</h6>
<div id="outline-container-org00000af" class="outline-6">
<h6 id="org00000af"><span class="section-number-6">1.1.2.9.5.</span> LDP</h6>
<div class="outline-text-6" id="text-1-1-2-9-5">
<pre class="example" id="org00000ad">
<pre class="example" id="org00000ae">
int *x;
int foo() { return x[0] + x[1]; }

Expand Down Expand Up @@ -1324,8 +1366,8 @@ <h5 id="org00000c8"><span class="section-number-5">1.1.2.11.</span> MOV</h5>
</p>
</div>

<div id="outline-container-org00000ae" class="outline-6">
<h6 id="org00000ae"><span class="section-number-6">1.1.2.11.1.</span> MOVZ</h6>
<div id="outline-container-org00000c1" class="outline-6">
<h6 id="org00000c1"><span class="section-number-6">1.1.2.11.1.</span> MOVZ</h6>
<div class="outline-text-6" id="text-1-1-2-11-1">
<p>
<code>MOVZ &lt;Xd&gt;, #&lt;imm&gt;{, LSL #&lt;shift&gt;}</code>
Expand All @@ -1337,8 +1379,8 @@ <h6 id="org00000ae"><span class="section-number-6">1.1.2.11.1.</span> MOVZ</h6>
</div>
</div>

<div id="outline-container-org00000b2" class="outline-6">
<h6 id="org00000b2"><span class="section-number-6">1.1.2.11.2.</span> MOVK</h6>
<div id="outline-container-org00000c5" class="outline-6">
<h6 id="org00000c5"><span class="section-number-6">1.1.2.11.2.</span> MOVK</h6>
<div class="outline-text-6" id="text-1-1-2-11-2">
<p>
MOVe and Keep
Expand Down Expand Up @@ -1372,7 +1414,7 @@ <h6 id="org00000b2"><span class="section-number-6">1.1.2.11.2.</span> MOVK</h6>
X0 最终为 0xaaaabbbbccccdddd
</p>

<pre class="example" id="org00000c3">
<pre class="example" id="org00000c4">
int64_t foo () {
return 0xaaaabbbbccccdddd;
}
Expand All @@ -1396,8 +1438,8 @@ <h6 id="org00000b2"><span class="section-number-6">1.1.2.11.2.</span> MOVK</h6>
<h5 id="org00000dc"><span class="section-number-5">1.1.2.12.</span> Floating Point</h5>
<div class="outline-text-5" id="text-1-1-2-12">
</div>
<div id="outline-container-org00000cb" class="outline-6">
<h6 id="org00000cb"><span class="section-number-6">1.1.2.12.1.</span> arith</h6>
<div id="outline-container-org00000cc" class="outline-6">
<h6 id="org00000cc"><span class="section-number-6">1.1.2.12.1.</span> arith</h6>
<div class="outline-text-6" id="text-1-1-2-12-1">
<ul class="org-ul">
<li>FADD/FSUB/FMUL/FDIV</li>
Expand Down Expand Up @@ -1482,8 +1524,8 @@ <h6 id="org00000d4"><span class="section-number-6">1.1.2.12.5.</span> SCVTF/UCVT
<h6 id="org00000d7"><span class="section-number-6">1.1.2.12.6.</span> FCSEL</h6>
</div>

<div id="outline-container-org00000c1" class="outline-6">
<h6 id="org00000c1"><span class="section-number-6">1.1.2.12.7.</span> LOAD/STORE</h6>
<div id="outline-container-org00000dd" class="outline-6">
<h6 id="org00000dd"><span class="section-number-6">1.1.2.12.7.</span> LOAD/STORE</h6>
<div class="outline-text-6" id="text-1-1-2-12-7">
<p>
普通的 LDR/STR 等指令也可以 load/store 浮点数, 只要指定 FPR, 例如 `ldr d0, [sp]`
Expand Down Expand Up @@ -1579,7 +1621,7 @@ <h6 id="org00000d2"><span class="section-number-6">1.1.2.14.3.</span> PACIBSP/AU
</pre>
</div>

<pre class="example" id="org00000eb">
<pre class="example" id="org00000ec">
Breakpoint 1, main () at test.S:5
5 pacibsp
(gdb) p /x $lr
Expand Down Expand Up @@ -1611,7 +1653,7 @@ <h6 id="org00000d2"><span class="section-number-6">1.1.2.14.3.</span> PACIBSP/AU
</pre>
</div>

<pre class="example" id="org00000ec">
<pre class="example" id="org00000ed">
Breakpoint 1, main () at test.S:5
5 pacibsp
(gdb) p /x $lr
Expand Down Expand Up @@ -1651,8 +1693,8 @@ <h6 id="org00000f3"><span class="section-number-6">1.1.2.14.4.</span> DMB/DSB/IS
</div>
</div>

<div id="outline-container-org00000f4" class="outline-6">
<h6 id="org00000f4"><span class="section-number-6">1.1.2.14.5.</span> LDXR/STXR/LDAXR/STLXR</h6>
<div id="outline-container-org00000f5" class="outline-6">
<h6 id="org00000f5"><span class="section-number-6">1.1.2.14.5.</span> LDXR/STXR/LDAXR/STLXR</h6>
<div class="outline-text-6" id="text-1-1-2-14-5">
<p>
load/store exclusive register, 类似于 RISC-V 的 <a href="riscv/riscv_tutorial.html#ID-8c466f7a-a7ee-4c3b-b473-f939126eb191">LR/SC</a>
Expand Down Expand Up @@ -1715,7 +1757,7 @@ <h5 id="org00000e7"><span class="section-number-5">1.1.3.1.</span> hello world</
</pre>
</div>

<pre class="example" id="org00000fd">
<pre class="example" id="org00000fe">
$&gt; aarch64-linux-gnu-gcc test.s -O0 -g -static
$&gt; qemu-aarch64 ./a.out
hello world
Expand Down Expand Up @@ -1775,7 +1817,7 @@ <h5 id="org00000e0"><span class="section-number-5">1.1.3.2.</span> argv</h5>
</pre>
</div>

<pre class="example" id="org0000101">
<pre class="example" id="org0000102">
$&gt; aarch64-linux-gnu-gcc test.s -O0 -g -static
$&gt; qemu-aarch64 ./a.out hello world
hello
Expand Down Expand Up @@ -2006,8 +2048,8 @@ <h4 id="org00000f2"><span class="section-number-4">1.2.3.</span> 参数为结构
</div>
</div>

<div id="outline-container-org00000f5" class="outline-4">
<h4 id="org00000f5"><span class="section-number-4">1.2.4.</span> 变长参数</h4>
<div id="outline-container-org0000118" class="outline-4">
<h4 id="org0000118"><span class="section-number-4">1.2.4.</span> 变长参数</h4>
<div class="outline-text-4" id="text-1-2-4">
<p>
caller 会使用 r0~r7, f0~f7 以及 stack 传递参数, 和正常的函数调用一样. 但 callee
Expand Down Expand Up @@ -2097,6 +2139,7 @@ <h3 id="org000012b"><span class="section-number-3">1.5.</span> AArch64 vs. RISC-
<li>aarch64 支持 madd, adc 等指令</li>
<li>aarch64 支持 movk, movz 等指令</li>
<li>aarch64 支持 fp16</li>
<li>aarch64 不支持 thumb 指令, 但 RISC-V 支持 c 扩展</li>
</ul>
</div>
</div>
Expand All @@ -2106,7 +2149,7 @@ <h3 id="org000012b"><span class="section-number-3">1.5.</span> AArch64 vs. RISC-

<p class="author">Author: <a href="mailto:[email protected]">[email protected]</a><br />
Date: 2022-02-08 Tue 15:30<br />
Last updated: 2024-09-14 Sat 16:45</p>
Last updated: 2024-09-14 Sat 19:45</p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="知识共享许可协议" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a>
</div>
</body>
Expand Down

0 comments on commit 0792a9c

Please sign in to comment.