Mercurial > pub > dyncall > dyncall
annotate doc/manual/callconvs/callconv_mips.tex @ 117:4a64b733dc76
- doc, added first version of mips64 callconv page
- cleanups, todo update
author | cslag |
---|---|
date | Sun, 03 Jul 2016 01:39:37 +0200 |
parents | 6da2a7ee2a86 |
children | 5675d34f0a06 |
rev | line source |
---|---|
0 | 1 %////////////////////////////////////////////////////////////////////////////// |
2 % | |
3 % Copyright (c) 2007,2009 Daniel Adler <dadler@uni-goettingen.de>, | |
4 % Tassilo Philipp <tphilipp@potion-studios.com> | |
5 % | |
6 % Permission to use, copy, modify, and distribute this software for any | |
7 % purpose with or without fee is hereby granted, provided that the above | |
8 % copyright notice and this permission notice appear in all copies. | |
9 % | |
10 % THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 % WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 % MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 % ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 % WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 % | |
18 %////////////////////////////////////////////////////////////////////////////// | |
19 | |
117 | 20 \subsection{MIPS32 Calling Convention} |
0 | 21 |
22 \paragraph{Overview} | |
23 | |
24 Multiple revisions of the MIPS Instruction set exist, namely MIPS I, MIPS II, MIPS III, MIPS IV, MIPS32 and MIPS64. | |
117 | 25 Nowadays, MIPS32 and MIPS64 are the main ones used for 32-bit and 64-bit instruction sets, respectively.\\ |
26 Given MIPS processor are often used for embedded devices, several add-on extensions exist for the MIPS family, for example: | |
0 | 27 |
28 \begin{description} | |
29 \item [MIPS-3D] simple floating-point SIMD instructions dedicated to common 3D tasks. | |
30 \item [MDMX] (MaDMaX) more extensive integer SIMD instruction set using 64 bit floating-point registers. | |
31 \item [MIPS16e] adds compression to the instruction stream to make programs take up less room (allegedly a response to the THUMB instruction set of the ARM architecture). | |
32 \item [MIPS MT] multithreading additions to the system similar to HyperThreading. | |
33 \end{description} | |
34 | |
117 | 35 Unfortunately, there is actually no such thing as "The MIPS Calling Convention". Many possible conventions are used |
36 by many different environments such as \emph{O32}\cite{MIPSo32}, \emph{O64}\cite{MIPSo64}, \emph{N32}\cite{MIPSn32/n64}, \emph{N64}\cite{MIPSn32/n64}, \emph{EABI}\cite{MIPSeabi} and \emph{NUBI}\cite{MIPSnubi}.\\ | |
0 | 37 |
38 \paragraph{\product{dyncall} support} | |
39 | |
117 | 40 Currently, dyncall supports for MIPS 32-bit architectures the widely-used O32 calling convention, as well as EABI (which is used on the Homebrew SDK for |
41 the Playstation Portable). \product{dyncall} currently does not support MIPS16e (contrary to the like-minded ARM-THUMB, which is supported). Both, calls and callbacks are supported. | |
0 | 42 |
43 \subsubsection{MIPS EABI 32-bit Calling Convention} | |
44 | |
45 \paragraph{Register usage} | |
46 | |
47 \begin{table}[h] | |
77 | 48 \begin{tabular*}{0.95\textwidth}{lll} |
108 | 49 Name & Alias & Brief description\\ |
50 \hline | |
51 {\bf \$0} & {\bf \$zero} & Hardware zero \\ | |
52 {\bf \$1} & {\bf \$at} & Assembler temporary \\ | |
53 {\bf \$2-\$3} & {\bf \$v0-\$v1} & Integer results \\ | |
110 | 54 {\bf \$4-\$11} & {\bf \$a0-\$a7} & Integer arguments, or double precision float arguments\\ |
108 | 55 {\bf \$12-\$15,\$24} & {\bf \$t4-\$t7,\$t8} & Integer temporaries \\ |
56 {\bf \$25} & {\bf \$t9} & Integer temporary, hold the address of the called function for all PIC calls (by convention) \\ | |
57 {\bf \$16-\$23} & {\bf \$s0-\$s7} & Preserved \\ | |
58 {\bf \$26,\$27} & {\bf \$kt0,\$kt1} & Reserved for kernel \\ | |
59 {\bf \$28} & {\bf \$gp} & Global pointer, preserve \\ | |
60 {\bf \$29} & {\bf \$sp} & Stack pointer, preserve \\ | |
61 {\bf \$30} & {\bf \$s8} & Frame pointer, preserve \\ | |
62 {\bf \$31} & {\bf \$ra} & Return address, preserve \\ | |
63 {\bf hi, lo} & & Multiply/divide special registers \\ | |
64 {\bf \$f0,\$f2} & & Float results \\ | |
65 {\bf \$f1,\$f3,\$f4-\$f11,\$f20-\$f23} & & Float temporaries \\ | |
117 | 66 {\bf \$f12-\$f19} & & Single precision float arguments \\ |
76 | 67 \end{tabular*} |
98 | 68 \caption{Register usage on MIPS32 EABI calling convention} |
0 | 69 \end{table} |
70 | |
71 \paragraph{Parameter passing} | |
72 | |
73 \begin{itemize} | |
98 | 74 \item Stack grows down |
0 | 75 \item Stack parameter order: right-to-left |
76 \item Caller cleans up the stack | |
110 | 77 \item first 8 integers (<= 32bit) are passed in registers \$a0-\$a7 |
78 \item first 8 single precision floating point arguments are passed in registers \$f12-\$f19 | |
108 | 79 \item if either integer or float registers are used up, the stack is used |
111 | 80 \item 64-bit stack arguments are always aligned to 8 bytes |
110 | 81 \item 64-bit integers or double precision floats are passed on two general purpose registers starting at an even register number, skipping one odd register |
82 \item \$a0-\$a7 and \$f12-\$f19 are not required to be preserved | |
83 \item results are returned in \$v0 (32-bit), \$v0 and \$v1 (64-bit), \$f0 or \$f0 and \$f2 (2 $\times$ 32 bit float e.g. complex) | |
0 | 84 \end{itemize} |
85 | |
86 \paragraph{Stack layout} | |
87 | |
88 Stack directly after function prolog:\\ | |
89 | |
90 \begin{figure}[h] | |
91 \begin{tabular}{5|3|1 1} | |
92 \hhline{~-~~} | |
92 | 93 & \vdots & & \\ |
94 \hhline{~=~~} | |
95 register save area & \hspace{4cm} & & \mrrbrace{5}{caller's frame} \\ | |
96 \hhline{~-~~} | |
117 | 97 local data & & & \\ |
92 | 98 \hhline{~-~~} |
99 \mrlbrace{3}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ | |
100 & \ldots & & \\ | |
101 & \ldots & & \\ | |
0 | 102 \hhline{~=~~} |
92 | 103 register save area (with return address) & & & \mrrbrace{5}{current frame} \\ |
0 | 104 \hhline{~-~~} |
92 | 105 local data & & & \\ |
0 | 106 \hhline{~-~~} |
92 | 107 parameter area & & & \\ |
0 | 108 \hhline{~-~~} |
92 | 109 & \vdots & & \\ |
0 | 110 \hhline{~-~~} |
111 \end{tabular} | |
112 \caption{Stack layout on mips32 eabi calling convention} | |
113 \end{figure} | |
114 | |
98 | 115 \newpage |
116 | |
95 | 117 \subsubsection{MIPS O32 32-bit Calling Convention} |
118 | |
98 | 119 \paragraph{Register usage} |
120 | |
121 \begin{table}[h] | |
122 \begin{tabular*}{0.95\textwidth}{lll} | |
108 | 123 Name & Alias & Brief description\\ |
98 | 124 \hline |
108 | 125 {\bf \$0} & {\bf \$zero} & hardware zero \\ |
126 {\bf \$1} & {\bf \$at} & assembler temporary \\ | |
127 {\bf \$2-\$3} & {\bf \$v0-\$v1} & return value, scratch \\ | |
128 {\bf \$4-\$7} & {\bf \$a0-\$a3} & first integer arguments, scratch\\ | |
129 {\bf \$8-\$15,\$24} & {\bf \$t0-\$t7,\$t8} & temporaries, scratch \\ | |
130 {\bf \$25} & {\bf \$t9} & temporary, hold the address of the called function for all PIC calls (by convention) \\ | |
131 {\bf \$16-\$23} & {\bf \$s0-\$s7} & preserved \\ | |
132 {\bf \$26,\$27} & {\bf \$k0,\$k1} & reserved for kernel \\ | |
117 | 133 {\bf \$28} & {\bf \$gp} & global pointer, preserved by caller \\ |
108 | 134 {\bf \$29} & {\bf \$sp} & stack pointer, preserve \\ |
135 {\bf \$30} & {\bf \$fp} & frame pointer, preserve \\ | |
136 {\bf \$31} & {\bf \$ra} & return address, preserve \\ | |
137 {\bf hi, lo} & & multiply/divide special registers \\ | |
138 {\bf \$f0-\$f3} & & float return value, scratch \\ | |
139 {\bf \$f4-\$f11,\$f16-\$f19} & & float temporaries, scratch \\ | |
140 {\bf \$f12-\$f15} & & first floating point arguments, scratch \\ | |
141 {\bf \$f20-\$f31} & & preserved \\ | |
98 | 142 \end{tabular*} |
143 \caption{Register usage on MIPS O32 calling convention} | |
144 \end{table} | |
145 | |
146 \paragraph{Parameter passing} | |
147 | |
148 \begin{itemize} | |
149 \item Stack grows down | |
150 \item Stack parameter order: right-to-left | |
151 \item Caller cleans up the stack | |
108 | 152 \item Caller is required to always leave a 16-byte spill area for\$a0-\$a3 at the and of {\bf its} frame, to be used and spilled to by the callee, if needed |
104
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
153 \item The different stack areas (local data, register save area, parameter area) are each aligned to 8 bytes. |
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
154 \item generally, first four 32bit arguments are passed in registers \$a0-\$a3, respectively (see below for exceptions if first arg is a float) |
98 | 155 \item subsequent parameters are passed vie the stack |
104
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
156 \item 64-bit params passed via registers are passed using either two registers (starting at an even register number, skipping an odd one if necessary), or via the stack using an 8-byte alignment |
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
157 \item if the very first call argument is a float, up to 2 floats or doubles can be passed via \$f12 and \$f14, respectively, for first and second argument |
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
158 \item if any arguments are passed via float registers, skip \$a0-\$a3 for subsequent arguments as if the values were passed via them |
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
159 \item note that if the first argument is not a float, but the second, it'll get passed via the \$a? registers |
dbca6763f2be
- complete, working mips o32 callback (using hardware fp); fixes error from last commit, which ignored first 2 float args
cslag
parents:
101
diff
changeset
|
160 \item results are returned in \$v0 (32-bit int return values), \$f0 (32-bit float), \$v0 and \$v1 (64-bit int), \$f0 and \$f3 (64bit float) |
98 | 161 \end{itemize} |
162 | |
163 \paragraph{Stack layout} | |
164 | |
165 Stack directly after function prolog:\\ | |
166 | |
167 \begin{figure}[h] | |
168 \begin{tabular}{5|3|1 1} | |
169 \hhline{~-~~} | |
117 | 170 & \vdots & & \\ |
98 | 171 \hhline{~=~~} |
172 local data & \hspace{4cm} & & \mrrbrace{12}{caller's frame} \\ | |
173 \hhline{~-~~} | |
117 | 174 register save area & return address & & \\ |
175 & s7 & & \\ | |
176 & \vdots & & \\ | |
177 & s0 & & \\ | |
98 | 178 \hhline{~-~~} |
117 | 179 \mrlbrace{7}{parameter area} & \ldots & \mrrbrace{3}{stack parameters} & \\ |
180 & \ldots & & \\ | |
181 & \ldots & & \\ | |
182 & a3 & \mrrbrace{4}{spill area} & \\ | |
183 & a2 & & \\ | |
184 & a1 & & \\ | |
185 & a0 & & \\ | |
98 | 186 \hhline{~=~~} |
117 | 187 local data & & & \mrrbrace{5}{current frame} \\ |
98 | 188 \hhline{~-~~} |
117 | 189 register save area (with return address) & & & \\ |
98 | 190 \hhline{~-~~} |
117 | 191 parameter area & & & \\ |
192 & \vdots & & \\ | |
98 | 193 \hhline{~-~~} |
194 \end{tabular} | |
195 \caption{Stack layout on MIPS O32 calling convention} | |
196 \end{figure} | |
197 | |
198 \newpage | |
95 | 199 |
200 \subsubsection{MIPS N32 32-bit Calling Convention} | |
201 | |
202 @@@ | |
203 |