1 /*****************************************************************************/
5 /* 6502 code generator */
9 /* (C) 1998-2001 llrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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. */
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: */
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 */
32 /*****************************************************************************/
41 /*****************************************************************************/
43 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
58 /* Code generator flags.
59 * Note: The type flags are designed so that a smaller type may override a
60 * larger one by or'ing it into the existing one.
62 #define CF_NONE 0x0000 /* No special flags */
64 #define CF_TYPE 0x0007 /* Mask for operand type */
65 #define CF_CHAR 0x0003 /* Operation on characters */
66 #define CF_INT 0x0001 /* Operation on ints */
67 #define CF_PTR CF_INT /* Alias for readability */
68 #define CF_LONG 0x0000 /* Operation on longs */
70 #define CF_NOKEEP 0x0008 /* Value may get destroyed when storing */
72 #define CF_UNSIGNED 0x0010 /* Value is unsigned */
73 #define CF_CONST 0x0020 /* Constant value available */
74 #define CF_CONSTADDR 0x0040 /* Constant address value available */
75 #define CF_TEST 0x0080 /* Test value */
76 #define CF_FIXARGC 0x0100 /* Function has fixed arg count */
77 #define CF_FORCECHAR 0x0200 /* Handle chars as chars, not ints */
78 #define CF_SHORT 0x0400 /* Use short addressing */
79 #define CF_REG 0x0800 /* Value is in primary register */
81 /* Type of static address */
82 #define CF_ADDRMASK 0xF000 /* Type of address */
83 #define CF_STATIC 0x0000 /* Static local */
84 #define CF_EXTERNAL 0x1000 /* Static external */
85 #define CF_ABSOLUTE 0x2000 /* Numeric absolute address */
86 #define CF_LOCAL 0x4000 /* Auto variable */
87 #define CF_REGVAR 0x8000 /* Register variable */
91 /* Compiler relative stackpointer */
96 /*****************************************************************************/
97 /* Pre- and postamble */
98 /*****************************************************************************/
102 void g_preamble (void);
103 /* Generate the assembler code preamble */
107 /*****************************************************************************/
108 /* Segment support */
109 /*****************************************************************************/
113 void g_pushseg (struct CodeSeg** CS, struct DataSeg** DS, const char* FuncName);
114 /* Push the current segments and generate new ones for the given function */
116 void g_popseg (void);
117 /* Restore the old segments */
119 void g_usecode (void);
120 /* Switch to the code segment */
122 void g_userodata (void);
123 /* Switch to the read only data segment */
125 void g_usedata (void);
126 /* Switch to the data segment */
128 void g_usebss (void);
129 /* Switch to the bss segment */
131 void g_codename (const char* Name);
132 /* Set the name of the CODE segment */
134 void g_rodataname (const char* Name);
135 /* Set the name of the RODATA segment */
137 void g_dataname (const char* Name);
138 /* Set the name of the DATA segment */
140 void g_bssname (const char* Name);
141 /* Set the name of the BSS segment */
145 /*****************************************************************************/
146 /* Functions handling local labels */
147 /*****************************************************************************/
151 void g_defloclabel (unsigned label);
152 /* Define a local label */
156 /*****************************************************************************/
157 /* Functions handling global labels */
158 /*****************************************************************************/
162 void g_defgloblabel (const char* Name);
163 /* Define a global label with the given name */
165 void g_defexport (const char* Name, int ZP);
166 /* Export the given label */
168 void g_defimport (const char* Name, int ZP);
169 /* Import the given label */
173 /*****************************************************************************/
175 /*****************************************************************************/
179 int pop (unsigned flags);
180 /* Pop an argument of the given size */
182 int push (unsigned flags);
183 /* Push an argument of the given size */
185 unsigned sizeofarg (unsigned flags);
186 /* Return the size of a function argument type that is encoded in flags */
190 /*****************************************************************************/
191 /* type conversion and similiar stuff */
192 /*****************************************************************************/
196 void g_toslong (unsigned flags);
197 /* Make sure, the value on TOS is a long. Convert if necessary */
199 void g_tosint (unsigned flags);
200 /* Make sure, the value on TOS is an int. Convert if necessary */
202 void g_reglong (unsigned flags);
203 /* Make sure, the value in the primary register a long. Convert if necessary */
205 unsigned g_typeadjust (unsigned lhs, unsigned rhs);
206 /* Adjust the integer operands before doing a binary operation. lhs is a flags
207 * value, that corresponds to the value on TOS, rhs corresponds to the value
208 * in (e)ax. The return value is the the flags value for the resulting type.
211 unsigned g_typecast (unsigned lhs, unsigned rhs);
212 /* Cast the value in the primary register to the operand size that is flagged
213 * by the lhs value. Return the result value.
216 void g_scale (unsigned flags, long val);
217 /* Scale the value in the primary register by the given value. If val is positive,
218 * scale up, is val is negative, scale down. This function is used to scale
219 * the operands or results of pointer arithmetic by the size of the type, the
225 /*****************************************************************************/
226 /* Function entry and exit */
227 /*****************************************************************************/
231 void g_enter (unsigned flags, unsigned argsize);
232 /* Function prologue */
234 void g_leave (int flags, int val);
235 /* Function epilogue */
239 /*****************************************************************************/
240 /* Register variables */
241 /*****************************************************************************/
245 void g_save_regvars (int RegOffs, unsigned Bytes);
246 /* Save register variables */
248 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes);
249 /* Restore register variables */
253 /*****************************************************************************/
254 /* Fetching memory cells */
255 /*****************************************************************************/
259 void g_getimmed (unsigned Flags, unsigned long Val, unsigned Offs);
260 /* Load a constant into the primary register */
262 void g_getstatic (unsigned Flags, unsigned long Label, unsigned Offs);
263 /* Fetch an static memory cell into the primary register */
265 void g_getlocal (unsigned Flags, int Offs);
266 /* Fetch specified local object (local var). */
268 void g_getind (unsigned Flags, unsigned Offs);
269 /* Fetch the specified object type indirect through the primary register
270 * into the primary register
273 void g_leasp (int Offs);
274 /* Fetch the address of the specified symbol into the primary register */
276 void g_leavariadic (int Offs);
277 /* Fetch the address of a parameter in a variadic function into the primary
283 /*****************************************************************************/
284 /* Store into memory */
285 /*****************************************************************************/
289 void g_putstatic (unsigned flags, unsigned long label, unsigned offs);
290 /* Store the primary register into the specified static memory cell */
292 void g_putlocal (unsigned Flags, int Offs, long Val);
293 /* Put data into local object. */
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
302 /*****************************************************************************/
303 /* Adds and subs of variables fix a fixed address */
304 /*****************************************************************************/
308 void g_addlocal (unsigned flags, int offs);
309 /* Add a local variable to ax */
311 void g_addstatic (unsigned flags, unsigned long label, unsigned offs);
312 /* Add a static variable to ax */
316 /*****************************************************************************/
317 /* Compares of ax with a variable with fixed address */
318 /*****************************************************************************/
322 void g_cmplocal (unsigned flags, int offs);
323 /* Compare a local variable to ax */
325 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs);
326 /* Compare a static variable to ax */
330 /*****************************************************************************/
331 /* Special op= functions */
332 /*****************************************************************************/
336 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
338 /* Emit += for a static variable */
340 void g_addeqlocal (unsigned flags, int offs, unsigned long val);
341 /* Emit += for a local variable */
343 void g_addeqind (unsigned flags, unsigned offs, unsigned long val);
344 /* Emit += for the location with address in ax */
346 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
348 /* Emit -= for a static variable */
350 void g_subeqlocal (unsigned flags, int offs, unsigned long val);
351 /* Emit -= for a local variable */
353 void g_subeqind (unsigned flags, unsigned offs, unsigned long val);
354 /* Emit -= for the location with address in ax */
358 /*****************************************************************************/
359 /* Add a variable address to the value in ax */
360 /*****************************************************************************/
364 void g_addaddr_local (unsigned flags, int offs);
365 /* Add the address of a local variable to ax */
367 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs);
368 /* Add the address of a static variable to ax */
372 /*****************************************************************************/
374 /*****************************************************************************/
378 void g_save (unsigned flags);
379 void g_restore (unsigned flags);
381 void g_cmp (unsigned flags, unsigned long val);
382 /* Immidiate compare. The primary register will not be changed, Z flag
386 void g_test (unsigned flags);
387 void g_push (unsigned flags, unsigned long val);
388 void g_swap (unsigned flags);
390 void g_call (unsigned Flags, const char* Label, unsigned ArgSize);
391 /* Call the specified subroutine name */
393 void g_callind (unsigned Flags, unsigned ArgSize);
394 /* Call subroutine with address in AX */
396 void g_jump (unsigned Label);
397 /* Jump to specified internal label number */
399 void g_switch (unsigned Flags);
400 /* Output switch statement preamble */
402 void g_case (unsigned flags, unsigned label, unsigned long val);
403 /* Create table code for one case selector */
405 void g_truejump (unsigned flags, unsigned label);
406 /* Jump to label if zero flag clear */
408 void g_falsejump (unsigned flags, unsigned label);
409 /* Jump to label if zero flag set */
411 void g_space (int space);
412 /* Create or drop space on the stack */
414 void g_cstackcheck (void);
415 /* Check for a C stack overflow */
417 void g_stackcheck (void);
418 /* Check for a stack overflow */
420 void g_add (unsigned flags, unsigned long val);
421 void g_sub (unsigned flags, unsigned long val);
422 void g_rsub (unsigned flags, unsigned long val);
423 void g_mul (unsigned flags, unsigned long val);
424 void g_div (unsigned flags, unsigned long val);
425 void g_mod (unsigned flags, unsigned long val);
426 void g_or (unsigned flags, unsigned long val);
427 void g_xor (unsigned flags, unsigned long val);
428 void g_and (unsigned flags, unsigned long val);
429 void g_asr (unsigned flags, unsigned long val);
430 void g_asl (unsigned flags, unsigned long val);
431 void g_neg (unsigned flags);
432 void g_bneg (unsigned flags);
433 void g_com (unsigned flags);
434 void g_inc (unsigned flags, unsigned long n);
435 void g_dec (unsigned flags, unsigned long n);
436 void g_eq (unsigned flags, unsigned long val);
437 void g_ne (unsigned flags, unsigned long val);
438 void g_lt (unsigned flags, unsigned long val);
439 void g_le (unsigned flags, unsigned long val);
440 void g_gt (unsigned flags, unsigned long val);
441 void g_ge (unsigned flags, unsigned long val);
443 void g_res (unsigned n);
444 /* Reserve static storage, n bytes */
446 void g_defdata (unsigned flags, unsigned long val, unsigned offs);
447 /* Define data with the size given in flags */
449 void g_defbytes (const void* bytes, unsigned count);
450 /* Output a row of bytes as a constant */
452 void g_zerobytes (unsigned n);
453 /* Output n bytes of data initialized with zero */
457 /*****************************************************************************/
458 /* User supplied assembler code */
459 /*****************************************************************************/
463 void g_asmcode (const char* Line, int Len);
464 /* Output one line of assembler code. If Len is greater than zero, it is used
465 * as the maximum number of characters to use from Line.
470 /*****************************************************************************/
471 /* Inlined known functions */
472 /*****************************************************************************/
476 void g_strlen (unsigned flags, unsigned long val, unsigned offs);
477 /* Inline the strlen() function */
481 /* End of codegen.h */