]> git.sur5r.net Git - cc65/blob - src/cc65/codegen.h
977b868b7e0e4d09d630e44d7a363c687ff6529d
[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         0x000F  /* 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_UNSIGNED     0x0010  /* Value is unsigned */
60 #define CF_CONST        0x0020  /* Constant value available */
61 #define CF_CONSTADDR    0x0040  /* Constant address value available */
62 #define CF_TEST         0x0080  /* Test value */
63 #define CF_FIXARGC      0x0100  /* Function has fixed arg count */
64 #define CF_FORCECHAR    0x0200  /* Handle chars as chars, not ints */
65 #define CF_SHORT        0x0400  /* Use short addressing */
66 #define CF_REG          0x0800  /* Value is in primary register */
67
68 /* Type of static address */
69 #define CF_ADDRMASK     0xF000  /* Type of address */
70 #define CF_STATIC       0x0000  /* Static local */
71 #define CF_EXTERNAL     0x1000  /* Static external */
72 #define CF_ABSOLUTE     0x2000  /* Numeric absolute address */
73 #define CF_LOCAL        0x4000  /* Auto variable */
74 #define CF_REGVAR       0x8000  /* Register variable */
75
76
77
78 /* Compiler relative stackpointer */
79 extern int oursp;
80
81
82
83 /*****************************************************************************/
84 /*                            Pre- and postamble                             */
85 /*****************************************************************************/
86
87
88
89 void g_preamble (void);
90 /* Generate the assembler code preamble */
91
92 void g_postamble (void);
93 /* Generate assembler code postamble */
94
95
96
97 /*****************************************************************************/
98 /*                              Segment support                              */
99 /*****************************************************************************/
100
101
102
103 void g_usecode (void);
104 /* Switch to the code segment */
105
106 void g_userodata (void);
107 /* Switch to the read only data segment */
108
109 void g_usedata (void);
110 /* Switch to the data segment */
111
112 void g_usebss (void);
113 /* Switch to the bss segment */
114
115 void g_codename (const char* Name);
116 /* Set the name of the CODE segment */
117
118 void g_rodataname (const char* Name);
119 /* Set the name of the RODATA segment */
120
121 void g_dataname (const char* Name);
122 /* Set the name of the DATA segment */
123
124 void g_bssname (const char* Name);
125 /* Set the name of the BSS segment */
126
127
128
129 /*****************************************************************************/
130 /*                      Functions handling local labels                      */
131 /*****************************************************************************/
132
133
134
135 void g_defloclabel (unsigned label);
136 /* Define a local label */
137
138
139
140 /*****************************************************************************/
141 /*                     Functions handling global labels                      */
142 /*****************************************************************************/
143
144
145
146 void g_defgloblabel (const char* Name);
147 /* Define a global label with the given name */
148
149 void g_defexport (const char* Name, int ZP);
150 /* Export the given label */
151
152 void g_defimport (const char* Name, int ZP);
153 /* Import the given label */
154
155
156
157 /*****************************************************************************/
158 /*                                   stack                                   */
159 /*****************************************************************************/
160
161
162
163 int pop (unsigned flags);
164 /* Pop an argument of the given size */
165
166 int push (unsigned flags);
167 /* Push an argument of the given size */
168
169 unsigned sizeofarg (unsigned flags);
170 /* Return the size of a function argument type that is encoded in flags */
171
172
173
174 /*****************************************************************************/
175 /*                    type conversion and similiar stuff                     */
176 /*****************************************************************************/
177
178
179
180 void g_toslong (unsigned flags);
181 /* Make sure, the value on TOS is a long. Convert if necessary */
182
183 void g_tosint (unsigned flags);
184 /* Make sure, the value on TOS is an int. Convert if necessary */
185
186 void g_reglong (unsigned flags);
187 /* Make sure, the value in the primary register a long. Convert if necessary */
188
189 unsigned g_typeadjust (unsigned lhs, unsigned rhs);
190 /* Adjust the integer operands before doing a binary operation. lhs is a flags
191  * value, that corresponds to the value on TOS, rhs corresponds to the value
192  *  in (e)ax. The return value is the the flags value for the resulting type.
193  */
194
195 unsigned g_typecast (unsigned lhs, unsigned rhs);
196 /* Cast the value in the primary register to the operand size that is flagged
197  * by the lhs value. Return the result value.
198  */
199
200 void g_scale (unsigned flags, long val);
201 /* Scale the value in the primary register by the given value. If val is positive,
202  * scale up, is val is negative, scale down. This function is used to scale
203  * the operands or results of pointer arithmetic by the size of the type, the
204  * pointer points to.
205  */
206
207
208
209 /*****************************************************************************/
210 /*                          Function entry and exit                          */
211 /*****************************************************************************/
212
213
214
215 void g_enter (unsigned flags, unsigned argsize);
216 /* Function prologue */
217
218 void g_leave (int flags, int val);
219 /* Function epilogue */
220
221
222
223 /*****************************************************************************/
224 /*                            Register variables                             */
225 /*****************************************************************************/
226
227
228
229 void g_save_regvars (int RegOffs, unsigned Bytes);
230 /* Save register variables */
231
232 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes);
233 /* Restore register variables */
234
235
236
237 /*****************************************************************************/
238 /*                           Fetching memory cells                           */
239 /*****************************************************************************/
240
241
242
243 void g_getimmed (unsigned flags, unsigned long val, unsigned offs);
244 void g_getstatic (unsigned flags, unsigned long label, unsigned offs);
245 void g_getlocal (unsigned flags, int offs);
246 void g_getind (unsigned flags, unsigned offs);
247 void g_leasp (int offs);
248
249
250
251 /*****************************************************************************/
252 /*                             Store into memory                             */
253 /*****************************************************************************/
254
255
256
257 void g_putstatic (unsigned flags, unsigned long label, unsigned offs);
258 /* Store the primary register into the specified static memory cell */
259
260 void g_putlocal (unsigned flags, int offs);
261 /* Put data into local object. */
262
263 void g_putind (unsigned flags, unsigned offs);
264 /* Store the specified object type in the primary register at the address
265  * on the top of the stack
266  */
267
268
269
270 /*****************************************************************************/
271 /*              Adds and subs of variables fix a fixed address               */
272 /*****************************************************************************/
273
274
275
276 void g_addlocal (unsigned flags, int offs);
277 /* Add a local variable to ax */
278
279 void g_addstatic (unsigned flags, unsigned long label, unsigned offs);
280 /* Add a static variable to ax */
281
282
283
284 /*****************************************************************************/
285 /*             Compares of ax with a variable with fixed address             */
286 /*****************************************************************************/
287
288
289
290 void g_cmplocal (unsigned flags, int offs);
291 /* Compare a local variable to ax */
292
293 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs);
294 /* Compare a static variable to ax */
295
296
297
298 /*****************************************************************************/
299 /*                           Special op= functions                           */
300 /*****************************************************************************/
301
302
303
304 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
305                     unsigned long val);
306 /* Emit += for a static variable */
307
308 void g_addeqlocal (unsigned flags, int offs, unsigned long val);
309 /* Emit += for a local variable */
310
311 void g_addeqind (unsigned flags, unsigned offs, unsigned long val);
312 /* Emit += for the location with address in ax */
313
314 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
315                     unsigned long val);
316 /* Emit -= for a static variable */
317
318 void g_subeqlocal (unsigned flags, int offs, unsigned long val);
319 /* Emit -= for a local variable */
320
321 void g_subeqind (unsigned flags, unsigned offs, unsigned long val);
322 /* Emit -= for the location with address in ax */
323
324
325
326 /*****************************************************************************/
327 /*                 Add a variable address to the value in ax                 */
328 /*****************************************************************************/
329
330
331
332 void g_addaddr_local (unsigned flags, int offs);
333 /* Add the address of a local variable to ax */
334
335 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs);
336 /* Add the address of a static variable to ax */
337
338
339
340 /*****************************************************************************/
341 /*                                                                           */
342 /*****************************************************************************/
343
344
345
346 void g_save (unsigned flags);
347 void g_restore (unsigned flags);
348
349 void g_cmp (unsigned flags, unsigned long val);
350 /* Immidiate compare. The primary register will not be changed, Z flag
351  * will be set.
352  */
353
354 void g_test (unsigned flags);
355 void g_push (unsigned flags, unsigned long val);
356 void g_swap (unsigned flags);
357 void g_call (unsigned flags, char *lbl, unsigned argsize);
358 void g_callind (unsigned flags, unsigned argsize);
359 void g_jump (unsigned label);
360 void g_switch (unsigned flags);
361
362 void g_case (unsigned flags, unsigned label, unsigned long val);
363 /* Create table code for one case selector */
364
365 void g_truejump (unsigned flags, unsigned label);
366 /* Jump to label if zero flag clear */
367
368 void g_falsejump (unsigned flags, unsigned label);
369 /* Jump to label if zero flag set */
370
371 void g_space (int space);
372 /* Create or drop space on the stack */
373
374 void g_cstackcheck (void);
375 /* Check for a C stack overflow */
376
377 void g_stackcheck (void);
378 /* Check for a stack overflow */
379
380 void g_add (unsigned flags, unsigned long val);
381 void g_sub (unsigned flags, unsigned long val);
382 void g_rsub (unsigned flags, unsigned long val);
383 void g_mul (unsigned flags, unsigned long val);
384 void g_div (unsigned flags, unsigned long val);
385 void g_mod (unsigned flags, unsigned long val);
386 void g_or (unsigned flags, unsigned long val);
387 void g_xor (unsigned flags, unsigned long val);
388 void g_and (unsigned flags, unsigned long val);
389 void g_asr (unsigned flags, unsigned long val);
390 void g_asl (unsigned flags, unsigned long val);
391 void g_neg (unsigned flags);
392 void g_bneg (unsigned flags);
393 void g_com (unsigned flags);
394 void g_inc (unsigned flags, unsigned long n);
395 void g_dec (unsigned flags, unsigned long n);
396 void g_eq (unsigned flags, unsigned long val);
397 void g_ne (unsigned flags, unsigned long val);
398 void g_lt (unsigned flags, unsigned long val);
399 void g_le (unsigned flags, unsigned long val);
400 void g_gt (unsigned flags, unsigned long val);
401 void g_ge (unsigned flags, unsigned long val);
402
403 void g_res (unsigned n);
404 /* Reserve static storage, n bytes */
405
406 void g_defdata (unsigned flags, unsigned long val, unsigned offs);
407 /* Define data with the size given in flags */
408
409 void g_defbytes (const unsigned char *bytes, unsigned count);
410 /* Output a row of bytes as a constant */
411
412 void g_zerobytes (unsigned n);
413 /* Output n bytes of data initialized with zero */
414
415
416
417 /*****************************************************************************/
418 /*                          Inlined known functions                          */
419 /*****************************************************************************/
420
421
422
423 void g_strlen (unsigned flags, unsigned long val, unsigned offs);
424 /* Inline the strlen() function */
425
426
427
428 /* End of codegen.h */
429 #endif
430
431