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