{"id":107041,"date":"2022-08-23T07:00:00","date_gmt":"2022-08-23T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107041"},"modified":"2022-08-23T06:26:24","modified_gmt":"2022-08-23T13:26:24","slug":"20220823-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20220823-00\/?p=107041","title":{"rendered":"The AArch64 processor (aka arm64), part 20: The classic calling convention"},"content":{"rendered":"<p>For AArch64, Windows employs two calling conventions. One is for classic 64-bit ARM code and the other (named ARM64EC) is for 64-bit ARM code that is intended to interoperate with x64 emulation. The EC stands for &#8220;Emulation Compatible&#8221;. We&#8217;ll look at the ARM64EC calling convention later.<\/p>\n<p>The classic calling convention follows <a href=\"https:\/\/static.docs.arm.com\/100986\/0000\/abi_sve_aapcs64_100986_0000_00_en.pdf\"> the Procedure Call Standard for the ARM 64-bit Architecture<\/a>. This is a rather lengthy document, <a href=\"https:\/\/docs.microsoft.com\/en-us\/cpp\/build\/arm64-windows-abi-conventions?view=msvc-160\"> summarized on MSDN<\/a>, and which I will further simplify here. If you need to dig into the details for weirdo edge cases, go to the other documents.<\/p>\n<p>Integer and pointer parameters are passed in <var>x0<\/var> through <var>x7<\/var>, and floating point parameters go into <var>v0<\/var> through <var>v7<\/var>.<\/p>\n<p>If a parameter does not fill its assigned register or memory, then the value goes into the low-order bits and the upper bits are <i>uninitialized<\/i>.<\/p>\n<p>If a parameter is a large structure (larger than 16 bytes), then it is passed by address. Small structures are passed by value, packed into integer registers. (Execption: If the small structure consists entirely of floats or entirely of doubles, then it is passed in floating point registers, one for each member.)<\/p>\n<p>Each parameter is assigned the next available register for its value class (integer\/pointer or floating point). Since the value goes into the low-order bits, this means that floats are effectively passed in <var>s#<\/var> and doubles in <var>d#<\/var>.<\/p>\n<p>If a parameter is a 128-bit integer, it consumes an even\/odd register pair. This may force an odd-numbered integer register to be skipped.<\/p>\n<p>There is no backfilling, as occurred on AArch32. If a floating point register is used to hold a float, the remaining bits are left unused and cannot be used to hold a later float parameter.<\/p>\n<p>If you run out of registers for a particular value class, future parameters go onto the stack. However, parameters of the other value class can still go into registers if registers are still available.<\/p>\n<p>If a structure does not fit entirely in registers, then it goes completely on the stack.<\/p>\n<p>There is no parameter home space on the stack. At function entry, the first stack-based parameter is stored directly at the top of the stack.<\/p>\n<p>The return value is placed in <var>x0<\/var>\/<var>x1<\/var> or <var>v0<\/var>\/<var>v1<\/var>, depending on its value class. Again, if the return value doesn&#8217;t fill the output register, the unused bits are left uninitialized. If the return value doesn&#8217;t fit in two registers, then the caller passes as a secret first parameter a pointer to a block of memory that receives the return value.<\/p>\n<p>All stack parameters are caller-clean. In practice, instead of cleaning the stack after every call, the caller preadjusts the stack pointer in its prologue to reserve space for all outbound stack-based parameters and just reuses the space for each function call, doing the cleanup in the epilogue.<\/p>\n<p>Here are some examples:<\/p>\n<pre>void f(int8_t a, int64_t b, int16_t c);\r\n\r\n    ; x0[ 7:0] = a\r\n    ; x1[63:0] = b\r\n    ; x2[15:0] = c\r\n<\/pre>\n<p>The parameters that are smaller than a 64-bit register occupy the low-order bits of the 64-bit register, and the upper bits are garbage. The recipient may not assume that the upper bits are the zero-extension or sign-extension of the formal parameter.<\/p>\n<pre>void f(float a, int b, double c, float d)\r\n\r\n    ; s0       = a\r\n    ; x0[31:0] = b\r\n    ; d1       = c\r\n    ; s2       = d\r\n<\/pre>\n<p>Notice that parameter <var>b<\/var> goes into <var>x0<\/var> since it is the first integer\/pointer parameter. The fact that <var>s0<\/var> was taken by <var>a<\/var> is irrelevant.<\/p>\n<p>Note also that parameter <var>d<\/var> goes into <var>s2<\/var> rather than sneaking into the unused upper bits of <var>v0<\/var>.<\/p>\n<p>Since integer\/pointer and floating point parameters are allocated independently, you can have multiple signatures that all use the same underlying calling convention.<\/p>\n<pre>void f(int i1, float f1);\r\nvoid f(float f1, int i1);\r\n<\/pre>\n<p>In the next example, the structure <code>T<\/code> is a so-called <i>homogeneous floating-point aggregate<\/i>: It consists of a series of identical floating point types. It therefore is passable in floating point registers.<\/p>\n<pre>struct T { float x; float y; float a[2]; };\r\nvoid f(T t1, float f1, T t2, float f2, int i);\r\n\r\n    ; s0 = t1.x\r\n    ; s1 = t1.y\r\n    ; s2 = t1.a[0]\r\n    ; s3 = t1.a[1]\r\n    ; s4 = f\r\n    ; t2 on the stack\r\n    ; f2 on the stack\r\n    ; w0 = i\r\n<\/pre>\n<p>The first parameter <var>t1<\/var> is passed in registers <var>s0<\/var> through <var>s3<\/var>. Next comes a float, which goes into <var>s4<\/var>. And then comes another <code>T<\/code>, but there are not enough registers remaining, so <var>t2<\/var> goes onto the stack. Note that <var>f2<\/var> also goes on the stack; it does not backfill into <var>s5<\/var>. On the other hand, we haven&#8217;t run out of integer registers, so <var>i<\/var> can get passed in the low 32 bits of <var>x0<\/var>.<\/p>\n<p>Varadic functions follow a different set of register assignment rules: Floating point registers are not used by variadic functions. All floating point parameters are passed as if they were integer parameters: A single-precision floating point parameter is passed as if it were a 32-bit integer, and a double-precision floating point parameter is passed as if it were a 64-bit integer. This rule applies even to the non-variadic parameters.<\/p>\n<p>Next time, we&#8217;ll look at how these parameter passing rules are implemented in code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How parameters are passed.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2],"class_list":["post-107041","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>How parameters are passed.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107041","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=107041"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107041\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=107041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107041"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}