1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* codegen.h */ |
4 | /* */ |
5 | /* 6502 code generator */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 1998-2013, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | #ifndef CODEGEN_H |
37 | #define CODEGEN_H |
38 | |
39 | |
40 | |
41 | /* common */ |
42 | #include "coll.h" |
43 | #include "inttypes.h" |
44 | |
45 | /* cc65 */ |
46 | #include "segments.h" |
47 | |
48 | |
49 | |
50 | /*****************************************************************************/ |
51 | /* Data */ |
52 | /*****************************************************************************/ |
53 | |
54 | |
55 | |
56 | /* Code generator flags. |
57 | ** Note: The type flags are designed so that a smaller type may override a |
58 | ** larger one by or'ing it into the existing one. |
59 | ** Note^2: The actual type including the sign flag is in the lower bits, so |
60 | ** we can mask the information and use them as a table index. |
61 | */ |
62 | #define CF_NONE 0x0000 /* No special flags */ |
63 | |
64 | /* Values for the actual type */ |
65 | #define CF_CHAR 0x0003 /* Operation on characters */ |
66 | #define CF_INT 0x0001 /* Operation on ints */ |
67 | #define CF_PTR CF_INT /* Alias for readability */ |
68 | #define CF_LONG 0x0000 /* Operation on longs */ |
69 | #define CF_FLOAT 0x0004 /* Operation on a float */ |
70 | |
71 | /* Signedness */ |
72 | #define CF_UNSIGNED 0x0008 /* Value is unsigned */ |
73 | |
74 | /* Masks for retrieving type information */ |
75 | #define CF_TYPEMASK 0x0007 /* Type information */ |
76 | #define CF_STYPEMASK 0x000F /* Includes signedness */ |
77 | |
78 | #define CF_NOKEEP 0x0010 /* Value may get destroyed when storing */ |
79 | #define CF_CONST 0x0020 /* Constant value available */ |
80 | #define CF_CONSTADDR 0x0040 /* Constant address value available */ |
81 | #define CF_TEST 0x0080 /* Test value */ |
82 | #define CF_FIXARGC 0x0100 /* Function has fixed arg count */ |
83 | #define CF_FORCECHAR 0x0200 /* Handle chars as chars, not ints */ |
84 | #define CF_REG 0x0800 /* Value is in primary register */ |
85 | |
86 | /* Type of static address */ |
87 | #define CF_ADDRMASK 0xF000 /* Type of address */ |
88 | #define CF_STATIC 0x0000 /* Static local */ |
89 | #define CF_EXTERNAL 0x1000 /* Static external */ |
90 | #define CF_ABSOLUTE 0x2000 /* Numeric absolute address */ |
91 | #define CF_LOCAL 0x4000 /* Auto variable */ |
92 | #define CF_REGVAR 0x8000 /* Register variable */ |
93 | |
94 | |
95 | |
96 | /* Forward */ |
97 | struct StrBuf; |
98 | |
99 | |
100 | |
101 | /*****************************************************************************/ |
102 | /* Files, pre- and postamble */ |
103 | /*****************************************************************************/ |
104 | |
105 | |
106 | |
107 | void g_preamble (void); |
108 | /* Generate the assembler code preamble */ |
109 | |
110 | void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime); |
111 | /* If debug info is enabled, place a file info into the source */ |
112 | |
113 | |
114 | |
115 | /*****************************************************************************/ |
116 | /* Segment support */ |
117 | /*****************************************************************************/ |
118 | |
119 | |
120 | |
121 | void g_userodata (void); |
122 | /* Switch to the read only data segment */ |
123 | |
124 | void g_usedata (void); |
125 | /* Switch to the data segment */ |
126 | |
127 | void g_usebss (void); |
128 | /* Switch to the bss segment */ |
129 | |
130 | void g_segname (segment_t Seg); |
131 | /* Emit the name of a segment if necessary */ |
132 | |
133 | |
134 | |
135 | /*****************************************************************************/ |
136 | /* Functions handling local labels */ |
137 | /*****************************************************************************/ |
138 | |
139 | |
140 | |
141 | void g_defcodelabel (unsigned label); |
142 | /* Define a local code label */ |
143 | |
144 | void g_defdatalabel (unsigned label); |
145 | /* Define a local data label */ |
146 | |
147 | void g_aliasdatalabel (unsigned label, unsigned baselabel, long offs); |
148 | /* Define label as a local alias for baselabel+offs */ |
149 | |
150 | |
151 | |
152 | /*****************************************************************************/ |
153 | /* Functions handling global labels */ |
154 | /*****************************************************************************/ |
155 | |
156 | |
157 | |
158 | void g_defgloblabel (const char* Name); |
159 | /* Define a global label with the given name */ |
160 | |
161 | void g_defexport (const char* Name, int ZP); |
162 | /* Export the given label */ |
163 | |
164 | void g_defimport (const char* Name, int ZP); |
165 | /* Import the given label */ |
166 | |
167 | void g_importstartup (void); |
168 | /* Forced import of the startup segment */ |
169 | |
170 | void g_importmainargs (void); |
171 | /* Forced import of a special symbol that handles arguments to main */ |
172 | |
173 | |
174 | |
175 | /*****************************************************************************/ |
176 | /* stack */ |
177 | /*****************************************************************************/ |
178 | |
179 | |
180 | |
181 | int pop (unsigned flags); |
182 | /* Pop an argument of the given size */ |
183 | |
184 | int push (unsigned flags); |
185 | /* Push an argument of the given size */ |
186 | |
187 | unsigned sizeofarg (unsigned flags); |
188 | /* Return the size of a function argument type that is encoded in flags */ |
189 | |
190 | |
191 | |
192 | /*****************************************************************************/ |
193 | /* type conversion and similiar stuff */ |
194 | /*****************************************************************************/ |
195 | |
196 | |
197 | |
198 | void g_toslong (unsigned flags); |
199 | /* Make sure, the value on TOS is a long. Convert if necessary */ |
200 | |
201 | void g_tosint (unsigned flags); |
202 | /* Make sure, the value on TOS is an int. Convert if necessary */ |
203 | |
204 | void g_regint (unsigned Flags); |
205 | /* Make sure, the value in the primary register an int. Convert if necessary */ |
206 | |
207 | void g_reglong (unsigned Flags); |
208 | /* Make sure, the value in the primary register a long. Convert if necessary */ |
209 | |
210 | unsigned g_typeadjust (unsigned lhs, unsigned rhs); |
211 | /* Adjust the integer operands before doing a binary operation. lhs is a flags |
212 | ** value, that corresponds to the value on TOS, rhs corresponds to the value |
213 | ** in (e)ax. The return value is the the flags value for the resulting type. |
214 | */ |
215 | |
216 | unsigned g_typecast (unsigned lhs, unsigned rhs); |
217 | /* Cast the value in the primary register to the operand size that is flagged |
218 | ** by the lhs value. Return the result value. |
219 | */ |
220 | |
221 | void g_scale (unsigned flags, long val); |
222 | /* Scale the value in the primary register by the given value. If val is positive, |
223 | ** scale up, is val is negative, scale down. This function is used to scale |
224 | ** the operands or results of pointer arithmetic by the size of the type, the |
225 | ** pointer points to. |
226 | */ |
227 | |
228 | |
229 | |
230 | /*****************************************************************************/ |
231 | /* Function entry and exit */ |
232 | /*****************************************************************************/ |
233 | |
234 | |
235 | |
236 | void g_enter (unsigned flags, unsigned argsize); |
237 | /* Function prologue */ |
238 | |
239 | void g_leave (void); |
240 | /* Function epilogue */ |
241 | |
242 | |
243 | |
244 | /*****************************************************************************/ |
245 | /* Register variables */ |
246 | /*****************************************************************************/ |
247 | |
248 | |
249 | |
250 | void g_swap_regvars (int StackOffs, int RegOffs, unsigned Bytes); |
251 | /* Swap a register variable with a location on the stack */ |
252 | |
253 | void g_save_regvars (int RegOffs, unsigned Bytes); |
254 | /* Save register variables */ |
255 | |
256 | void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes); |
257 | /* Restore register variables */ |
258 | |
259 | |
260 | |
261 | /*****************************************************************************/ |
262 | /* Fetching memory cells */ |
263 | /*****************************************************************************/ |
264 | |
265 | |
266 | |
267 | void g_getimmed (unsigned Flags, unsigned long Val, long Offs); |
268 | /* Load a constant into the primary register */ |
269 | |
270 | void g_getstatic (unsigned Flags, uintptr_t Label, long Offs); |
271 | /* Fetch an static memory cell into the primary register */ |
272 | |
273 | void g_getlocal (unsigned Flags, int Offs); |
274 | /* Fetch specified local object (local var). */ |
275 | |
276 | void g_getind (unsigned Flags, unsigned Offs); |
277 | /* Fetch the specified object type indirect through the primary register |
278 | ** into the primary register |
279 | */ |
280 | |
281 | void g_leasp (int Offs); |
282 | /* Fetch the address of the specified symbol into the primary register */ |
283 | |
284 | void g_leavariadic (int Offs); |
285 | /* Fetch the address of a parameter in a variadic function into the primary |
286 | ** register |
287 | */ |
288 | |
289 | |
290 | |
291 | /*****************************************************************************/ |
292 | /* Store into memory */ |
293 | /*****************************************************************************/ |
294 | |
295 | |
296 | |
297 | void g_putstatic (unsigned flags, uintptr_t label, long offs); |
298 | /* Store the primary register into the specified static memory cell */ |
299 | |
300 | void g_putlocal (unsigned Flags, int Offs, long Val); |
301 | /* Put data into local object. */ |
302 | |
303 | void g_putind (unsigned flags, unsigned offs); |
304 | /* Store the specified object type in the primary register at the address |
305 | ** on the top of the stack |
306 | */ |
307 | |
308 | |
309 | |
310 | /*****************************************************************************/ |
311 | /* Adds and subs of variables fix a fixed address */ |
312 | /*****************************************************************************/ |
313 | |
314 | |
315 | |
316 | void g_addlocal (unsigned flags, int offs); |
317 | /* Add a local variable to ax */ |
318 | |
319 | void g_addstatic (unsigned flags, uintptr_t label, long offs); |
320 | /* Add a static variable to ax */ |
321 | |
322 | |
323 | |
324 | /*****************************************************************************/ |
325 | /* Special op= functions */ |
326 | /*****************************************************************************/ |
327 | |
328 | |
329 | |
330 | void g_addeqstatic (unsigned flags, uintptr_t label, long offs, |
331 | unsigned long val); |
332 | /* Emit += for a static variable */ |
333 | |
334 | void g_addeqlocal (unsigned flags, int offs, unsigned long val); |
335 | /* Emit += for a local variable */ |
336 | |
337 | void g_addeqind (unsigned flags, unsigned offs, unsigned long val); |
338 | /* Emit += for the location with address in ax */ |
339 | |
340 | void g_subeqstatic (unsigned flags, uintptr_t label, long offs, |
341 | unsigned long val); |
342 | /* Emit -= for a static variable */ |
343 | |
344 | void g_subeqlocal (unsigned flags, int offs, unsigned long val); |
345 | /* Emit -= for a local variable */ |
346 | |
347 | void g_subeqind (unsigned flags, unsigned offs, unsigned long val); |
348 | /* Emit -= for the location with address in ax */ |
349 | |
350 | |
351 | |
352 | /*****************************************************************************/ |
353 | /* Add a variable address to the value in ax */ |
354 | /*****************************************************************************/ |
355 | |
356 | |
357 | |
358 | void g_addaddr_local (unsigned flags, int offs); |
359 | /* Add the address of a local variable to ax */ |
360 | |
361 | void g_addaddr_static (unsigned flags, uintptr_t label, long offs); |
362 | /* Add the address of a static variable to ax */ |
363 | |
364 | |
365 | |
366 | /*****************************************************************************/ |
367 | /* */ |
368 | /*****************************************************************************/ |
369 | |
370 | |
371 | |
372 | void g_save (unsigned flags); |
373 | /* Copy primary register to hold register. */ |
374 | |
375 | void g_restore (unsigned flags); |
376 | /* Copy hold register to primary. */ |
377 | |
378 | void g_cmp (unsigned flags, unsigned long val); |
379 | /* Immidiate compare. The primary register will not be changed, Z flag |
380 | ** will be set. |
381 | */ |
382 | |
383 | void g_test (unsigned flags); |
384 | /* Test the value in the primary and set the condition codes */ |
385 | |
386 | void g_push (unsigned flags, unsigned long val); |
387 | /* Push the primary register or a constant value onto the stack */ |
388 | |
389 | void g_swap (unsigned flags); |
390 | /* Swap the primary register and the top of the stack. flags give the type |
391 | ** of *both* values (must have same size). |
392 | */ |
393 | |
394 | void g_call (unsigned Flags, const char* Label, unsigned ArgSize); |
395 | /* Call the specified subroutine name */ |
396 | |
397 | void g_callind (unsigned Flags, unsigned ArgSize, int Offs); |
398 | /* Call subroutine indirect */ |
399 | |
400 | void g_jump (unsigned Label); |
401 | /* Jump to specified internal label number */ |
402 | |
403 | void g_truejump (unsigned flags, unsigned label); |
404 | /* Jump to label if zero flag clear */ |
405 | |
406 | void g_falsejump (unsigned flags, unsigned label); |
407 | /* Jump to label if zero flag set */ |
408 | |
409 | void g_lateadjustSP (unsigned label); |
410 | /* Adjust stack based on non-immediate data */ |
411 | |
412 | void g_drop (unsigned Space); |
413 | /* Drop space allocated on the stack */ |
414 | |
415 | void g_space (int space); |
416 | /* Create or drop space on the stack */ |
417 | |
418 | void g_cstackcheck (void); |
419 | /* Check for a C stack overflow */ |
420 | |
421 | void g_stackcheck (void); |
422 | /* Check for a stack overflow */ |
423 | |
424 | void g_add (unsigned flags, unsigned long val); |
425 | void g_sub (unsigned flags, unsigned long val); |
426 | void g_rsub (unsigned flags, unsigned long val); |
427 | void g_mul (unsigned flags, unsigned long val); |
428 | void g_div (unsigned flags, unsigned long val); |
429 | void g_mod (unsigned flags, unsigned long val); |
430 | void g_or (unsigned flags, unsigned long val); |
431 | void g_xor (unsigned flags, unsigned long val); |
432 | void g_and (unsigned flags, unsigned long val); |
433 | void g_asr (unsigned flags, unsigned long val); |
434 | void g_asl (unsigned flags, unsigned long val); |
435 | void g_neg (unsigned flags); |
436 | void g_bneg (unsigned flags); |
437 | void g_com (unsigned flags); |
438 | void g_inc (unsigned flags, unsigned long n); |
439 | void g_dec (unsigned flags, unsigned long n); |
440 | void g_eq (unsigned flags, unsigned long val); |
441 | void g_ne (unsigned flags, unsigned long val); |
442 | void g_lt (unsigned flags, unsigned long val); |
443 | void g_le (unsigned flags, unsigned long val); |
444 | void g_gt (unsigned flags, unsigned long val); |
445 | void g_ge (unsigned flags, unsigned long val); |
446 | |
447 | void g_res (unsigned n); |
448 | /* Reserve static storage, n bytes */ |
449 | |
450 | void g_defdata (unsigned flags, unsigned long val, long offs); |
451 | /* Define data with the size given in flags */ |
452 | |
453 | void g_defbytes (const void* bytes, unsigned count); |
454 | /* Output a row of bytes as a constant */ |
455 | |
456 | void g_zerobytes (unsigned Count); |
457 | /* Output Count bytes of data initialized with zero */ |
458 | |
459 | void g_initregister (unsigned Label, unsigned Reg, unsigned Size); |
460 | /* Initialize a register variable from static initialization data */ |
461 | |
462 | void g_initauto (unsigned Label, unsigned Size); |
463 | /* Initialize a local variable at stack offset zero from static data */ |
464 | |
465 | void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size); |
466 | /* Initialize a static local variable from static initialization data */ |
467 | |
468 | |
469 | |
470 | /*****************************************************************************/ |
471 | /* Switch statement */ |
472 | /*****************************************************************************/ |
473 | |
474 | |
475 | |
476 | void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth); |
477 | /* Generate code for a switch statement */ |
478 | |
479 | |
480 | |
481 | /*****************************************************************************/ |
482 | /* User supplied assembler code */ |
483 | /*****************************************************************************/ |
484 | |
485 | |
486 | |
487 | void g_asmcode (struct StrBuf* B); |
488 | /* Output one line of assembler code. */ |
489 | |
490 | |
491 | |
492 | /* End of codegen.h */ |
493 | |
494 | #endif |
495 | |