Imported Upstream version 2.9.0
[debian/cc1111] / doc / z80 / combined_i186_z80_design.tex
1 % Combined i186/Z80 design
2 \documentclass{article}
3
4 % Too long to type :)
5 \newcommand{\tosh}{Toshiba TLCS-900H}
6
7 \begin{document}
8 \title{Combined i186/Z80 backend design}
9 \author{Michael Hope michaelh@earthling.net.nz}
10 \date{\today}
11 \maketitle
12
13 \begin{abstract}
14 There is much similarity between the Zilog Z80, Nintendo GBZ80, Intel
15 i186, and \tosh processors.  This document describes the design of a
16 backend consisting of a register allocator and set of extendable code
17 generators for SDCC which can target all of these processors.
18 \end{abstract}
19
20 \section{Motivation}
21 The primary motivation is to add a i186 backend to SDCC, and in the
22 process come to understand register allocation.  The secondary goal is
23 to attempt to combine the common parts from the Z80 and i186 backends
24 to make both easier to maintain.  In the 'would be nice' category is
25 adding support for the \tosh.
26
27 \section{Processor descriptions}
28
29 \subsection{Zilog Z80}
30 \begin{tabular}{l|l|l}
31         Name    & Parts & Notes \\ \hline
32         AF      & A     & Accumulator and flags.  Unusable as a pair. \\ \hline
33         BC      & B, C  & \\ \hline
34         DE      & D, E  & \\ \hline
35         HL      & H, L  & \\ \hline
36         IX      & None  & Index register. \\ \hline
37         IY      & None  & Index register. \\
38 \end{tabular}
39
40 The Z80 also has a switchable alternate register set AF', BC', DE',
41 and HL' which are not accessible directly.  It is assumed that it is
42 too hard to track these to make it worthwhile.  IX and IY can be split
43 at the byte level by using undocumented instructions.  While this
44 would make them more usable as general purpose registers, it is
45 ignored for compatibility.
46
47 \subsection{Nintendo GBZ80}
48 The GBZ80 is basically a Z80 less the index registers and the
49 alternate register set. \\
50 \begin{tabular}{l|l|l}
51         Name    & Parts & Notes \\ \hline
52         AF      & A     & Accumulator and flags.  Unusable as a pair. \\ \hline
53         BC      & B, C  & \\ \hline
54         DE      & D, E  & \\ \hline
55         HL      & H, L  & \\
56 \end{tabular}
57
58 \subsection{Intel i186}
59 \begin{tabular}{l|l|l}
60         Name    & Parts         & Notes \\ \hline
61         AX      & AH, AL        & Accumulator. \\ \hline
62         BX      & BH, BL        & \\ \hline
63         CX      & CH, CL        & \\ \hline
64         DX      & DH, DL        & \\ \hline
65         DI      & None          & Destination Index. \\ \hline
66         SI      & None          & Source Index. \\ \hline
67         BP      & None          & Base pointer. \\
68 \end{tabular}
69
70 Note that the segment registers CS, DS, ES, and SS are not listed.
71 For simplicity only tiny mode is supported.  Tiny mode is where both
72 the data and code exist in one segment, such that CS = DS.  This
73 allows constant data stored in the code segment to be accessed in the
74 same method as data.  This may cause trouble later if far mode is
75 needed.
76
77 \subsection{\tosh}
78 The 900H seems to be inspired by the Z80.  It is listed as a 16 bit
79 device, but most of the registers are 32 bits wide. \\
80 \begin{tabular}{l|l|l}
81         Name    & Parts         & Notes \\ \hline
82         XWA     & WA, W, A      & Accumulator \\ \hline
83         XBC     & BC, B, C      & \\ \hline
84         XDE     & DE, D, E      & \\ \hline
85         XHL     & HL, H, L      & \\ \hline
86         XIX     & IX            & \\ \hline
87         XIY     & IY            & \\ \hline
88         XIZ     & IZ            & \\
89 \end{tabular}
90
91 All registers can act as either an index base or an index offset.  The
92 offset is limited to sixteen bits.  Apparently XIX, XIY, and XIZ can
93 be split at the byte level.  For simplicity this is ignored.
94
95 \section{Common features}
96 \subsection{Stack}
97 The stack grows downwards.  All push operations are pre-decrement.
98 All but the GBZ80 have a base pointer register that can be used along
99 with an offset to access local variables and parameters.  The GBZ80 and
100 Z80 both have problems with more than 127 bytes of local variables due
101 to their offset being a INT8.
102
103 \subsection{Registers}
104 All contain a reasonable but small number of registers.  All have some
105 special purpose registers which can either be handled separately or
106 ignored.  All general purpose registers can be split at the byte and
107 word level, but doing so may make the rest of the register
108 unavailable.
109
110 \subsection{Memory}
111 All have a 64k address space.  However this should not be assumed to make
112 far support easier later.
113
114 \section{Design}
115 \subsection{Basics}
116 The design is mainly stack based, such that all local variables and
117 parameters go onto the stack.  Compare with the mcs51 backend which
118 overlays variables onto a shared static area.
119
120 All stack access goes through the base pointer register (BP).  The
121 stack frame consists of the parameters pushed right to left, the
122 return context, and then local variables.  SP points to the base of
123 the local variables.  BP points to the bottom of the return context
124 and hence to just above the top of the local variables.  Note that as
125 the stack grows down the parameters appear with the left most
126 parameter at the lowest address.
127
128 A scratch register will be available for any sub operation to use and
129 will be valid only within that sub operation.  The accumulator is also
130 unavailable.  Return values are normally returned in the scratch
131 register and accumulator.
132
133 \begin{tabular}{l|l|l|l|l}
134         Name            & i186    & Z80     & GBZ80  & 900H     \\ \hline
135         Base pointer    & BP      & IX      & HL     & XIX      \\ \hline
136         Scratch         & BX      & HL      & DE     & XHL      \\ \hline
137         Return          & BX, AX  & HL, IY  & DE, HL & XHL      \\ \hline
138         Available       & CX, DX  & BC, DE  & BC     & XBC, XDE \\ \hline
139         Ignored         & SI, DI  & IY      & None   & XIY, XIZ \\
140 \end{tabular}
141
142 \subsection{Register allocator}
143 The current Z80 and mcs51 register allocators perform these steps:
144 \begin{enumerate}
145         \item Pack each basic block by removing straight assignments and marking
146 remat. iTemps.
147         \item Set the number of registers required for each live range based on
148 the type and size of the live range.
149         \item Assign registers to each segment by deassigning registers and stack
150 locations for any just expired iTemps, skipping any instructions which don't need
151 registers, and spilling and assigning registers to the result of this tuple.
152         \item Create the register mask for this segment.
153 \end{enumerate}
154
155 Optimisations include assigning into the accumulator or the scratch
156 register where possible.  This requires knowledge of what the code
157 generator touches for a given instruction.
158
159 The first generation register allocator will only pack assignments and mark
160 remat. variables.  Only the register management is processor specific.  The
161 allocator may ask for a given size register or if a given size register is
162 available.  Note that only whole registers may be returned.  For example, 
163 allocation will fail if a sixteen bit register is requested and no pair
164 is available, even two eight bit registers are available.  Note that on
165 the Z80, GBZ80, and i186 a request for a 32 bit register will always fail.
166
167 \subsection{Code generator}
168 The possible operations are:
169 \begin{itemize}
170         \item NOT - Logical not.  0 -> 1, others -> 0.
171         \item CPL - Bitwise complement.
172         \item UMINUS - Unary minus.  result = 0 - left.
173         \item IPUSH - Push immediate onto the stack.
174         \item CALL - Call a function.
175         \item PCALL - Call via pointer.
176         \item FUNCTION - Emit the function prelude.
177         \item ENDFUNCTION - Emit the function prologue.
178         \item RET - Load the return value and jump to end of function.
179         \item LABEL - Generate a local label.
180         \item GOTO - Jump to a local label.
181         \item Arithmitic - +, -, *, /, \%.
182         \item Comparison - LT, GT, LEQ, GEQ, !=, =.
183         \item Logical - \&\&, ||
184         \item Binary - AND, OR, XOR.
185         \item Shift - RRC, RLC, LSR, LSL.
186         \item Pointer - Set and Get.
187         \item Assign.
188         \item IF jump.
189         \item Misc - Jump table, cast, address of.
190 \end{itemize}
191
192 \end{document}