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