c96cec673bc3ef8213420140ba65dd0ce2fbfdd1
[cacao.git] / doc / handbook / x86_64.tex
1 \section{AMD64 (x86\_64) code generator}
2
3 \subsection{Introduction}
4
5 The AMD64 architecture, formerly know as x86\_64, is an improvement of
6 the Intel IA32 architecture by AMD -- Advanced Micro Devices. The
7 extraordinary success of the IA32 architecture and the upcoming memory
8 address space problem on IA32 high end servers, led to a special
9 design decision. Unlike Intel, with it's completely new designed IA64
10 architecture, AMD decided to extend the IA32 instruction set with
11 new 64-bit instructions.
12
13 Due to the fact that the IA32 instructions have no fixed length, as
14 this is the fact on RISC machines, it was easy for them to introduce a
15 new \textit{prefix byte} called \texttt{REX}. The \textit{REX prefix}
16 enables the 64-bit operation mode of the following instruction in the
17 new \textit{64-bit mode} of the processor.
18
19 A processor of the AMD64 architecture has two main operating modes:
20
21 \begin{itemize}
22 \item Long Mode
23 \item Legacy Mode
24 \end{itemize}
25
26 In the \textit{Legacy Mode} the processor acts like an IA32
27 processor. Any 32-bit operating system or software can be run on these
28 type of processors without changes, so companies running IA32 servers
29 and software can change their hardware to AMD64 and their systems are
30 still operational. This was the main intention for AMD to develop this
31 architecture. Furthermore the \textit{Long Mode} is split into two
32 coexistent operating modes:
33
34 \begin{itemize}
35 \item 64-bit Mode
36 \item Compatibility Mode
37 \end{itemize}
38
39 The \textit{64-bit Mode} exposes the power of this architecture. Any
40 memory operation now uses 64-bit addresses and ALU instructions can
41 operate on 64-bit operands. Within \textit{Compatibility Mode} any
42 IA32 software can be run under the control of 64-bit operation
43 system. This, as mentioned before, is yet another point for companies
44 to change their hardware to AMD64. So their software can be slowly
45 migrated to the new 64-bit system, but not every type of software is
46 faster in 64-bit code.
47
48 Another crucial pointer to make the AMD64 architecture faster than
49 IA32, is the limited number of registers. Any IA32 architecture, from
50 the early \textit{i386} to the newest generation of \textit{Intel
51 Pentium 4} or \textit{AMD Athlon}, has only 8 general purpose
52 registers. With the \textit{REX prefix}, AMD has the ability to
53 increase the amount of accessible registers by 1 bit. This means in
54 \textit{64-bit Mode} 16 general purpose registers are available. The
55 value of a \textit{REX prefix} is in the range \texttt{40h} through
56 \texttt{4Fh}, depending on the particular bits used (see table
57 \ref{REX}).
58
59 \begin{table}
60 \begin{center}
61 \begin{tabular}[b]{|c|c|l|}
62 \hline
63 Mnemonic & Bit Position & Definition \\ \hline
64 -        & 7-4          & 0100 \\ \hline
65 REX.W    & 3            & 0 = Default operand size \\
66          &              & 1 = 64-bit operand size \\ \hline
67 REX.R    & 2            & 1-bit (high) extension of the ModRM \textit{reg} field, \\
68          &              & thus permitting access to 16 registers. \\ \hline
69 REX.X    & 1            & 1-bit (high) extension of the SIB \textit{index} field, \\
70          &              & thus permitting access to 16 registers. \\ \hline
71 REX.B    & 0            & 1-bit (high) extension of the ModRM \textit{r/m} field, \\
72          &              & SIB \textit{base} field, or opcode \textit{reg} field, thus \\
73          &              & permitting access to 16 registers. \\ \hline
74 \end{tabular}
75 \caption{REX Prefix Byte Fields}
76 \label{REX}
77 \end{center}
78 \end{table}
79
80
81 \subsection{Code generation}
82
83 AMD64 code generation is mostly the same as on IA32. All new 64-bit
84 instructions can handle both \textit{memory operands} and
85 \textit{register operands}, so there is no need to change the
86 implementation of the IA32 ICMDs.
87
88 Much better code generation can be achieved in the area of
89 \textit{long arithmetic}. Since all 16 general purpose registers can
90 hold 64-bit integer values, there is no need for special long
91 handling, like on IA32 were we stored all long varibales in memory. A
92 simple \texttt{ICMD\_LADD} was on IA32 (best case shown for AMD64 ---
93 \texttt{src->regoff == iptr->dst->regoff}):
94
95 \begin{verbatim}
96 i386_mov_membase_reg(REG_SP, src->prev->regoff * 8, REG_ITMP1);
97 i386_alu_reg_membase(I386_ADD, REG_ITMP1, REG_SP, iptr->dst->regoff * 8);
98 i386_mov_membase_reg(REG_SP, src->prev->regoff * 8 + 4, REG_ITMP1);
99 i386_alu_reg_membase(I386_ADC, REG_ITMP1, REG_SP, iptr->dst->regoff * 8 + 4);
100 \end{verbatim}
101
102 First memory operand is added to second memory operand which is at the
103 same stack location as the destination operand. This are four
104 instructions executed for one addition. If we would use registers for
105 long variables we could get a \textit{best-case} of two instructions,
106 namely \textit{add} followed by a \textit{adc}. On AMD64 we can
107 generate one instruction for this addition:
108
109 \begin{verbatim}
110 x86_64_alu_reg_reg(X86_64_ADD, src->prev->regoff, iptr->dst->regoff);
111 \end{verbatim}
112
113 This means, the AMD64 port is \textit{four-times} faster than the IA32
114 port (maybe even more, because we do not use memory accesses). Even if
115 we would implement the usage of registers for long variables on IA32,
116 the AMD64 port would be at least twice as fast.
117
118 To be able to use the new 64-bit instructions, we need to prefix
119 nearly all instructions --- some instructions can be used in 64-bit
120 mode without escaping --- with the mentioned \textit{REX prefix}
121 byte. In CACAO we use a macro called
122
123 \begin{verbatim}
124 x86_64_emit_rex(size,reg,index,rm)
125 \end{verbatim}
126
127 The names of the arguments are respective to their use in the
128 \textit{REX prefix} (see table \ref{REX}).