Mercurial > pub > dyncall > dyncall
comparison doc/manual/callconvs/callconv_x86.tex @ 472:e5820b7a3fbc
doc:
- some more plan 9 disas examples
- x86 callconv doc updated:
* mainly info about aggregate passing and returning
* removing section about pascal calling convention, as dyncall doesn't support 16bit abis
- some cleanups
author | Tassilo Philipp |
---|---|
date | Thu, 10 Feb 2022 17:32:05 +0100 |
parents | b47168dacba6 |
children | 17287342e273 |
comparison
equal
deleted
inserted
replaced
471:4e84d6faed54 | 472:e5820b7a3fbc |
---|---|
93 \paragraph{Parameter passing} | 93 \paragraph{Parameter passing} |
94 | 94 |
95 \begin{itemize} | 95 \begin{itemize} |
96 \item stack parameter order: right-to-left | 96 \item stack parameter order: right-to-left |
97 \item caller cleans up the stack | 97 \item caller cleans up the stack |
98 \item all arguments are pushed onto the stack | 98 \item all arguments are pushed onto the stack (as dwords) |
99 \item arguments \textgreater\ 64 bits are pushed as a sequence of dwords | |
100 \item aggregates (structs, unions) are pushed as a sequence of dwords | |
99 \end{itemize} | 101 \end{itemize} |
100 | 102 |
101 \paragraph{Return values} | 103 \paragraph{Return values} |
102 | 104 |
103 \begin{itemize} | 105 \begin{itemize} |
104 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 106 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
105 \item integers \textgreater\ 32 bits are returned via the eax and edx registers | 107 \item integers and aggregates (structs, unions) \textgreater\ 32 and \textless=\ 64 bits are returned via the eax and edx registers |
106 \item return values \textgreater\ 64 bits (e.g. structures) are returned by the caller allocating the space and | 108 \item return values \textgreater\ 64 bits (e.g. aggregates) are returned by the caller allocating the space and |
107 passing a pointer to the callee as a new, implicit first parameter (this means, on the stack) | 109 passing a pointer to the callee as a new, implicit first parameter (this means, on the stack) |
108 \item floating point types are returned via the st0 register (except on Minix, where they are returned as integers are) | 110 \item floating point types are returned via the st0 register (except on Minix, where they are returned as integers are) |
109 \end{itemize} | 111 \end{itemize} |
110 | 112 |
111 | 113 |
170 \begin{itemize} | 172 \begin{itemize} |
171 \item stack parameter order: right-to-left | 173 \item stack parameter order: right-to-left |
172 \item called function cleans up the stack | 174 \item called function cleans up the stack |
173 \item first two integers/pointers (\textless=\ 32bit) are passed via ecx and edx (even if preceded by other arguments) | 175 \item first two integers/pointers (\textless=\ 32bit) are passed via ecx and edx (even if preceded by other arguments) |
174 \item if first argument is a 64 bit integer, it is passed via ecx and edx | 176 \item if first argument is a 64 bit integer, it is passed via ecx and edx |
175 \item all other parameters are pushed onto the stack | 177 \item all other parameters are pushed onto the stack (as dwords) |
178 \item arguments \textgreater\ 64 bits are pushed as a sequence of dwords | |
179 \item aggregates (structs, unions) are pushed as a sequence of dwords, but are never split between registers and stack (if registers are still available and | |
180 aggregate doesn't fit entirely into ecx and edx, it is passed via the stack and remaining registers are free for subsequent arguments) | |
176 \end{itemize} | 181 \end{itemize} |
177 | 182 |
178 \paragraph{Return values} | 183 \paragraph{Return values} |
179 | 184 |
180 \begin{itemize} | 185 \begin{itemize} |
181 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 186 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
182 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@verify | 187 \item integers and aggregates (structs, unions) \textgreater\ 32 and \textless=\ 64 bits are returned via the eax and edx registers |
183 \item floating point types are returned via the st0 register@@@ really ? | 188 \item return values \textgreater\ 64 bits (e.g. aggregates) are returned by the caller allocating the space and |
189 passing a pointer to the callee as a new, implicit first parameter (always via the stack, never via a register) | |
190 \item floating point types are returned via the st0 register | |
184 \end{itemize} | 191 \end{itemize} |
185 | 192 |
186 | 193 |
187 \paragraph{Stack layout} | 194 \paragraph{Stack layout} |
188 | 195 |
242 | 249 |
243 \begin{itemize} | 250 \begin{itemize} |
244 \item stack parameter order: right-to-left | 251 \item stack parameter order: right-to-left |
245 \item called function cleans up the stack | 252 \item called function cleans up the stack |
246 \item first two integers/pointers (\textless=\ 32bit) are passed via ecx and edx (even if preceded by other arguments) | 253 \item first two integers/pointers (\textless=\ 32bit) are passed via ecx and edx (even if preceded by other arguments) |
247 \item if first argument is a 64 bit integer, it is pushed on the stack and the two registers are skipped | 254 \item arguments \textgreater\ 32 bits are pushed onto the stack as a sequence of dwords (never passed via registers, any respective register is skipped and not used for subsequent args) |
248 \item all other parameters are pushed onto the stack | 255 \item all other parameters are pushed onto the stack (as dwords) |
256 \item aggregates (structs, unions) are pushed as a sequence of dwords, and never passed via registers (no matter their size, any respective register is skipped and not used for subsequent args) | |
257 \item varargs are always passed via the stack | |
249 \end{itemize} | 258 \end{itemize} |
250 | 259 |
251 | 260 |
252 \paragraph{Return values} | 261 \paragraph{Return values} |
253 | 262 |
254 \begin{itemize} | 263 \begin{itemize} |
255 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register. | 264 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
256 \item integers \textgreater\ 32 bits are returned via the eax and edx registers. | 265 \item integers \textgreater\ 32 and \textless=\ 64 bits are returned via the eax and edx registers |
257 \item floating point types are returned via the st0. | 266 \item aggregates (structs, unions) of any size are returned by the caller allocating the space and |
267 passing a pointer to the callee as a new, implicit first parameter (always via ecx), that same pointer is returned in eax | |
268 \item floating point types are returned via the st0 | |
258 \end{itemize} | 269 \end{itemize} |
259 | 270 |
260 | 271 |
261 \pagebreak | 272 \pagebreak |
262 | 273 |
318 \paragraph{Parameter passing} | 329 \paragraph{Parameter passing} |
319 | 330 |
320 \begin{itemize} | 331 \begin{itemize} |
321 \item stack parameter order: left-to-right | 332 \item stack parameter order: left-to-right |
322 \item called function cleans up the stack | 333 \item called function cleans up the stack |
323 \item first three integers/pointers (with exception of method pointers) (\textless=\ 32bit) are passed via eax, ecx and edx (even if preceded or interleaved by other arguments) | 334 \item first three integers/pointers (with exception of method pointers) (\textless=\ 32bit) are passed via eax, ecx and edx (preceding or interleaved arguments that are not passed via registers are pushed onto the stack) |
335 \item arguments \textgreater\ 32 bits are passed as a pointer to the value | |
336 \item aggregates (structs, unions) are pushed as a sequence of dwords, and never passed via registers (no matter their size) | |
337 \item varargs are always passed via the stack | |
324 \item all other parameters are pushed onto the stack | 338 \item all other parameters are pushed onto the stack |
339 \item the direction flag is clear on entry and must be returned clear % mention it first, above @@@ | |
325 \end{itemize} | 340 \end{itemize} |
326 | 341 |
327 | 342 |
328 \pagebreak | 343 \pagebreak |
329 | 344 |
330 \paragraph{Return values} | 345 \paragraph{Return values} |
331 | 346 |
332 \begin{itemize} | 347 \begin{itemize} |
333 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 348 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
334 \item integers \textgreater\ 32 bits are returned via the eax and edx registers | 349 \item integers and aggregates (structs, unions) \textgreater\ 32 and \textless=\ 64 bits are returned via the eax and edx registers |
335 \item floating point types are returned via the st0 register | 350 \item floating point types are returned via the st0 register |
336 \item all others (e.g. all structs, return values \textgreater\ 64 bits, ...) are returned by the caller allocating the space and | 351 \item return values \textgreater\ 32 bits (e.g. aggregates, long long, ...) are returned by the caller allocating the space and |
337 passing a pointer to the callee as a new, implicit first parameter | 352 passing a pointer to the callee as a new, implicit {\bf last} parameter |
338 \end{itemize} | 353 \end{itemize} |
339 | 354 |
340 | 355 |
341 | 356 |
342 \paragraph{Stack layout} | 357 \paragraph{Stack layout} |
396 | 411 |
397 \begin{itemize} | 412 \begin{itemize} |
398 \item stack parameter order: right-to-left | 413 \item stack parameter order: right-to-left |
399 \item called function cleans up the stack | 414 \item called function cleans up the stack |
400 \item first four integers/pointers (\textless=\ 32bit) are passed via eax, edx, ebx and ecx (even if preceded by other arguments) | 415 \item first four integers/pointers (\textless=\ 32bit) are passed via eax, edx, ebx and ecx (even if preceded by other arguments) |
401 \item arguments \textgreater 32 bits, as well as all subsequent arguments, are passed via the stack | 416 \item arguments \textgreater\ 32 bits, as well as all subsequent arguments, are passed via the stack |
417 \item aggregates (structs, unions) are passed as a pointer to the aggregate | |
402 \item all other parameters are pushed onto the stack | 418 \item all other parameters are pushed onto the stack |
403 \item varargs are always passed via the stack | |
404 \end{itemize} | 419 \end{itemize} |
405 | 420 |
406 | 421 |
407 \paragraph{Return values} | 422 \paragraph{Return values} |
408 | 423 |
409 \begin{itemize} | 424 \begin{itemize} |
410 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 425 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
411 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@ verify | 426 \item integers \textgreater\ 32 bits and \textless=\ 64 bits are returned via the eax and edx registers |
412 \item floating point types are returned via the st0 register@@@ really ? | 427 \item aggregates (structs, unions) \textless=\ 32 bits are returned in eax |
428 \item aggregates (structs, unions) \textgreater\ 32 bits are returned by the caller allocating the space and | |
429 passing a pointer to the callee via esi, that same pointer is returned in eax | |
413 \end{itemize} | 430 \end{itemize} |
414 | 431 |
415 | 432 |
416 \paragraph{Stack layout} | 433 \paragraph{Stack layout} |
417 | 434 |
467 \end{table} | 484 \end{table} |
468 | 485 |
469 \paragraph{Parameter passing} | 486 \paragraph{Parameter passing} |
470 | 487 |
471 \begin{itemize} | 488 \begin{itemize} |
472 \item Stack parameter order: right-to-left | 489 \item stack parameter order: right-to-left |
473 \item Called function cleans up the stack | 490 \item called function cleans up the stack |
474 \item All parameters are pushed onto the stack | 491 \item all parameters are pushed onto the stack (as dwords) |
475 \item Stack is usually 4 byte aligned (GCC \textgreater=\ 3.x seems to use a 16byte alignement) | 492 \item arguments \textgreater\ 64 bits are pushed as a sequence of dwords |
493 \item aggregates (structs, unions) are pushed as a sequence of dwords | |
494 \item stack is usually 4 byte aligned (GCC \textgreater=\ 3.x seems to use a 16byte alignement) | |
476 \item the direction flag is clear on entry and must be returned clear % mention it first, above @@@ | 495 \item the direction flag is clear on entry and must be returned clear % mention it first, above @@@ |
477 \end{itemize} | 496 \end{itemize} |
478 | 497 |
479 % introduce mangling section? \item Function name is decorated by prepending an underscore character and appending a '@' character and the number of bytes of stack space required | 498 % introduce mangling section? \item Function name is decorated by prepending an underscore character and appending a '@' character and the number of bytes of stack space required |
480 | 499 |
481 \paragraph{Return values} | 500 \paragraph{Return values} |
482 | 501 |
483 \begin{itemize} | 502 \begin{itemize} |
484 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 503 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
485 \item integers \textgreater\ 32 bits are returned via the eax and edx registers | 504 \item integers and aggregates (structs, unions) \textgreater\ 32 and \textless=\ 64 bits are returned via the eax and edx registers |
505 \item return values \textgreater\ 64 bits (e.g. aggregates) are returned by the caller allocating the space and | |
506 passing a pointer to the callee as a new, implicit first parameter (this means, on the stack) | |
486 \item floating point types are returned via the st0 register | 507 \item floating point types are returned via the st0 register |
487 \end{itemize} | 508 \end{itemize} |
488 | 509 |
489 | 510 |
490 \paragraph{Stack layout} | 511 \paragraph{Stack layout} |
543 | 564 |
544 \paragraph{Parameter passing} | 565 \paragraph{Parameter passing} |
545 | 566 |
546 \begin{itemize} | 567 \begin{itemize} |
547 \item stack parameter order: right-to-left | 568 \item stack parameter order: right-to-left |
548 \item called function cleans up the stack | 569 \item called function cleans up the stack (except for variadic functions where the caller cleans up) |
549 \item first parameter (this pointer) is passed via ecx | 570 \item first parameter (this pointer) is passed via ecx |
550 \item all other parameters are pushed onto the stack | 571 \item all other parameters are pushed onto the stack |
572 \item arguments \textgreater\ 64 bits are pushed as a sequence of dwords | |
573 \item aggregates (structs, unions) are pushed as a sequence of dwords | |
551 \end{itemize} | 574 \end{itemize} |
552 | 575 |
553 % introduce mangling section? \item Function name is decorated by prepending a '@' character and appending a '@' character and the number of bytes (decimal) of stack space required | 576 % introduce mangling section? \item Function name is decorated by prepending a '@' character and appending a '@' character and the number of bytes (decimal) of stack space required |
554 | 577 |
555 \paragraph{Return values} | 578 \paragraph{Return values} |
556 | 579 |
557 \begin{itemize} | 580 \begin{itemize} |
558 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 581 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
559 \item integers \textgreater\ 32 bits are returned via the eax and edx registers@@@verify | 582 \item integers \textgreater\ 32 bits and \textless=\ 64 bits are returned via the eax and edx |
560 \item floating point types are returned via the st0 register@@@ really ? | 583 \item aggregates (structs, unions) of any size are returned by the caller allocating the space and passing |
584 a pointer to the callee as a new, implicit first parameter, that same pointer is returned in eax | |
585 \item floating point types are returned via the st0 register | |
561 \end{itemize} | 586 \end{itemize} |
562 | 587 |
563 | 588 |
564 \paragraph{Stack layout} | 589 \paragraph{Stack layout} |
565 | 590 |
666 | 691 |
667 | 692 |
668 \subsubsection{pascal} | 693 \subsubsection{pascal} |
669 | 694 |
670 The best known uses of the pascal calling convention are the 16 bit OS/2 APIs, Microsoft Windows 3.x and Borland Delphi 1.x. | 695 The best known uses of the pascal calling convention are the 16 bit OS/2 APIs, Microsoft Windows 3.x and Borland Delphi 1.x. |
671 | 696 It is a variation of stdcall, however, arguments are passed from left-to-right. |
672 \paragraph{Registers and register usage} | 697 Since this calling convention is for 16-bit APIs, it is not discussed in |
673 | 698 further detail, here. |
674 \begin{table}[h] | 699 |
675 \begin{tabular*}{0.95\textwidth}{3 B} | |
676 Name & Brief description\\ | |
677 \hline | |
678 {\bf eax} & scratch, return value\\ | |
679 {\bf ebx} & preserve\\ | |
680 {\bf ecx} & scratch\\ | |
681 {\bf edx} & scratch, return value\\ | |
682 {\bf esi} & preserve\\ | |
683 {\bf edi} & preserve\\ | |
684 {\bf ebp} & preserve\\ | |
685 {\bf esp} & stack pointer\\ | |
686 {\bf st0} & scratch, floating point return value\\ | |
687 {\bf st1-st7} & scratch\\ | |
688 \end{tabular*} | |
689 \caption{Register usage on x86 pascal calling convention} | |
690 \end{table} | |
691 | |
692 \paragraph{Parameter passing} | |
693 | |
694 \begin{itemize} | |
695 \item stack parameter order: left-to-right | |
696 \item called function cleans up the stack | |
697 \item all parameters are pushed onto the stack | |
698 \end{itemize} | |
699 | |
700 | |
701 \paragraph{Return values} | |
702 | |
703 \begin{itemize} | |
704 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | |
705 \item integers \textgreater\ 32 bits are returned via the eax and edx registers | |
706 \item floating point types are returned via the st0 register | |
707 \end{itemize} | |
708 | |
709 | |
710 \paragraph{Stack layout} | |
711 | |
712 Stack directly after function prolog:\\ | |
713 | |
714 \begin{figure}[h] | |
715 \begin{tabular}{5|3|1 1} | |
716 & \vdots & & \\ | |
717 \hhline{~=~~} | |
718 register save area & \hspace{4cm} & & \mrrbrace{6}{caller's frame} \\ | |
719 \hhline{~-~~} | |
720 local data & & & \\ | |
721 \hhline{~-~~} | |
722 \mrlbrace{3}{parameter area} & arg 0 & \mrrbrace{3}{stack parameters} & \\ | |
723 & \ldots & & \\ | |
724 & arg n-1 & & \\ | |
725 \hhline{~-~~} | |
726 & return address & & \\ | |
727 \hhline{~=~~} | |
728 register save area & & & \mrrbrace{4}{current frame} \\ | |
729 \hhline{~-~~} | |
730 local data & & & \\ | |
731 \hhline{~-~~} | |
732 parameter area & & & \\ | |
733 \hhline{~-~~} | |
734 & \vdots & & \\ | |
735 \end{tabular} | |
736 \caption{Stack layout on x86 pascal calling convention} | |
737 \end{figure} | |
738 | 700 |
739 | 701 |
740 \clearpage | 702 \clearpage |
741 | 703 |
742 \subsubsection{plan9call} | 704 \subsubsection{plan9call} |
765 | 727 |
766 \begin{itemize} | 728 \begin{itemize} |
767 \item stack parameter order: right-to-left | 729 \item stack parameter order: right-to-left |
768 \item caller cleans up the stack | 730 \item caller cleans up the stack |
769 \item all parameters are pushed onto the stack | 731 \item all parameters are pushed onto the stack |
732 \item all parameters are pushed onto the stack (as dwords) | |
733 \item arguments \textgreater\ 64 bits are pushed as a sequence of dwords | |
734 \item aggregates (structs, unions) are pushed as a sequence of dwords | |
770 \end{itemize} | 735 \end{itemize} |
771 | 736 |
772 \pagebreak | 737 \pagebreak |
773 | 738 |
774 \paragraph{Return values} | 739 \paragraph{Return values} |
775 | 740 |
776 \begin{itemize} | 741 \begin{itemize} |
777 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register | 742 \item return values of pointer or integral type (\textless=\ 32 bits) are returned via the eax register |
778 \item integers \textgreater\ 32 bits or structures are returned by the caller allocating the space and | 743 \item integers \textgreater\ 32 bits and aggregates (structs, unions) of any size are returned by the caller allocating |
779 passing a pointer to the callee as a new, implicit first parameter (this means, on the stack) | 744 the space and passing a pointer to the callee as a new, implicit first parameter, that same pointer is returned in eax |
780 \item floating point types are returned via the st0 register (called F0 in plan9 8a's terms) | 745 \item floating point types are returned via the st0 register (called F0 in plan9 8a's terms) |
781 \end{itemize} | 746 \end{itemize} |
782 | 747 |
783 | 748 |
784 \paragraph{Stack layout} | 749 \paragraph{Stack layout} |