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