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