asm.html 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. <!--{
  2. "Title": "A Quick Guide to Go's Assembler",
  3. "Path": "/doc/asm"
  4. }-->
  5. <h2 id="introduction">A Quick Guide to Go's Assembler</h2>
  6. <p>
  7. This document is a quick outline of the unusual form of assembly language used by the <code>gc</code> Go compiler.
  8. The document is not comprehensive.
  9. </p>
  10. <p>
  11. The assembler is based on the input style of the Plan 9 assemblers, which is documented in detail
  12. <a href="https://9p.io/sys/doc/asm.html">elsewhere</a>.
  13. If you plan to write assembly language, you should read that document although much of it is Plan 9-specific.
  14. The current document provides a summary of the syntax and the differences with
  15. what is explained in that document, and
  16. describes the peculiarities that apply when writing assembly code to interact with Go.
  17. </p>
  18. <p>
  19. The most important thing to know about Go's assembler is that it is not a direct representation of the underlying machine.
  20. Some of the details map precisely to the machine, but some do not.
  21. This is because the compiler suite (see
  22. <a href="https://9p.io/sys/doc/compiler.html">this description</a>)
  23. needs no assembler pass in the usual pipeline.
  24. Instead, the compiler operates on a kind of semi-abstract instruction set,
  25. and instruction selection occurs partly after code generation.
  26. The assembler works on the semi-abstract form, so
  27. when you see an instruction like <code>MOV</code>
  28. what the toolchain actually generates for that operation might
  29. not be a move instruction at all, perhaps a clear or load.
  30. Or it might correspond exactly to the machine instruction with that name.
  31. In general, machine-specific operations tend to appear as themselves, while more general concepts like
  32. memory move and subroutine call and return are more abstract.
  33. The details vary with architecture, and we apologize for the imprecision; the situation is not well-defined.
  34. </p>
  35. <p>
  36. The assembler program is a way to parse a description of that
  37. semi-abstract instruction set and turn it into instructions to be
  38. input to the linker.
  39. If you want to see what the instructions look like in assembly for a given architecture, say amd64, there
  40. are many examples in the sources of the standard library, in packages such as
  41. <a href="/pkg/runtime/"><code>runtime</code></a> and
  42. <a href="/pkg/math/big/"><code>math/big</code></a>.
  43. You can also examine what the compiler emits as assembly code
  44. (the actual output may differ from what you see here):
  45. </p>
  46. <pre>
  47. $ cat x.go
  48. package main
  49. func main() {
  50. println(3)
  51. }
  52. $ GOOS=linux GOARCH=amd64 go tool compile -S x.go # or: go build -gcflags -S x.go
  53. "".main STEXT size=74 args=0x0 locals=0x10
  54. 0x0000 00000 (x.go:3) TEXT "".main(SB), $16-0
  55. 0x0000 00000 (x.go:3) MOVQ (TLS), CX
  56. 0x0009 00009 (x.go:3) CMPQ SP, 16(CX)
  57. 0x000d 00013 (x.go:3) JLS 67
  58. 0x000f 00015 (x.go:3) SUBQ $16, SP
  59. 0x0013 00019 (x.go:3) MOVQ BP, 8(SP)
  60. 0x0018 00024 (x.go:3) LEAQ 8(SP), BP
  61. 0x001d 00029 (x.go:3) FUNCDATA $0, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
  62. 0x001d 00029 (x.go:3) FUNCDATA $1, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
  63. 0x001d 00029 (x.go:3) FUNCDATA $2, gclocals·33cdeccccebe80329f1fdbee7f5874cb(SB)
  64. 0x001d 00029 (x.go:4) PCDATA $0, $0
  65. 0x001d 00029 (x.go:4) PCDATA $1, $0
  66. 0x001d 00029 (x.go:4) CALL runtime.printlock(SB)
  67. 0x0022 00034 (x.go:4) MOVQ $3, (SP)
  68. 0x002a 00042 (x.go:4) CALL runtime.printint(SB)
  69. 0x002f 00047 (x.go:4) CALL runtime.printnl(SB)
  70. 0x0034 00052 (x.go:4) CALL runtime.printunlock(SB)
  71. 0x0039 00057 (x.go:5) MOVQ 8(SP), BP
  72. 0x003e 00062 (x.go:5) ADDQ $16, SP
  73. 0x0042 00066 (x.go:5) RET
  74. 0x0043 00067 (x.go:5) NOP
  75. 0x0043 00067 (x.go:3) PCDATA $1, $-1
  76. 0x0043 00067 (x.go:3) PCDATA $0, $-1
  77. 0x0043 00067 (x.go:3) CALL runtime.morestack_noctxt(SB)
  78. 0x0048 00072 (x.go:3) JMP 0
  79. ...
  80. </pre>
  81. <p>
  82. The <code>FUNCDATA</code> and <code>PCDATA</code> directives contain information
  83. for use by the garbage collector; they are introduced by the compiler.
  84. </p>
  85. <p>
  86. To see what gets put in the binary after linking, use <code>go tool objdump</code>:
  87. </p>
  88. <pre>
  89. $ go build -o x.exe x.go
  90. $ go tool objdump -s main.main x.exe
  91. TEXT main.main(SB) /tmp/x.go
  92. x.go:3 0x10501c0 65488b0c2530000000 MOVQ GS:0x30, CX
  93. x.go:3 0x10501c9 483b6110 CMPQ 0x10(CX), SP
  94. x.go:3 0x10501cd 7634 JBE 0x1050203
  95. x.go:3 0x10501cf 4883ec10 SUBQ $0x10, SP
  96. x.go:3 0x10501d3 48896c2408 MOVQ BP, 0x8(SP)
  97. x.go:3 0x10501d8 488d6c2408 LEAQ 0x8(SP), BP
  98. x.go:4 0x10501dd e86e45fdff CALL runtime.printlock(SB)
  99. x.go:4 0x10501e2 48c7042403000000 MOVQ $0x3, 0(SP)
  100. x.go:4 0x10501ea e8e14cfdff CALL runtime.printint(SB)
  101. x.go:4 0x10501ef e8ec47fdff CALL runtime.printnl(SB)
  102. x.go:4 0x10501f4 e8d745fdff CALL runtime.printunlock(SB)
  103. x.go:5 0x10501f9 488b6c2408 MOVQ 0x8(SP), BP
  104. x.go:5 0x10501fe 4883c410 ADDQ $0x10, SP
  105. x.go:5 0x1050202 c3 RET
  106. x.go:3 0x1050203 e83882ffff CALL runtime.morestack_noctxt(SB)
  107. x.go:3 0x1050208 ebb6 JMP main.main(SB)
  108. </pre>
  109. <h3 id="constants">Constants</h3>
  110. <p>
  111. Although the assembler takes its guidance from the Plan 9 assemblers,
  112. it is a distinct program, so there are some differences.
  113. One is in constant evaluation.
  114. Constant expressions in the assembler are parsed using Go's operator
  115. precedence, not the C-like precedence of the original.
  116. Thus <code>3&amp;1&lt;&lt;2</code> is 4, not 0—it parses as <code>(3&amp;1)&lt;&lt;2</code>
  117. not <code>3&amp;(1&lt;&lt;2)</code>.
  118. Also, constants are always evaluated as 64-bit unsigned integers.
  119. Thus <code>-2</code> is not the integer value minus two,
  120. but the unsigned 64-bit integer with the same bit pattern.
  121. The distinction rarely matters but
  122. to avoid ambiguity, division or right shift where the right operand's
  123. high bit is set is rejected.
  124. </p>
  125. <h3 id="symbols">Symbols</h3>
  126. <p>
  127. Some symbols, such as <code>R1</code> or <code>LR</code>,
  128. are predefined and refer to registers.
  129. The exact set depends on the architecture.
  130. </p>
  131. <p>
  132. There are four predeclared symbols that refer to pseudo-registers.
  133. These are not real registers, but rather virtual registers maintained by
  134. the toolchain, such as a frame pointer.
  135. The set of pseudo-registers is the same for all architectures:
  136. </p>
  137. <ul>
  138. <li>
  139. <code>FP</code>: Frame pointer: arguments and locals.
  140. </li>
  141. <li>
  142. <code>PC</code>: Program counter:
  143. jumps and branches.
  144. </li>
  145. <li>
  146. <code>SB</code>: Static base pointer: global symbols.
  147. </li>
  148. <li>
  149. <code>SP</code>: Stack pointer: the highest address within the local stack frame.
  150. </li>
  151. </ul>
  152. <p>
  153. All user-defined symbols are written as offsets to the pseudo-registers
  154. <code>FP</code> (arguments and locals) and <code>SB</code> (globals).
  155. </p>
  156. <p>
  157. The <code>SB</code> pseudo-register can be thought of as the origin of memory, so the symbol <code>foo(SB)</code>
  158. is the name <code>foo</code> as an address in memory.
  159. This form is used to name global functions and data.
  160. Adding <code>&lt;&gt;</code> to the name, as in <span style="white-space: nowrap"><code>foo&lt;&gt;(SB)</code></span>, makes the name
  161. visible only in the current source file, like a top-level <code>static</code> declaration in a C file.
  162. Adding an offset to the name refers to that offset from the symbol's address, so
  163. <code>foo+4(SB)</code> is four bytes past the start of <code>foo</code>.
  164. </p>
  165. <p>
  166. The <code>FP</code> pseudo-register is a virtual frame pointer
  167. used to refer to function arguments.
  168. The compilers maintain a virtual frame pointer and refer to the arguments on the stack as offsets from that pseudo-register.
  169. Thus <code>0(FP)</code> is the first argument to the function,
  170. <code>8(FP)</code> is the second (on a 64-bit machine), and so on.
  171. However, when referring to a function argument this way, it is necessary to place a name
  172. at the beginning, as in <code>first_arg+0(FP)</code> and <code>second_arg+8(FP)</code>.
  173. (The meaning of the offset—offset from the frame pointer—distinct
  174. from its use with <code>SB</code>, where it is an offset from the symbol.)
  175. The assembler enforces this convention, rejecting plain <code>0(FP)</code> and <code>8(FP)</code>.
  176. The actual name is semantically irrelevant but should be used to document
  177. the argument's name.
  178. It is worth stressing that <code>FP</code> is always a
  179. pseudo-register, not a hardware
  180. register, even on architectures with a hardware frame pointer.
  181. </p>
  182. <p>
  183. For assembly functions with Go prototypes, <code>go</code> <code>vet</code> will check that the argument names
  184. and offsets match.
  185. On 32-bit systems, the low and high 32 bits of a 64-bit value are distinguished by adding
  186. a <code>_lo</code> or <code>_hi</code> suffix to the name, as in <code>arg_lo+0(FP)</code> or <code>arg_hi+4(FP)</code>.
  187. If a Go prototype does not name its result, the expected assembly name is <code>ret</code>.
  188. </p>
  189. <p>
  190. The <code>SP</code> pseudo-register is a virtual stack pointer
  191. used to refer to frame-local variables and the arguments being
  192. prepared for function calls.
  193. It points to the highest address within the local stack frame, so references should use negative offsets
  194. in the range [−framesize, 0):
  195. <code>x-8(SP)</code>, <code>y-4(SP)</code>, and so on.
  196. </p>
  197. <p>
  198. On architectures with a hardware register named <code>SP</code>,
  199. the name prefix distinguishes
  200. references to the virtual stack pointer from references to the architectural
  201. <code>SP</code> register.
  202. That is, <code>x-8(SP)</code> and <code>-8(SP)</code>
  203. are different memory locations:
  204. the first refers to the virtual stack pointer pseudo-register,
  205. while the second refers to the
  206. hardware's <code>SP</code> register.
  207. </p>
  208. <p>
  209. On machines where <code>SP</code> and <code>PC</code> are
  210. traditionally aliases for a physical, numbered register,
  211. in the Go assembler the names <code>SP</code> and <code>PC</code>
  212. are still treated specially;
  213. for instance, references to <code>SP</code> require a symbol,
  214. much like <code>FP</code>.
  215. To access the actual hardware register use the true <code>R</code> name.
  216. For example, on the ARM architecture the hardware
  217. <code>SP</code> and <code>PC</code> are accessible as
  218. <code>R13</code> and <code>R15</code>.
  219. </p>
  220. <p>
  221. Branches and direct jumps are always written as offsets to the PC, or as
  222. jumps to labels:
  223. </p>
  224. <pre>
  225. label:
  226. MOVW $0, R1
  227. JMP label
  228. </pre>
  229. <p>
  230. Each label is visible only within the function in which it is defined.
  231. It is therefore permitted for multiple functions in a file to define
  232. and use the same label names.
  233. Direct jumps and call instructions can target text symbols,
  234. such as <code>name(SB)</code>, but not offsets from symbols,
  235. such as <code>name+4(SB)</code>.
  236. </p>
  237. <p>
  238. Instructions, registers, and assembler directives are always in UPPER CASE to remind you
  239. that assembly programming is a fraught endeavor.
  240. (Exception: the <code>g</code> register renaming on ARM.)
  241. </p>
  242. <p>
  243. In Go object files and binaries, the full name of a symbol is the
  244. package path followed by a period and the symbol name:
  245. <code>fmt.Printf</code> or <code>math/rand.Int</code>.
  246. Because the assembler's parser treats period and slash as punctuation,
  247. those strings cannot be used directly as identifier names.
  248. Instead, the assembler allows the middle dot character U+00B7
  249. and the division slash U+2215 in identifiers and rewrites them to
  250. plain period and slash.
  251. Within an assembler source file, the symbols above are written as
  252. <code>fmt·Printf</code> and <code>math∕rand·Int</code>.
  253. The assembly listings generated by the compilers when using the <code>-S</code> flag
  254. show the period and slash directly instead of the Unicode replacements
  255. required by the assemblers.
  256. </p>
  257. <p>
  258. Most hand-written assembly files do not include the full package path
  259. in symbol names, because the linker inserts the package path of the current
  260. object file at the beginning of any name starting with a period:
  261. in an assembly source file within the math/rand package implementation,
  262. the package's Int function can be referred to as <code>·Int</code>.
  263. This convention avoids the need to hard-code a package's import path in its
  264. own source code, making it easier to move the code from one location to another.
  265. </p>
  266. <h3 id="directives">Directives</h3>
  267. <p>
  268. The assembler uses various directives to bind text and data to symbol names.
  269. For example, here is a simple complete function definition. The <code>TEXT</code>
  270. directive declares the symbol <code>runtime·profileloop</code> and the instructions
  271. that follow form the body of the function.
  272. The last instruction in a <code>TEXT</code> block must be some sort of jump, usually a <code>RET</code> (pseudo-)instruction.
  273. (If it's not, the linker will append a jump-to-itself instruction; there is no fallthrough in <code>TEXTs</code>.)
  274. After the symbol, the arguments are flags (see below)
  275. and the frame size, a constant (but see below):
  276. </p>
  277. <pre>
  278. TEXT runtime·profileloop(SB),NOSPLIT,$8
  279. MOVQ $runtime·profileloop1(SB), CX
  280. MOVQ CX, 0(SP)
  281. CALL runtime·externalthreadhandler(SB)
  282. RET
  283. </pre>
  284. <p>
  285. In the general case, the frame size is followed by an argument size, separated by a minus sign.
  286. (It's not a subtraction, just idiosyncratic syntax.)
  287. The frame size <code>$24-8</code> states that the function has a 24-byte frame
  288. and is called with 8 bytes of argument, which live on the caller's frame.
  289. If <code>NOSPLIT</code> is not specified for the <code>TEXT</code>,
  290. the argument size must be provided.
  291. For assembly functions with Go prototypes, <code>go</code> <code>vet</code> will check that the
  292. argument size is correct.
  293. </p>
  294. <p>
  295. Note that the symbol name uses a middle dot to separate the components and is specified as an offset from the
  296. static base pseudo-register <code>SB</code>.
  297. This function would be called from Go source for package <code>runtime</code> using the
  298. simple name <code>profileloop</code>.
  299. </p>
  300. <p>
  301. Global data symbols are defined by a sequence of initializing
  302. <code>DATA</code> directives followed by a <code>GLOBL</code> directive.
  303. Each <code>DATA</code> directive initializes a section of the
  304. corresponding memory.
  305. The memory not explicitly initialized is zeroed.
  306. The general form of the <code>DATA</code> directive is
  307. <pre>
  308. DATA symbol+offset(SB)/width, value
  309. </pre>
  310. <p>
  311. which initializes the symbol memory at the given offset and width with the given value.
  312. The <code>DATA</code> directives for a given symbol must be written with increasing offsets.
  313. </p>
  314. <p>
  315. The <code>GLOBL</code> directive declares a symbol to be global.
  316. The arguments are optional flags and the size of the data being declared as a global,
  317. which will have initial value all zeros unless a <code>DATA</code> directive
  318. has initialized it.
  319. The <code>GLOBL</code> directive must follow any corresponding <code>DATA</code> directives.
  320. </p>
  321. <p>
  322. For example,
  323. </p>
  324. <pre>
  325. DATA divtab&lt;&gt;+0x00(SB)/4, $0xf4f8fcff
  326. DATA divtab&lt;&gt;+0x04(SB)/4, $0xe6eaedf0
  327. ...
  328. DATA divtab&lt;&gt;+0x3c(SB)/4, $0x81828384
  329. GLOBL divtab&lt;&gt;(SB), RODATA, $64
  330. GLOBL runtime·tlsoffset(SB), NOPTR, $4
  331. </pre>
  332. <p>
  333. declares and initializes <code>divtab&lt;&gt;</code>, a read-only 64-byte table of 4-byte integer values,
  334. and declares <code>runtime·tlsoffset</code>, a 4-byte, implicitly zeroed variable that
  335. contains no pointers.
  336. </p>
  337. <p>
  338. There may be one or two arguments to the directives.
  339. If there are two, the first is a bit mask of flags,
  340. which can be written as numeric expressions, added or or-ed together,
  341. or can be set symbolically for easier absorption by a human.
  342. Their values, defined in the standard <code>#include</code> file <code>textflag.h</code>, are:
  343. </p>
  344. <ul>
  345. <li>
  346. <code>NOPROF</code> = 1
  347. <br>
  348. (For <code>TEXT</code> items.)
  349. Don't profile the marked function. This flag is deprecated.
  350. </li>
  351. <li>
  352. <code>DUPOK</code> = 2
  353. <br>
  354. It is legal to have multiple instances of this symbol in a single binary.
  355. The linker will choose one of the duplicates to use.
  356. </li>
  357. <li>
  358. <code>NOSPLIT</code> = 4
  359. <br>
  360. (For <code>TEXT</code> items.)
  361. Don't insert the preamble to check if the stack must be split.
  362. The frame for the routine, plus anything it calls, must fit in the
  363. spare space remaining in the current stack segment.
  364. Used to protect routines such as the stack splitting code itself.
  365. </li>
  366. <li>
  367. <code>RODATA</code> = 8
  368. <br>
  369. (For <code>DATA</code> and <code>GLOBL</code> items.)
  370. Put this data in a read-only section.
  371. </li>
  372. <li>
  373. <code>NOPTR</code> = 16
  374. <br>
  375. (For <code>DATA</code> and <code>GLOBL</code> items.)
  376. This data contains no pointers and therefore does not need to be
  377. scanned by the garbage collector.
  378. </li>
  379. <li>
  380. <code>WRAPPER</code> = 32
  381. <br>
  382. (For <code>TEXT</code> items.)
  383. This is a wrapper function and should not count as disabling <code>recover</code>.
  384. </li>
  385. <li>
  386. <code>NEEDCTXT</code> = 64
  387. <br>
  388. (For <code>TEXT</code> items.)
  389. This function is a closure so it uses its incoming context register.
  390. </li>
  391. <li>
  392. <code>LOCAL</code> = 128
  393. <br>
  394. This symbol is local to the dynamic shared object.
  395. </li>
  396. <li>
  397. <code>TLSBSS</code> = 256
  398. <br>
  399. (For <code>DATA</code> and <code>GLOBL</code> items.)
  400. Put this data in thread local storage.
  401. </li>
  402. <li>
  403. <code>NOFRAME</code> = 512
  404. <br>
  405. (For <code>TEXT</code> items.)
  406. Do not insert instructions to allocate a stack frame and save/restore the return
  407. address, even if this is not a leaf function.
  408. Only valid on functions that declare a frame size of 0.
  409. </li>
  410. <li>
  411. <code>TOPFRAME</code> = 2048
  412. <br>
  413. (For <code>TEXT</code> items.)
  414. Function is the outermost frame of the call stack. Traceback should stop at this function.
  415. </li>
  416. </ul>
  417. <h3 id="special-instructions">Special instructions</h3>
  418. <p>
  419. The <code>PCALIGN</code> pseudo-instruction is used to indicate that the next instruction should be aligned
  420. to a specified boundary by padding with no-op instructions.
  421. </p>
  422. <p>
  423. It is currently supported on arm64, amd64, ppc64, loong64 and riscv64.
  424. For example, the start of the <code>MOVD</code> instruction below is aligned to 32 bytes:
  425. <pre>
  426. PCALIGN $32
  427. MOVD $2, R0
  428. </pre>
  429. </p>
  430. <h3 id="data-offsets">Interacting with Go types and constants</h3>
  431. <p>
  432. If a package has any .s files, then <code>go build</code> will direct
  433. the compiler to emit a special header called <code>go_asm.h</code>,
  434. which the .s files can then <code>#include</code>.
  435. The file contains symbolic <code>#define</code> constants for the
  436. offsets of Go struct fields, the sizes of Go struct types, and most
  437. Go <code>const</code> declarations defined in the current package.
  438. Go assembly should avoid making assumptions about the layout of Go
  439. types and instead use these constants.
  440. This improves the readability of assembly code, and keeps it robust to
  441. changes in data layout either in the Go type definitions or in the
  442. layout rules used by the Go compiler.
  443. </p>
  444. <p>
  445. Constants are of the form <code>const_<i>name</i></code>.
  446. For example, given the Go declaration <code>const bufSize =
  447. 1024</code>, assembly code can refer to the value of this constant
  448. as <code>const_bufSize</code>.
  449. </p>
  450. <p>
  451. Field offsets are of the form <code><i>type</i>_<i>field</i></code>.
  452. Struct sizes are of the form <code><i>type</i>__size</code>.
  453. For example, consider the following Go definition:
  454. </p>
  455. <pre>
  456. type reader struct {
  457. buf [bufSize]byte
  458. r int
  459. }
  460. </pre>
  461. <p>
  462. Assembly can refer to the size of this struct
  463. as <code>reader__size</code> and the offsets of the two fields
  464. as <code>reader_buf</code> and <code>reader_r</code>.
  465. Hence, if register <code>R1</code> contains a pointer to
  466. a <code>reader</code>, assembly can reference the <code>r</code> field
  467. as <code>reader_r(R1)</code>.
  468. </p>
  469. <p>
  470. If any of these <code>#define</code> names are ambiguous (for example,
  471. a struct with a <code>_size</code> field), <code>#include
  472. "go_asm.h"</code> will fail with a "redefinition of macro" error.
  473. </p>
  474. <h3 id="runtime">Runtime Coordination</h3>
  475. <p>
  476. For garbage collection to run correctly, the runtime must know the
  477. location of pointers in all global data and in most stack frames.
  478. The Go compiler emits this information when compiling Go source files,
  479. but assembly programs must define it explicitly.
  480. </p>
  481. <p>
  482. A data symbol marked with the <code>NOPTR</code> flag (see above)
  483. is treated as containing no pointers to runtime-allocated data.
  484. A data symbol with the <code>RODATA</code> flag
  485. is allocated in read-only memory and is therefore treated
  486. as implicitly marked <code>NOPTR</code>.
  487. A data symbol with a total size smaller than a pointer
  488. is also treated as implicitly marked <code>NOPTR</code>.
  489. It is not possible to define a symbol containing pointers in an assembly source file;
  490. such a symbol must be defined in a Go source file instead.
  491. Assembly source can still refer to the symbol by name
  492. even without <code>DATA</code> and <code>GLOBL</code> directives.
  493. A good general rule of thumb is to define all non-<code>RODATA</code>
  494. symbols in Go instead of in assembly.
  495. </p>
  496. <p>
  497. Each function also needs annotations giving the location of
  498. live pointers in its arguments, results, and local stack frame.
  499. For an assembly function with no pointer results and
  500. either no local stack frame or no function calls,
  501. the only requirement is to define a Go prototype for the function
  502. in a Go source file in the same package. The name of the assembly
  503. function must not contain the package name component (for example,
  504. function <code>Syscall</code> in package <code>syscall</code> should
  505. use the name <code>·Syscall</code> instead of the equivalent name
  506. <code>syscall·Syscall</code> in its <code>TEXT</code> directive).
  507. For more complex situations, explicit annotation is needed.
  508. These annotations use pseudo-instructions defined in the standard
  509. <code>#include</code> file <code>funcdata.h</code>.
  510. </p>
  511. <p>
  512. If a function has no arguments and no results,
  513. the pointer information can be omitted.
  514. This is indicated by an argument size annotation of <code>$<i>n</i>-0</code>
  515. on the <code>TEXT</code> instruction.
  516. Otherwise, pointer information must be provided by
  517. a Go prototype for the function in a Go source file,
  518. even for assembly functions not called directly from Go.
  519. (The prototype will also let <code>go</code> <code>vet</code> check the argument references.)
  520. At the start of the function, the arguments are assumed
  521. to be initialized but the results are assumed uninitialized.
  522. If the results will hold live pointers during a call instruction,
  523. the function should start by zeroing the results and then
  524. executing the pseudo-instruction <code>GO_RESULTS_INITIALIZED</code>.
  525. This instruction records that the results are now initialized
  526. and should be scanned during stack movement and garbage collection.
  527. It is typically easier to arrange that assembly functions do not
  528. return pointers or do not contain call instructions;
  529. no assembly functions in the standard library use
  530. <code>GO_RESULTS_INITIALIZED</code>.
  531. </p>
  532. <p>
  533. If a function has no local stack frame,
  534. the pointer information can be omitted.
  535. This is indicated by a local frame size annotation of <code>$0-<i>n</i></code>
  536. on the <code>TEXT</code> instruction.
  537. The pointer information can also be omitted if the
  538. function contains no call instructions.
  539. Otherwise, the local stack frame must not contain pointers,
  540. and the assembly must confirm this fact by executing the
  541. pseudo-instruction <code>NO_LOCAL_POINTERS</code>.
  542. Because stack resizing is implemented by moving the stack,
  543. the stack pointer may change during any function call:
  544. even pointers to stack data must not be kept in local variables.
  545. </p>
  546. <p>
  547. Assembly functions should always be given Go prototypes,
  548. both to provide pointer information for the arguments and results
  549. and to let <code>go</code> <code>vet</code> check that
  550. the offsets being used to access them are correct.
  551. </p>
  552. <h2 id="architectures">Architecture-specific details</h2>
  553. <p>
  554. It is impractical to list all the instructions and other details for each machine.
  555. To see what instructions are defined for a given machine, say ARM,
  556. look in the source for the <code>obj</code> support library for
  557. that architecture, located in the directory <code>src/cmd/internal/obj/arm</code>.
  558. In that directory is a file <code>a.out.go</code>; it contains
  559. a long list of constants starting with <code>A</code>, like this:
  560. </p>
  561. <pre>
  562. const (
  563. AAND = obj.ABaseARM + obj.A_ARCHSPECIFIC + iota
  564. AEOR
  565. ASUB
  566. ARSB
  567. AADD
  568. ...
  569. </pre>
  570. <p>
  571. This is the list of instructions and their spellings as known to the assembler and linker for that architecture.
  572. Each instruction begins with an initial capital <code>A</code> in this list, so <code>AAND</code>
  573. represents the bitwise and instruction,
  574. <code>AND</code> (without the leading <code>A</code>),
  575. and is written in assembly source as <code>AND</code>.
  576. The enumeration is mostly in alphabetical order.
  577. (The architecture-independent <code>AXXX</code>, defined in the
  578. <code>cmd/internal/obj</code> package,
  579. represents an invalid instruction).
  580. The sequence of the <code>A</code> names has nothing to do with the actual
  581. encoding of the machine instructions.
  582. The <code>cmd/internal/obj</code> package takes care of that detail.
  583. </p>
  584. <p>
  585. The instructions for both the 386 and AMD64 architectures are listed in
  586. <code>cmd/internal/obj/x86/a.out.go</code>.
  587. </p>
  588. <p>
  589. The architectures share syntax for common addressing modes such as
  590. <code>(R1)</code> (register indirect),
  591. <code>4(R1)</code> (register indirect with offset), and
  592. <code>$foo(SB)</code> (absolute address).
  593. The assembler also supports some (not necessarily all) addressing modes
  594. specific to each architecture.
  595. The sections below list these.
  596. </p>
  597. <p>
  598. One detail evident in the examples from the previous sections is that data in the instructions flows from left to right:
  599. <code>MOVQ</code> <code>$0,</code> <code>CX</code> clears <code>CX</code>.
  600. This rule applies even on architectures where the conventional notation uses the opposite direction.
  601. </p>
  602. <p>
  603. Here follow some descriptions of key Go-specific details for the supported architectures.
  604. </p>
  605. <h3 id="x86">32-bit Intel 386</h3>
  606. <p>
  607. The runtime pointer to the <code>g</code> structure is maintained
  608. through the value of an otherwise unused (as far as Go is concerned) register in the MMU.
  609. In the runtime package, assembly code can include <code>go_tls.h</code>, which defines
  610. an OS- and architecture-dependent macro <code>get_tls</code> for accessing this register.
  611. The <code>get_tls</code> macro takes one argument, which is the register to load the
  612. <code>g</code> pointer into.
  613. </p>
  614. <p>
  615. For example, the sequence to load <code>g</code> and <code>m</code>
  616. using <code>CX</code> looks like this:
  617. </p>
  618. <pre>
  619. #include "go_tls.h"
  620. #include "go_asm.h"
  621. ...
  622. get_tls(CX)
  623. MOVL g(CX), AX // Move g into AX.
  624. MOVL g_m(AX), BX // Move g.m into BX.
  625. </pre>
  626. <p>
  627. The <code>get_tls</code> macro is also defined on <a href="#amd64">amd64</a>.
  628. </p>
  629. <p>
  630. Addressing modes:
  631. </p>
  632. <ul>
  633. <li>
  634. <code>(DI)(BX*2)</code>: The location at address <code>DI</code> plus <code>BX*2</code>.
  635. </li>
  636. <li>
  637. <code>64(DI)(BX*2)</code>: The location at address <code>DI</code> plus <code>BX*2</code> plus 64.
  638. These modes accept only 1, 2, 4, and 8 as scale factors.
  639. </li>
  640. </ul>
  641. <p>
  642. When using the compiler and assembler's
  643. <code>-dynlink</code> or <code>-shared</code> modes,
  644. any load or store of a fixed memory location such as a global variable
  645. must be assumed to overwrite <code>CX</code>.
  646. Therefore, to be safe for use with these modes,
  647. assembly sources should typically avoid CX except between memory references.
  648. </p>
  649. <h3 id="amd64">64-bit Intel 386 (a.k.a. amd64)</h3>
  650. <p>
  651. The two architectures behave largely the same at the assembler level.
  652. Assembly code to access the <code>m</code> and <code>g</code>
  653. pointers on the 64-bit version is the same as on the 32-bit 386,
  654. except it uses <code>MOVQ</code> rather than <code>MOVL</code>:
  655. </p>
  656. <pre>
  657. get_tls(CX)
  658. MOVQ g(CX), AX // Move g into AX.
  659. MOVQ g_m(AX), BX // Move g.m into BX.
  660. </pre>
  661. <p>
  662. Register <code>BP</code> is callee-save.
  663. The assembler automatically inserts <code>BP</code> save/restore when frame size is larger than zero.
  664. Using <code>BP</code> as a general purpose register is allowed,
  665. however it can interfere with sampling-based profiling.
  666. </p>
  667. <h3 id="arm">ARM</h3>
  668. <p>
  669. The registers <code>R10</code> and <code>R11</code>
  670. are reserved by the compiler and linker.
  671. </p>
  672. <p>
  673. <code>R10</code> points to the <code>g</code> (goroutine) structure.
  674. Within assembler source code, this pointer must be referred to as <code>g</code>;
  675. the name <code>R10</code> is not recognized.
  676. </p>
  677. <p>
  678. To make it easier for people and compilers to write assembly, the ARM linker
  679. allows general addressing forms and pseudo-operations like <code>DIV</code> or <code>MOD</code>
  680. that may not be expressible using a single hardware instruction.
  681. It implements these forms as multiple instructions, often using the <code>R11</code> register
  682. to hold temporary values.
  683. Hand-written assembly can use <code>R11</code>, but doing so requires
  684. being sure that the linker is not also using it to implement any of the other
  685. instructions in the function.
  686. </p>
  687. <p>
  688. When defining a <code>TEXT</code>, specifying frame size <code>$-4</code>
  689. tells the linker that this is a leaf function that does not need to save <code>LR</code> on entry.
  690. </p>
  691. <p>
  692. The name <code>SP</code> always refers to the virtual stack pointer described earlier.
  693. For the hardware register, use <code>R13</code>.
  694. </p>
  695. <p>
  696. Condition code syntax is to append a period and the one- or two-letter code to the instruction,
  697. as in <code>MOVW.EQ</code>.
  698. Multiple codes may be appended: <code>MOVM.IA.W</code>.
  699. The order of the code modifiers is irrelevant.
  700. </p>
  701. <p>
  702. Addressing modes:
  703. </p>
  704. <ul>
  705. <li>
  706. <code>R0-&gt;16</code>
  707. <br>
  708. <code>R0&gt;&gt;16</code>
  709. <br>
  710. <code>R0&lt;&lt;16</code>
  711. <br>
  712. <code>R0@&gt;16</code>:
  713. For <code>&lt;&lt;</code>, left shift <code>R0</code> by 16 bits.
  714. The other codes are <code>-&gt;</code> (arithmetic right shift),
  715. <code>&gt;&gt;</code> (logical right shift), and
  716. <code>@&gt;</code> (rotate right).
  717. </li>
  718. <li>
  719. <code>R0-&gt;R1</code>
  720. <br>
  721. <code>R0&gt;&gt;R1</code>
  722. <br>
  723. <code>R0&lt;&lt;R1</code>
  724. <br>
  725. <code>R0@&gt;R1</code>:
  726. For <code>&lt;&lt;</code>, left shift <code>R0</code> by the count in <code>R1</code>.
  727. The other codes are <code>-&gt;</code> (arithmetic right shift),
  728. <code>&gt;&gt;</code> (logical right shift), and
  729. <code>@&gt;</code> (rotate right).
  730. </li>
  731. <li>
  732. <code>[R0,g,R12-R15]</code>: For multi-register instructions, the set comprising
  733. <code>R0</code>, <code>g</code>, and <code>R12</code> through <code>R15</code> inclusive.
  734. </li>
  735. <li>
  736. <code>(R5, R6)</code>: Destination register pair.
  737. </li>
  738. </ul>
  739. <h3 id="arm64">ARM64</h3>
  740. <p>
  741. <code>R18</code> is the "platform register", reserved on the Apple platform.
  742. To prevent accidental misuse, the register is named <code>R18_PLATFORM</code>.
  743. <code>R27</code> and <code>R28</code> are reserved by the compiler and linker.
  744. <code>R29</code> is the frame pointer.
  745. <code>R30</code> is the link register.
  746. </p>
  747. <p>
  748. Instruction modifiers are appended to the instruction following a period.
  749. The only modifiers are <code>P</code> (postincrement) and <code>W</code>
  750. (preincrement):
  751. <code>MOVW.P</code>, <code>MOVW.W</code>
  752. </p>
  753. <p>
  754. Addressing modes:
  755. </p>
  756. <ul>
  757. <li>
  758. <code>R0-&gt;16</code>
  759. <br>
  760. <code>R0&gt;&gt;16</code>
  761. <br>
  762. <code>R0&lt;&lt;16</code>
  763. <br>
  764. <code>R0@&gt;16</code>:
  765. These are the same as on the 32-bit ARM.
  766. </li>
  767. <li>
  768. <code>$(8&lt;&lt;12)</code>:
  769. Left shift the immediate value <code>8</code> by <code>12</code> bits.
  770. </li>
  771. <li>
  772. <code>8(R0)</code>:
  773. Add the value of <code>R0</code> and <code>8</code>.
  774. </li>
  775. <li>
  776. <code>(R2)(R0)</code>:
  777. The location at <code>R0</code> plus <code>R2</code>.
  778. </li>
  779. <li>
  780. <code>R0.UXTB</code>
  781. <br>
  782. <code>R0.UXTB&lt;&lt;imm</code>:
  783. <code>UXTB</code>: extract an 8-bit value from the low-order bits of <code>R0</code> and zero-extend it to the size of <code>R0</code>.
  784. <code>R0.UXTB&lt;&lt;imm</code>: left shift the result of <code>R0.UXTB</code> by <code>imm</code> bits.
  785. The <code>imm</code> value can be 0, 1, 2, 3, or 4.
  786. The other extensions include <code>UXTH</code> (16-bit), <code>UXTW</code> (32-bit), and <code>UXTX</code> (64-bit).
  787. </li>
  788. <li>
  789. <code>R0.SXTB</code>
  790. <br>
  791. <code>R0.SXTB&lt;&lt;imm</code>:
  792. <code>SXTB</code>: extract an 8-bit value from the low-order bits of <code>R0</code> and sign-extend it to the size of <code>R0</code>.
  793. <code>R0.SXTB&lt;&lt;imm</code>: left shift the result of <code>R0.SXTB</code> by <code>imm</code> bits.
  794. The <code>imm</code> value can be 0, 1, 2, 3, or 4.
  795. The other extensions include <code>SXTH</code> (16-bit), <code>SXTW</code> (32-bit), and <code>SXTX</code> (64-bit).
  796. </li>
  797. <li>
  798. <code>(R5, R6)</code>: Register pair for <code>LDAXP</code>/<code>LDP</code>/<code>LDXP</code>/<code>STLXP</code>/<code>STP</code>/<code>STP</code>.
  799. </li>
  800. </ul>
  801. <p>
  802. Reference: <a href="/pkg/cmd/internal/obj/arm64">Go ARM64 Assembly Instructions Reference Manual</a>
  803. </p>
  804. <h3 id="ppc64">PPC64</h3>
  805. <p>
  806. This assembler is used by GOARCH values ppc64 and ppc64le.
  807. </p>
  808. <p>
  809. Reference: <a href="/pkg/cmd/internal/obj/ppc64">Go PPC64 Assembly Instructions Reference Manual</a>
  810. </p>
  811. <h3 id="s390x">IBM z/Architecture, a.k.a. s390x</h3>
  812. <p>
  813. The registers <code>R10</code> and <code>R11</code> are reserved.
  814. The assembler uses them to hold temporary values when assembling some instructions.
  815. </p>
  816. <p>
  817. <code>R13</code> points to the <code>g</code> (goroutine) structure.
  818. This register must be referred to as <code>g</code>; the name <code>R13</code> is not recognized.
  819. </p>
  820. <p>
  821. <code>R15</code> points to the stack frame and should typically only be accessed using the
  822. virtual registers <code>SP</code> and <code>FP</code>.
  823. </p>
  824. <p>
  825. Load- and store-multiple instructions operate on a range of registers.
  826. The range of registers is specified by a start register and an end register.
  827. For example, <code>LMG</code> <code>(R9),</code> <code>R5,</code> <code>R7</code> would load
  828. <code>R5</code>, <code>R6</code> and <code>R7</code> with the 64-bit values at
  829. <code>0(R9)</code>, <code>8(R9)</code> and <code>16(R9)</code> respectively.
  830. </p>
  831. <p>
  832. Storage-and-storage instructions such as <code>MVC</code> and <code>XC</code> are written
  833. with the length as the first argument.
  834. For example, <code>XC</code> <code>$8,</code> <code>(R9),</code> <code>(R9)</code> would clear
  835. eight bytes at the address specified in <code>R9</code>.
  836. </p>
  837. <p>
  838. If a vector instruction takes a length or an index as an argument then it will be the
  839. first argument.
  840. For example, <code>VLEIF</code> <code>$1,</code> <code>$16,</code> <code>V2</code> will load
  841. the value sixteen into index one of <code>V2</code>.
  842. Care should be taken when using vector instructions to ensure that they are available at
  843. runtime.
  844. To use vector instructions a machine must have both the vector facility (bit 129 in the
  845. facility list) and kernel support.
  846. Without kernel support a vector instruction will have no effect (it will be equivalent
  847. to a <code>NOP</code> instruction).
  848. </p>
  849. <p>
  850. Addressing modes:
  851. </p>
  852. <ul>
  853. <li>
  854. <code>(R5)(R6*1)</code>: The location at <code>R5</code> plus <code>R6</code>.
  855. It is a scaled mode as on the x86, but the only scale allowed is <code>1</code>.
  856. </li>
  857. </ul>
  858. <h3 id="mips">MIPS, MIPS64</h3>
  859. <p>
  860. General purpose registers are named <code>R0</code> through <code>R31</code>,
  861. floating point registers are <code>F0</code> through <code>F31</code>.
  862. </p>
  863. <p>
  864. <code>R30</code> is reserved to point to <code>g</code>.
  865. <code>R23</code> is used as a temporary register.
  866. </p>
  867. <p>
  868. In a <code>TEXT</code> directive, the frame size <code>$-4</code> for MIPS or
  869. <code>$-8</code> for MIPS64 instructs the linker not to save <code>LR</code>.
  870. </p>
  871. <p>
  872. <code>SP</code> refers to the virtual stack pointer.
  873. For the hardware register, use <code>R29</code>.
  874. </p>
  875. <p>
  876. Addressing modes:
  877. </p>
  878. <ul>
  879. <li>
  880. <code>16(R1)</code>: The location at <code>R1</code> plus 16.
  881. </li>
  882. <li>
  883. <code>(R1)</code>: Alias for <code>0(R1)</code>.
  884. </li>
  885. </ul>
  886. <p>
  887. The value of <code>GOMIPS</code> environment variable (<code>hardfloat</code> or
  888. <code>softfloat</code>) is made available to assembly code by predefining either
  889. <code>GOMIPS_hardfloat</code> or <code>GOMIPS_softfloat</code>.
  890. </p>
  891. <p>
  892. The value of <code>GOMIPS64</code> environment variable (<code>hardfloat</code> or
  893. <code>softfloat</code>) is made available to assembly code by predefining either
  894. <code>GOMIPS64_hardfloat</code> or <code>GOMIPS64_softfloat</code>.
  895. </p>
  896. <h3 id="unsupported_opcodes">Unsupported opcodes</h3>
  897. <p>
  898. The assemblers are designed to support the compiler so not all hardware instructions
  899. are defined for all architectures: if the compiler doesn't generate it, it might not be there.
  900. If you need to use a missing instruction, there are two ways to proceed.
  901. One is to update the assembler to support that instruction, which is straightforward
  902. but only worthwhile if it's likely the instruction will be used again.
  903. Instead, for simple one-off cases, it's possible to use the <code>BYTE</code>
  904. and <code>WORD</code> directives
  905. to lay down explicit data into the instruction stream within a <code>TEXT</code>.
  906. Here's how the 386 runtime defines the 64-bit atomic load function.
  907. </p>
  908. <pre>
  909. // uint64 atomicload64(uint64 volatile* addr);
  910. // so actually
  911. // void atomicload64(uint64 *res, uint64 volatile *addr);
  912. TEXT runtime·atomicload64(SB), NOSPLIT, $0-12
  913. MOVL ptr+0(FP), AX
  914. TESTL $7, AX
  915. JZ 2(PC)
  916. MOVL 0, AX // crash with nil ptr deref
  917. LEAL ret_lo+4(FP), BX
  918. // MOVQ (%EAX), %MM0
  919. BYTE $0x0f; BYTE $0x6f; BYTE $0x00
  920. // MOVQ %MM0, 0(%EBX)
  921. BYTE $0x0f; BYTE $0x7f; BYTE $0x03
  922. // EMMS
  923. BYTE $0x0F; BYTE $0x77
  924. RET
  925. </pre>