]> git.sur5r.net Git - cc65/blob - doc/coding.sgml
remote TABs in doc/ and test/
[cc65] / doc / coding.sgml
1 <!doctype linuxdoc system>
2
3 <article>
4 <title>cc65 coding hints
5 <author><url url="mailto:uz@cc65.org" name="Ullrich von Bassewitz">
6
7 <abstract>
8 How to generate the most effective code with cc65.
9 </abstract>
10
11
12
13 <sect>Use prototypes<p>
14
15 This will not only help to find errors between separate modules, it will also
16 generate better code, since the compiler must not assume that a variable sized
17 parameter list is in place and must not pass the argument count to the called
18 function. This will lead to shorter and faster code.
19
20
21
22 <sect>Don't declare auto variables in nested function blocks<p>
23
24 Variable declarations in nested blocks are usually a good thing. But with
25 cc65, there is a drawback: Since the compiler generates code in one pass, it
26 must create the variables on the stack each time the block is entered and
27 destroy them when the block is left. This causes a speed penalty and larger
28 code.
29
30
31
32 <sect>Remember that the compiler does no high level optimizations<p>
33
34 The compiler needs hints from you about the code to generate. It will try to
35 optimize the generated code, but follow the outline you gave in your C
36 program. So for example, when accessing indexed data structures, get a pointer
37 to the element and use this pointer instead of calculating the index again and
38 again. If you want to have your loops unrolled, or loop invariant code moved
39 outside the loop, you have to do that yourself.
40
41
42
43 <sect>Longs are slow!<p>
44
45 While long support is necessary for some things, it's really, really slow on
46 the 6502. Remember that any long variable will use 4 bytes of memory, and any
47 operation works on double the data compared to an int.
48
49
50
51 <sect>Use unsigned types wherever possible<p>
52
53 The 6502 CPU has no opcodes to handle signed values greater than 8 bit. So
54 sign extension, test of signedness etc. has to be done with extra code. As a
55 consequence, the code to handle signed operations is usually a bit larger and
56 slower than the same code for unsigned types.
57
58
59
60 <sect>Use chars instead of ints if possible<p>
61
62 While in arithmetic operations, chars are immidiately promoted to ints, they
63 are passed as chars in parameter lists and are accessed as chars in variables.
64 The code generated is usually not much smaller, but it is faster, since
65 accessing chars is faster. For several operations, the generated code may be
66 better if intermediate results that are known not to be larger than 8 bit are
67 casted to chars.
68
69 You should especially use unsigned chars for loop control variables if the
70 loop is known not to execute more than 255 times.
71
72
73
74 <sect>Make the size of your array elements one of 1, 2, 4, 8<p>
75
76 When indexing into an array, the compiler has to calculate the byte offset
77 into the array, which is the index multiplied by the size of one element. When
78 doing the multiplication, the compiler will do a strength reduction, that is,
79 replace the multiplication by a shift if possible. For the values 2, 4 and 8,
80 there are even more specialized subroutines available. So, array access is
81 fastest when using one of these sizes.
82
83
84
85 <sect>Expressions are evaluated from left to right<p>
86
87 Since cc65 is not building an explicit expression tree when parsing an
88 expression, constant subexpressions may not be detected and optimized properly
89 if you don't help. Look at this example:
90
91 <tscreen><verb>
92       #define OFFS   4
93       int  i;
94       i = i + OFFS + 3;
95 </verb></tscreen>
96
97 The expression is parsed from left to right, that means, the compiler sees 'i',
98 and puts it contents into the secondary register. Next is OFFS, which is
99 constant. The compiler emits code to add a constant to the secondary register.
100 Same thing again for the constant 3. So the code produced contains a fetch
101 of 'i', two additions of constants, and a store (into 'i'). Unfortunately, the
102 compiler does not see, that "OFFS + 3" is a constant for itself, since it does
103 its evaluation from left to right. There are some ways to help the compiler
104 to recognize expression like this:
105
106 <enum>
107
108 <item>Write "i = OFFS + 3 + i;". Since the first and second operand are
109 constant, the compiler will evaluate them at compile time reducing the code to
110 a fetch, one addition (secondary + constant) and one store.
111
112 <item>Write "i = i + (OFFS + 3)". When seeing the opening parenthesis, the
113 compiler will start a new expression evaluation for the stuff in the braces,
114 and since all operands in the subexpression are constant, it will detect this
115 and reduce the code to one fetch, one addition and one store.
116
117 </enum>
118
119
120 <sect>Use the preincrement and predecrement operators<p>
121
122 The compiler is not always smart enough to figure out, if the rvalue of an
123 increment is used or not. So it has to save and restore that value when
124 producing code for the postincrement and postdecrement operators, even if this
125 value is never used. To avoid the additional overhead, use the preincrement
126 and predecrement operators if you don't need the resulting value. That means,
127 use
128
129 <tscreen><verb>
130         ...
131         ++i;
132         ...
133 </verb></tscreen>
134
135     instead of
136
137 <tscreen><verb>
138         ...
139         i++;
140         ...
141 </verb></tscreen>
142
143
144
145 <sect>Use constants to access absolute memory locations<p>
146
147 The compiler produces optimized code, if the value of a pointer is a constant.
148 So, to access direct memory locations, use
149
150 <tscreen><verb>
151         #define VDC_STATUS 0xD601
152         *(char*)VDC_STATUS = 0x01;
153 </verb></tscreen>
154
155 That will be translated to
156
157 <tscreen><verb>
158         lda     #$01
159         sta     $D601
160 </verb></tscreen>
161
162 The constant value detection works also for struct pointers and arrays, if the
163 subscript is a constant. So
164
165 <tscreen><verb>
166         #define VDC     ((unsigned char*)0xD600)
167         #define STATUS  0x01
168         VDC[STATUS] = 0x01;
169 </verb></tscreen>
170
171 will also work.
172
173 If you first load the constant into a variable and use that variable to access
174 an absolute memory location, the generated code will be much slower, since the
175 compiler does not know anything about the contents of the variable.
176
177
178
179 <sect>Use initialized local variables<p>
180
181 Initialization of local variables when declaring them gives shorter and faster
182 code. So, use
183
184 <tscreen><verb>
185         int i = 1;
186 </verb></tscreen>
187
188 instead of
189
190 <tscreen><verb>
191         int i;
192         i = 1;
193 </verb></tscreen>
194
195 But beware: To maximize your savings, don't mix uninitialized and initialized
196 variables. Create one block of initialized variables and one of uniniitalized
197 ones. The reason for this is, that the compiler will sum up the space needed
198 for uninitialized variables as long as possible, and then allocate the space
199 once for all these variables. If you mix uninitialized and initialized
200 variables, you force the compiler to allocate space for the uninitialized
201 variables each time, it parses an initialized one. So do this:
202
203 <tscreen><verb>
204         int i, j;
205         int a = 3;
206         int b = 0;
207 </verb></tscreen>
208
209 instead of
210
211 <tscreen><verb>
212         int i;
213         int a = 3;
214         int j;
215         int b = 0;
216 </verb></tscreen>
217
218 The latter will work, but will create larger and slower code.
219
220
221
222 <sect>Use the array operator &lsqb;&rsqb; even for pointers<p>
223
224 When addressing an array via a pointer, don't use the plus and dereference
225 operators, but the array operator. This will generate better code in some
226 common cases.
227
228 Don't use
229
230 <tscreen><verb>
231         char* a;
232         char b, c;
233         char b = *(a + c);
234 </verb></tscreen>
235
236 Use
237
238 <tscreen><verb>
239         char* a;
240         char b, c;
241         char b = a[c];
242 </verb></tscreen>
243
244 instead.
245
246
247
248 <sect>Use register variables with care<p>
249
250 Register variables may give faster and shorter code, but they do also have an
251 overhead. Register variables are actually zero page locations, so using them
252 saves roughly one cycle per access. The calling routine may also use register
253 variables, so the old values have to be saved on function entry and restored
254 on exit. Saving an d restoring has an overhead of about 70 cycles per 2 byte
255 variable. It is easy to see, that - apart from the additional code that is
256 needed to save and restore the values - you need to make heavy use of a
257 variable to justify the overhead.
258
259 As a general rule: Use register variables only for pointers that are
260 dereferenced several times in your function, or for heavily used induction
261 variables in a loop (with several 100 accesses).
262
263 When declaring register variables, try to keep them together, because this
264 will allow the compiler to save and restore the old values in one chunk, and
265 not in several.
266
267 And remember: Register variables must be enabled with <tt/-r/ or <tt/-Or/.
268
269
270
271 <sect>Decimal constants greater than 0x7FFF are actually long ints<p>
272
273 The language rules for constant numeric values specify that decimal constants
274 without a type suffix that are not in integer range must be of type long int
275 or unsigned long int. So a simple constant like 40000 is of type long int!
276 This is often unexpected and may cause an expression to be evaluated with 32
277 bits. While in many cases the compiler takes care about it, in some places it
278 can't. So be careful when you get a warning like
279
280 <tscreen><verb>
281         test.c(7): Warning: Constant is long
282 </verb></tscreen>
283
284 Use the <tt/U/, <tt/L/ or <tt/UL/ suffixes to tell the compiler the desired
285 type of a numeric constant.
286
287
288
289 <sect>Access to parameters in variadic functions is expensive<p>
290
291 Since cc65 has the "wrong" calling order, the location of the fixed parameters
292 in a variadic function (a function with a variable parameter list) depends on
293 the number and size of variable arguments passed. Since this number and size
294 is unknown at compile time, the compiler will generate code to calculate the
295 location on the stack when needed.
296
297 Because of this additional code, accessing the fixed parameters in a variadic
298 function is much more expensive than access to parameters in a "normal"
299 function. Unfortunately, this additional code is also invisible to the
300 programmer, so it is easy to forget.
301
302 As a rule of thumb, if you access such a parameter more than once, you should
303 think about copying it into a normal variable and using this variable instead.
304
305
306 </article>
307