]> git.sur5r.net Git - cc65/blob - src/cc65/codegen.c
3d1bd3ffdf5edc70eae9d25f1850006a2d24f6f3
[cc65] / src / cc65 / codegen.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codegen.c                                 */
4 /*                                                                           */
5 /*                            6502 code generator                            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 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 #include <stdio.h>
37 #include <string.h>
38
39 /* common */
40 #include "check.h"
41 #include "version.h"
42 #include "xmalloc.h"
43
44 /* cc65 */
45 #include "asmcode.h"
46 #include "asmlabel.h"
47 #include "cpu.h"
48 #include "error.h"
49 #include "global.h"
50 #include "litpool.h"
51 #include "optimize.h"
52 #include "segname.h"
53 #include "util.h"
54 #include "codegen.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* Compiler relative stk ptr */
65 int oursp       = 0;
66
67 /* Current segment */
68 segment_t CurSeg = SEG_INV;
69
70 /* Segment names */
71 static char* SegmentHints [4] = {
72     "seg:code", "seg:rodata", "seg:data", "seg:bss"
73 };
74
75
76
77 /*****************************************************************************/
78 /*                                  Helpers                                  */
79 /*****************************************************************************/
80
81
82
83 static void typeerror (unsigned type)
84 /* Print an error message about an invalid operand type */
85 {
86     Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
87 }
88
89
90
91 static void CheckLocalOffs (unsigned Offs)
92 /* Check the offset into the stack for 8bit range */
93 {
94     if (Offs >= 256) {
95         /* Too many local vars */
96         AddCodeLine (";*** Too many locals");
97         Error ("Too many local variables");
98     }
99 }
100
101
102
103 static char* GetLabelName (unsigned flags, unsigned long label, unsigned offs)
104 {
105     static char lbuf [128];             /* Label name */
106
107     /* Create the correct label name */
108     switch (flags & CF_ADDRMASK) {
109
110         case CF_STATIC:
111             /* Static memory cell */
112             sprintf (lbuf, "L%04X+%u", (unsigned)(label & 0xFFFF), offs);
113             break;
114
115         case CF_EXTERNAL:
116             /* External label */
117             sprintf (lbuf, "_%s+%u", (char*) label, offs);
118             break;
119
120         case CF_ABSOLUTE:
121             /* Absolute address */
122             sprintf (lbuf, "$%04X", (unsigned)((label+offs) & 0xFFFF));
123             break;
124
125         case CF_REGVAR:
126             /* Variable in register bank */
127             sprintf (lbuf, "regbank+%u", (unsigned)((label+offs) & 0xFFFF));
128             break;
129
130         default:
131             Internal ("Invalid address flags");
132     }
133
134     /* Return a pointer to the static buffer */
135     return lbuf;
136 }
137
138
139
140 /*****************************************************************************/
141 /*                            Pre- and postamble                             */
142 /*****************************************************************************/
143
144
145
146 void g_preamble (void)
147 /* Generate the assembler code preamble */
148 {
149     AddCodeLine ("; File generated by cc65 v %u.%u.%u", VER_MAJOR, VER_MINOR, VER_PATCH);
150     AddEmptyLine ();
151
152     /* Insert some object file options */
153     AddCodeLine (".fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", VER_MAJOR, VER_MINOR, VER_PATCH);
154     AddEmptyLine ();
155
156     /* If we're producing code for some other CPU, switch the command set */
157     if (CPU == CPU_65C02) {
158         AddCodeLine (".pc02");
159     }
160
161     /* Allow auto import for runtime library routines */
162     AddCodeLine (".autoimport\ton");
163
164     /* Switch the assembler into case sensitive mode */
165     AddCodeLine (".case\t\ton");
166
167     /* Tell the assembler if we want to generate debug info */
168     AddCodeLine (".debuginfo\t%s", (DebugInfo != 0)? "on" : "off");
169
170     /* Import the stack pointer for direct auto variable access */
171     AddCodeLine (".importzp\tsp, sreg, regsave, regbank, tmp1, ptr1");
172
173     /* Define long branch macros */
174     AddCodeLine (".macpack\tlongbranch");
175     AddEmptyLine ();
176
177     /* Define the ldax macro */
178     AddCodeLine (".macro  ldax    Value");
179     AddCodeLine ("        lda     #<(Value)");
180     AddCodeLine ("        ldx     #>(Value)");
181     AddCodeLine (".endmacro");
182     AddEmptyLine ();
183
184     /* Tell the optimizer that this is the end of the preamble */
185     AddCodeHint ("end_of_preamble");
186 }
187
188
189
190 void g_postamble (void)
191 /* Generate assembler code postamble */
192 {
193     /* Tell the optimizer that this is the start of the postamble */
194     AddCodeHint ("start_of_postamble");
195 }
196
197
198
199 /*****************************************************************************/
200 /*                              Segment support                              */
201 /*****************************************************************************/
202
203
204
205 static void UseSeg (int NewSeg)
206 /* Switch to a specific segment */
207 {
208     if (CurSeg != NewSeg) {
209         CurSeg = NewSeg;
210         AddCodeLine (".segment\t\"%s\"", SegmentNames [CurSeg]);
211         AddCodeHint (SegmentHints [CurSeg]);
212     }
213 }
214
215
216
217 void g_usecode (void)
218 /* Switch to the code segment */
219 {
220     UseSeg (SEG_CODE);
221 }
222
223
224
225 void g_userodata (void)
226 /* Switch to the read only data segment */
227 {
228     UseSeg (SEG_RODATA);
229 }
230
231
232
233 void g_usedata (void)
234 /* Switch to the data segment */
235 {
236     UseSeg (SEG_DATA);
237 }
238
239
240
241 void g_usebss (void)
242 /* Switch to the bss segment */
243 {
244     UseSeg (SEG_BSS);
245 }
246
247
248
249 static void SegName (segment_t Seg, const char* Name)
250 /* Set the name of a segment */
251 {
252     /* Free the old name and set a new one */
253     NewSegName (Seg, Name);
254
255     /* If the new segment is the current segment, emit a segment directive
256      * with the new name.
257      */
258     if (Seg == CurSeg) {
259         CurSeg = SEG_INV;       /* Invalidate */
260         UseSeg (Seg);
261     }
262 }
263
264
265
266 void g_codename (const char* Name)
267 /* Set the name of the CODE segment */
268 {
269     SegName (SEG_CODE, Name);
270 }
271
272
273
274 void g_rodataname (const char* Name)
275 /* Set the name of the RODATA segment */
276 {
277     SegName (SEG_RODATA, Name);
278 }
279
280
281
282 void g_dataname (const char* Name)
283 /* Set the name of the DATA segment */
284 {
285     SegName (SEG_DATA, Name);
286 }
287
288
289
290 void g_bssname (const char* Name)
291 /* Set the name of the BSS segment */
292 {
293     SegName (SEG_BSS, Name);
294 }
295
296
297
298 /*****************************************************************************/
299 /*                                   Code                                    */
300 /*****************************************************************************/
301
302
303
304 unsigned sizeofarg (unsigned flags)
305 /* Return the size of a function argument type that is encoded in flags */
306 {
307     switch (flags & CF_TYPE) {
308
309         case CF_CHAR:
310             return (flags & CF_FORCECHAR)? 1 : 2;
311
312         case CF_INT:
313             return 2;
314
315         case CF_LONG:
316             return 4;
317
318         default:
319             typeerror (flags);
320             /* NOTREACHED */
321             return 2;
322     }
323 }
324
325
326
327 int pop (unsigned flags)
328 /* Pop an argument of the given size */
329 {
330     return oursp += sizeofarg (flags);
331 }
332
333
334
335 int push (unsigned flags)
336 /* Push an argument of the given size */
337 {
338     return oursp -= sizeofarg (flags);
339 }
340
341
342
343 static unsigned MakeByteOffs (unsigned Flags, unsigned Offs)
344 /* The value in Offs is an offset to an address in a/x. Make sure, an object
345  * of the type given in Flags can be loaded or stored into this address by
346  * adding part of the offset to the address in ax, so that the remaining
347  * offset fits into an index register. Return the remaining offset.
348  */
349 {
350     /* If the offset is too large for a byte register, add the high byte
351      * of the offset to the primary. Beware: We need a special correction
352      * if the offset in the low byte will overflow in the operation.
353      */
354     unsigned O = Offs & ~0xFFU;
355     if ((Offs & 0xFF) > 256 - sizeofarg (Flags)) {
356         /* We need to add the low byte also */
357         O += Offs & 0xFF;
358     }
359
360     /* Do the correction if we need one */
361     if (O != 0) {
362         g_inc (CF_INT | CF_CONST, O);
363         Offs -= O;
364     }
365
366     /* Return the new offset */
367     return Offs;
368 }
369
370
371
372 /*****************************************************************************/
373 /*                      Functions handling local labels                      */
374 /*****************************************************************************/
375
376
377
378 void g_defloclabel (unsigned label)
379 /* Define a local label */
380 {
381     AddCodeLine ("L%04X:", label & 0xFFFF);
382 }
383
384
385
386 /*****************************************************************************/
387 /*                     Functions handling global labels                      */
388 /*****************************************************************************/
389
390
391
392 void g_defgloblabel (const char* Name)
393 /* Define a global label with the given name */
394 {
395     AddCodeLine ("_%s:", Name);
396 }
397
398
399
400 void g_defexport (const char* Name, int ZP)
401 /* Export the given label */
402 {
403     if (ZP) {
404         AddCodeLine ("\t.exportzp\t_%s", Name);
405     } else {
406         AddCodeLine ("\t.export\t\t_%s", Name);
407     }
408 }
409
410
411
412 void g_defimport (const char* Name, int ZP)
413 /* Import the given label */
414 {
415     if (ZP) {
416         AddCodeLine ("\t.importzp\t_%s", Name);
417     } else {
418         AddCodeLine ("\t.import\t\t_%s", Name);
419     }
420 }
421
422
423
424 /*****************************************************************************/
425 /*                   Load functions for various registers                    */
426 /*****************************************************************************/
427
428
429
430 static void ldaconst (unsigned val)
431 /* Load a with a constant */
432 {
433     AddCodeLine ("\tlda\t#$%02X", val & 0xFF);
434 }
435
436
437
438 static void ldxconst (unsigned val)
439 /* Load x with a constant */
440 {
441     AddCodeLine ("\tldx\t#$%02X", val & 0xFF);
442 }
443
444
445
446 static void ldyconst (unsigned val)
447 /* Load y with a constant */
448 {
449     AddCodeLine ("\tldy\t#$%02X", val & 0xFF);
450 }
451
452
453
454 /*****************************************************************************/
455 /*                          Function entry and exit                          */
456 /*****************************************************************************/
457
458
459
460 /* Remember the argument size of a function. The variable is set by g_enter
461  * and used by g_leave. If the functions gets its argument size by the caller
462  * (variable param list or function without prototype), g_enter will set the
463  * value to -1.
464  */
465 static int funcargs;
466
467
468 void g_enter (unsigned flags, unsigned argsize)
469 /* Function prologue */
470 {
471     if ((flags & CF_FIXARGC) != 0) {
472         /* Just remember the argument size for the leave */
473         funcargs = argsize;
474     } else {
475         funcargs = -1;
476         AddCodeLine ("\tjsr\tenter");
477     }
478 }
479
480
481
482 void g_leave (int flags, int val)
483 /* Function epilogue */
484 {
485     int k;
486     char buf [40];
487
488     /* How many bytes of locals do we have to drop? */
489     k = -oursp;
490
491     /* If we didn't have a variable argument list, don't call leave */
492     if (funcargs >= 0) {
493
494         /* Load a function return code if needed */
495         if ((flags & CF_CONST) != 0) {
496             g_getimmed (flags, val, 0);
497         }
498
499         /* Drop stackframe or leave with rts */
500         k += funcargs;
501         if (k == 0) {
502             AddCodeLine ("\trts");
503         } else if (k <= 8) {
504             AddCodeLine ("\tjmp\tincsp%d", k);
505         } else {
506             CheckLocalOffs (k);
507             ldyconst (k);
508             AddCodeLine ("\tjmp\taddysp");
509         }
510
511     } else {
512
513         strcpy (buf, "\tjmp\tleave");
514         if (k) {
515             /* We've a stack frame to drop */
516             ldyconst (k);
517             strcat (buf, "y");
518         }
519         if (flags & CF_CONST) {
520             if ((flags & CF_TYPE) != CF_LONG) {
521                 /* Constant int sized value given for return code */
522                 if (val == 0) {
523                     /* Special case: return 0 */
524                     strcat (buf, "00");
525                 } else if (((val >> 8) & 0xFF) == 0) {
526                     /* Special case: constant with high byte zero */
527                     ldaconst (val);             /* Load low byte */
528                     strcat (buf, "0");
529                 } else {
530                     /* Others: arbitrary constant value */
531                     g_getimmed (flags, val, 0); /* Load value */
532                 }
533             } else {
534                 /* Constant long value: No shortcut possible */
535                 g_getimmed (flags, val, 0);
536             }
537         }
538
539         /* Output the jump */
540         AddCodeLine (buf);
541     }
542
543     /* Add an empty line  after a function to make the code more readable */
544     AddEmptyLine ();
545 }
546
547
548
549 /*****************************************************************************/
550 /*                            Register variables                             */
551 /*****************************************************************************/
552
553
554
555 void g_save_regvars (int RegOffs, unsigned Bytes)
556 /* Save register variables */
557 {
558     /* Don't loop for up to two bytes */
559     if (Bytes == 1) {
560
561         AddCodeLine ("\tlda\tregbank%+d", RegOffs);
562         AddCodeLine ("\tjsr\tpusha");
563
564     } else if (Bytes == 2) {
565
566         AddCodeLine ("\tlda\tregbank%+d", RegOffs);
567         AddCodeLine ("\tldx\tregbank%+d", RegOffs+1);
568         AddCodeLine ("\tjsr\tpushax");
569
570     } else {
571
572         /* More than two bytes - loop */
573         unsigned Label = GetLabel ();
574         g_space (Bytes);
575         ldyconst (Bytes - 1);
576         ldxconst (Bytes);
577         g_defloclabel (Label);
578         AddCodeLine ("\tlda\tregbank%+d,x", RegOffs-1);
579         AddCodeLine ("\tsta\t(sp),y");
580         AddCodeLine ("\tdey");
581         AddCodeLine ("\tdex");
582         AddCodeLine ("\tbne\tL%04X", Label);
583
584     }
585
586     /* We pushed stuff, correct the stack pointer */
587     oursp -= Bytes;
588 }
589
590
591
592 void g_restore_regvars (int StackOffs, int RegOffs, unsigned Bytes)
593 /* Restore register variables */
594 {
595     /* Calculate the actual stack offset and check it */
596     StackOffs -= oursp;
597     CheckLocalOffs (StackOffs);
598
599     /* Don't loop for up to two bytes */
600     if (Bytes == 1) {
601
602         ldyconst (StackOffs);
603         AddCodeLine ("\tlda\t(sp),y");
604         AddCodeLine ("\tsta\tregbank%+d", RegOffs);
605
606     } else if (Bytes == 2) {
607
608         ldyconst (StackOffs);
609         AddCodeLine ("\tlda\t(sp),y");
610         AddCodeLine ("\tsta\tregbank%+d", RegOffs);
611         AddCodeLine ("\tiny");
612         AddCodeLine ("\tlda\t(sp),y");
613         AddCodeLine ("\tsta\tregbank%+d", RegOffs+1);
614
615     } else {
616
617         /* More than two bytes - loop */
618         unsigned Label = GetLabel ();
619         ldyconst (StackOffs+Bytes-1);
620         ldxconst (Bytes);
621         g_defloclabel (Label);
622         AddCodeLine ("\tlda\t(sp),y");
623         AddCodeLine ("\tsta\tregbank%+d,x", RegOffs-1);
624         AddCodeLine ("\tdey");
625         AddCodeLine ("\tdex");
626         AddCodeLine ("\tbne\tL%04X", Label);
627
628     }
629 }
630
631
632
633 /*****************************************************************************/
634 /*                           Fetching memory cells                           */
635 /*****************************************************************************/
636
637
638
639 void g_getimmed (unsigned flags, unsigned long val, unsigned offs)
640 /* Load a constant into the primary register */
641 {
642     if ((flags & CF_CONST) != 0) {
643
644         /* Numeric constant */
645         switch (flags & CF_TYPE) {
646
647             case CF_CHAR:
648                 if ((flags & CF_FORCECHAR) != 0) {
649                     ldaconst (val);
650                     break;
651                 }
652                 /* FALL THROUGH */
653             case CF_INT:
654                 ldxconst ((val >> 8) & 0xFF);
655                 ldaconst (val & 0xFF);
656                 break;
657
658             case CF_LONG:
659                 if (val < 0x100) {
660                     AddCodeLine ("\tldx\t#$00");
661                     AddCodeLine ("\tstx\tsreg+1");
662                     AddCodeLine ("\tstx\tsreg");
663                     AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
664                 } else if ((val & 0xFFFF00FF) == 0) {
665                     AddCodeLine ("\tlda\t#$00");
666                     AddCodeLine ("\tsta\tsreg+1");
667                     AddCodeLine ("\tsta\tsreg");
668                     AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
669                 } else if ((val & 0xFFFF0000) == 0 && FavourSize == 0) {
670                     AddCodeLine ("\tlda\t#$00");
671                     AddCodeLine ("\tsta\tsreg+1");
672                     AddCodeLine ("\tsta\tsreg");
673                     AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
674                     AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
675                 } else if ((val & 0xFFFFFF00) == 0xFFFFFF00) {
676                     AddCodeLine ("\tldx\t#$FF");
677                     AddCodeLine ("\tstx\tsreg+1");
678                     AddCodeLine ("\tstx\tsreg");
679                     if ((val & 0xFF) == 0xFF) {
680                         AddCodeLine ("\ttxa");
681                     } else {
682                         AddCodeLine ("\tlda\t#$%02X", (unsigned char) val);
683                     }
684                 } else if ((val & 0xFFFF00FF) == 0xFFFF00FF) {
685                     AddCodeLine ("\tlda\t#$FF");
686                     AddCodeLine ("\tsta\tsreg+1");
687                     AddCodeLine ("\tsta\tsreg");
688                     AddCodeLine ("\tldx\t#$%02X", (unsigned char) (val >> 8));
689                 } else {
690                     /* Call a subroutine that will load following value */
691                     AddCodeLine ("\tjsr\tldeax");
692                     AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
693                 }
694                 break;
695
696             default:
697                 typeerror (flags);
698                 break;
699
700         }
701
702     } else {
703
704         /* Some sort of label */
705         const char* Label = GetLabelName (flags, val, offs);
706
707         /* Load the address into the primary */
708         AddCodeLine ("\tldax\t%s", Label);
709
710     }
711 }
712
713
714
715 void g_getstatic (unsigned flags, unsigned long label, unsigned offs)
716 /* Fetch an static memory cell into the primary register */
717 {
718     /* Create the correct label name */
719     char* lbuf = GetLabelName (flags, label, offs);
720
721     /* Check the size and generate the correct load operation */
722     switch (flags & CF_TYPE) {
723
724         case CF_CHAR:
725             if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
726                 AddCodeLine ("\tlda\t%s", lbuf);        /* load A from the label */
727             } else {
728                 ldxconst (0);
729                 AddCodeLine ("\tlda\t%s", lbuf);        /* load A from the label */
730                 if (!(flags & CF_UNSIGNED)) {
731                     /* Must sign extend */
732                     AddCodeLine ("\tbpl\t*+3");
733                     AddCodeLine ("\tdex");
734                     AddCodeHint ("x:!");                /* X is invalid now */
735                 }
736             }
737             break;
738
739         case CF_INT:
740             AddCodeLine ("\tlda\t%s", lbuf);
741             if (flags & CF_TEST) {
742                 AddCodeLine ("\tora\t%s+1", lbuf);
743             } else {
744                 AddCodeLine ("\tldx\t%s+1", lbuf);
745             }
746             break;
747
748         case CF_LONG:
749             if (flags & CF_TEST) {
750                 AddCodeLine ("\tlda\t%s+3", lbuf);
751                 AddCodeLine ("\tora\t%s+2", lbuf);
752                 AddCodeLine ("\tora\t%s+1", lbuf);
753                 AddCodeLine ("\tora\t%s+0", lbuf);
754             } else {
755                 AddCodeLine ("\tlda\t%s+3", lbuf);
756                 AddCodeLine ("\tsta\tsreg+1");
757                 AddCodeLine ("\tlda\t%s+2", lbuf);
758                 AddCodeLine ("\tsta\tsreg");
759                 AddCodeLine ("\tldx\t%s+1", lbuf);
760                 AddCodeLine ("\tlda\t%s", lbuf);
761             }
762             break;
763
764         default:
765             typeerror (flags);
766
767     }
768 }
769
770
771
772 void g_getlocal (unsigned flags, int offs)
773 /* Fetch specified local object (local var). */
774 {
775     offs -= oursp;
776     CheckLocalOffs (offs);
777     switch (flags & CF_TYPE) {
778
779         case CF_CHAR:
780             if ((flags & CF_FORCECHAR) || (flags & CF_TEST)) {
781                 if (CPU == CPU_65C02 && offs == 0) {
782                     AddCodeLine ("\tlda\t(sp)");
783                 } else {
784                     ldyconst (offs);
785                     AddCodeLine ("\tlda\t(sp),y");
786                 }
787             } else {
788                 if (offs == 0) {
789                     AddCodeLine ("\tldx\t#$00");
790                     AddCodeLine ("\tlda\t(sp,x)");
791                 } else {
792                     ldyconst (offs);
793                     AddCodeLine ("\tldx\t#$00");
794                     AddCodeLine ("\tlda\t(sp),y");
795                 }
796                 if ((flags & CF_UNSIGNED) == 0) {
797                     AddCodeLine ("\tbpl\t*+3");
798                     AddCodeLine ("\tdex");
799                     AddCodeHint ("x:!");        /* X is invalid now */
800                 }
801             }
802             break;
803
804         case CF_INT:
805             CheckLocalOffs (offs + 1);
806             if (flags & CF_TEST) {
807                 ldyconst (offs + 1);
808                 AddCodeLine ("\tlda\t(sp),y");
809                 AddCodeLine ("\tdey");
810                 AddCodeLine ("\tora\t(sp),y");
811             } else {
812                 if (FavourSize) {
813                     if (offs) {
814                         ldyconst (offs+1);
815                         AddCodeLine ("\tjsr\tldaxysp");
816                     } else {
817                         AddCodeLine ("\tjsr\tldax0sp");
818                     }
819                 } else {
820                     ldyconst (offs + 1);
821                     AddCodeLine ("\tlda\t(sp),y");
822                     AddCodeLine ("\ttax");
823                     AddCodeLine ("\tdey");
824                     AddCodeLine ("\tlda\t(sp),y");
825                 }
826             }
827             break;
828
829         case CF_LONG:
830             if (offs) {
831                 ldyconst (offs+3);
832                 AddCodeLine ("\tjsr\tldeaxysp");
833             } else {
834                 AddCodeLine ("\tjsr\tldeax0sp");
835             }
836             break;
837
838         default:
839             typeerror (flags);
840     }
841 }
842
843
844
845 void g_getind (unsigned flags, unsigned offs)
846 /* Fetch the specified object type indirect through the primary register
847  * into the primary register
848  */
849 {
850     /* If the offset is greater than 255, add the part that is > 255 to
851      * the primary. This way we get an easy addition and use the low byte
852      * as the offset
853      */
854     offs = MakeByteOffs (flags, offs);
855
856     /* Handle the indirect fetch */
857     switch (flags & CF_TYPE) {
858
859         case CF_CHAR:
860             /* Character sized */
861             if (offs) {
862                 ldyconst (offs);
863                 if (flags & CF_UNSIGNED) {
864                     AddCodeLine ("\tjsr\tldauidx");
865                 } else {
866                     AddCodeLine ("\tjsr\tldaidx");
867                 }
868             } else {
869                 if (flags & CF_UNSIGNED) {
870                     if (FavourSize) {
871                         AddCodeLine ("\tjsr\tldaui");
872                     } else {
873                         AddCodeLine ("\tsta\tptr1");
874                         AddCodeLine ("\tstx\tptr1+1");
875                         AddCodeLine ("\tldx\t#$00");
876                         AddCodeLine ("\tlda\t(ptr1,x)");
877                     }
878                 } else {
879                     AddCodeLine ("\tjsr\tldai");
880                 }
881             }
882             break;
883
884         case CF_INT:
885             if (flags & CF_TEST) {
886                 ldyconst (offs);
887                 AddCodeLine ("\tsta\tptr1");
888                 AddCodeLine ("\tstx\tptr1+1");
889                 AddCodeLine ("\tlda\t(ptr1),y");
890                 AddCodeLine ("\tiny");
891                 AddCodeLine ("\tora\t(ptr1),y");
892             } else {
893                 if (offs == 0) {
894                     AddCodeLine ("\tjsr\tldaxi");
895                 } else {
896                     ldyconst (offs+1);
897                     AddCodeLine ("\tjsr\tldaxidx");
898                 }
899             }
900             break;
901
902         case CF_LONG:
903             if (offs == 0) {
904                 AddCodeLine ("\tjsr\tldeaxi");
905             } else {
906                 ldyconst (offs+3);
907                 AddCodeLine ("\tjsr\tldeaxidx");
908             }
909             if (flags & CF_TEST) {
910                 AddCodeLine ("\tjsr\ttsteax");
911             }
912             break;
913
914         default:
915             typeerror (flags);
916
917     }
918 }
919
920
921
922 void g_leasp (int offs)
923 /* Fetch the address of the specified symbol into the primary register */
924 {
925     /* Calculate the offset relative to sp */
926     offs -= oursp;
927
928     /* For value 0 we do direct code */
929     if (offs == 0) {
930         AddCodeLine ("\tlda\tsp");
931         AddCodeLine ("\tldx\tsp+1");
932     } else {
933         if (FavourSize) {
934             ldaconst (offs);                    /* Load A with offset value */
935             AddCodeLine ("\tjsr\tleaasp");      /* Load effective address */
936         } else {
937             if (CPU == CPU_65C02 && offs == 1) {
938                 AddCodeLine ("\tlda\tsp");
939                 AddCodeLine ("\tldx\tsp+1");
940                 AddCodeLine ("\tina");
941                 AddCodeLine ("\tbne\t*+3");
942                 AddCodeLine ("\tinx");
943                 AddCodeHint ("x:!");            /* Invalidate X */
944             } else {
945                 ldaconst (offs);
946                 AddCodeLine ("\tclc");
947                 AddCodeLine ("\tldx\tsp+1");
948                 AddCodeLine ("\tadc\tsp");
949                 AddCodeLine ("\tbcc\t*+3");
950                 AddCodeLine ("\tinx");
951                 AddCodeHint ("x:!");            /* Invalidate X */
952             }
953         }
954     }
955 }
956
957
958
959 /*****************************************************************************/
960 /*                             Store into memory                             */
961 /*****************************************************************************/
962
963
964
965 void g_putstatic (unsigned flags, unsigned long label, unsigned offs)
966 /* Store the primary register into the specified static memory cell */
967 {
968     /* Create the correct label name */
969     char* lbuf = GetLabelName (flags, label, offs);
970
971     /* Check the size and generate the correct store operation */
972     switch (flags & CF_TYPE) {
973
974         case CF_CHAR:
975             AddCodeLine ("\tsta\t%s", lbuf);
976             break;
977
978         case CF_INT:
979             AddCodeLine ("\tsta\t%s", lbuf);
980             AddCodeLine ("\tstx\t%s+1", lbuf);
981             break;
982
983         case CF_LONG:
984             AddCodeLine ("\tsta\t%s", lbuf);
985             AddCodeLine ("\tstx\t%s+1", lbuf);
986             AddCodeLine ("\tldy\tsreg");
987             AddCodeLine ("\tsty\t%s+2", lbuf);
988             AddCodeLine ("\tldy\tsreg+1");
989             AddCodeLine ("\tsty\t%s+3", lbuf);
990             break;
991
992         default:
993             typeerror (flags);
994
995     }
996 }
997
998
999
1000 void g_putlocal (unsigned flags, int offs)
1001 /* Put data into local object. */
1002 {
1003     offs -= oursp;
1004     CheckLocalOffs (offs);
1005     switch (flags & CF_TYPE) {
1006
1007         case CF_CHAR:
1008             if (CPU == CPU_65C02 && offs == 0) {
1009                 AddCodeLine ("\tsta\t(sp)");
1010             } else {
1011                 ldyconst (offs);
1012                 AddCodeLine ("\tsta\t(sp),y");
1013             }
1014             break;
1015
1016         case CF_INT:
1017             if (offs) {
1018                 ldyconst (offs);
1019                 AddCodeLine ("\tjsr\tstaxysp");
1020             } else {
1021                 AddCodeLine ("\tjsr\tstax0sp");
1022             }
1023             break;
1024
1025         case CF_LONG:
1026             if (offs) {
1027                 ldyconst (offs);
1028                 AddCodeLine ("\tjsr\tsteaxysp");
1029             } else {
1030                 AddCodeLine ("\tjsr\tsteax0sp");
1031             }
1032             break;
1033
1034         default:
1035             typeerror (flags);
1036
1037     }
1038 }
1039
1040
1041
1042 void g_putind (unsigned Flags, unsigned Offs)
1043 /* Store the specified object type in the primary register at the address
1044  * on the top of the stack
1045  */
1046 {
1047     /* We can handle offsets below $100 directly, larger offsets must be added
1048      * to the address. Since a/x is in use, best code is achieved by adding
1049      * just the high byte. Be sure to check if the low byte will overflow while
1050      * while storing.
1051      */
1052     if ((Offs & 0xFF) > 256 - sizeofarg (Flags | CF_FORCECHAR)) {
1053
1054         /* Overflow - we need to add the low byte also */
1055         AddCodeLine ("\tldy\t#$00");
1056         AddCodeLine ("\tclc");
1057         AddCodeLine ("\tpha");
1058         AddCodeLine ("\tlda\t#$%02X", Offs & 0xFF);
1059         AddCodeLine ("\tadc\t(sp),y");
1060         AddCodeLine ("\tsta\t(sp),y");
1061         AddCodeLine ("\tiny");
1062         AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1063         AddCodeLine ("\tadc\t(sp),y");
1064         AddCodeLine ("\tsta\t(sp),y");
1065         AddCodeLine ("\tpla");
1066
1067         /* Complete address is on stack, new offset is zero */
1068         Offs = 0;
1069
1070     } else if ((Offs & 0xFF00) != 0) {
1071
1072         /* We can just add the high byte */
1073         AddCodeLine ("\tldy\t#$01");
1074         AddCodeLine ("\tclc");
1075         AddCodeLine ("\tpha");
1076         AddCodeLine ("\tlda\t#$%02X", (Offs >> 8) & 0xFF);
1077         AddCodeLine ("\tadc\t(sp),y");
1078         AddCodeLine ("\tsta\t(sp),y");
1079         AddCodeLine ("\tpla");
1080
1081         /* Offset is now just the low byte */
1082         Offs &= 0x00FF;
1083     }
1084
1085     /* Check the size and determine operation */
1086     switch (Flags & CF_TYPE) {
1087
1088         case CF_CHAR:
1089             if (Offs) {
1090                 ldyconst (Offs);
1091                 AddCodeLine ("\tjsr\tstaspidx");
1092             } else {
1093                 AddCodeLine ("\tjsr\tstaspp");
1094             }
1095             break;
1096
1097         case CF_INT:
1098             if (Offs) {
1099                 ldyconst (Offs);
1100                 AddCodeLine ("\tjsr\tstaxspidx");
1101             } else {
1102                 AddCodeLine ("\tjsr\tstaxspp");
1103             }
1104             break;
1105
1106         case CF_LONG:
1107             if (Offs) {
1108                 ldyconst (Offs);
1109                 AddCodeLine ("\tjsr\tsteaxspidx");
1110             } else {
1111                 AddCodeLine ("\tjsr\tsteaxspp");
1112             }
1113             break;
1114
1115         default:
1116             typeerror (Flags);
1117
1118     }
1119
1120     /* Pop the argument which is always a pointer */
1121     pop (CF_PTR);
1122 }
1123
1124
1125
1126 /*****************************************************************************/
1127 /*                    type conversion and similiar stuff                     */
1128 /*****************************************************************************/
1129
1130
1131
1132 void g_toslong (unsigned flags)
1133 /* Make sure, the value on TOS is a long. Convert if necessary */
1134 {
1135     switch (flags & CF_TYPE) {
1136
1137         case CF_CHAR:
1138         case CF_INT:
1139             if (flags & CF_UNSIGNED) {
1140                 AddCodeLine ("\tjsr\ttosulong");
1141             } else {
1142                 AddCodeLine ("\tjsr\ttoslong");
1143             }
1144             push (CF_INT);
1145             break;
1146
1147         case CF_LONG:
1148             break;
1149
1150         default:
1151             typeerror (flags);
1152     }
1153 }
1154
1155
1156
1157 void g_tosint (unsigned flags)
1158 /* Make sure, the value on TOS is an int. Convert if necessary */
1159 {
1160     switch (flags & CF_TYPE) {
1161
1162         case CF_CHAR:
1163         case CF_INT:
1164             break;
1165
1166         case CF_LONG:
1167             AddCodeLine ("\tjsr\ttosint");
1168             pop (CF_INT);
1169             break;
1170
1171         default:
1172             typeerror (flags);
1173     }
1174 }
1175
1176
1177
1178 void g_reglong (unsigned flags)
1179 /* Make sure, the value in the primary register a long. Convert if necessary */
1180 {
1181     switch (flags & CF_TYPE) {
1182
1183         case CF_CHAR:
1184         case CF_INT:
1185             if (flags & CF_UNSIGNED) {
1186                 if (FavourSize) {
1187                     AddCodeLine ("\tjsr\taxulong");
1188                 } else {
1189                     ldyconst (0);
1190                     AddCodeLine ("\tsty\tsreg");
1191                     AddCodeLine ("\tsty\tsreg+1");
1192                 }
1193             } else {
1194                 AddCodeLine ("\tjsr\taxlong");
1195             }
1196             break;
1197
1198         case CF_LONG:
1199             break;
1200
1201         default:
1202             typeerror (flags);
1203     }
1204 }
1205
1206
1207
1208 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1209 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1210  * value, that corresponds to the value on TOS, rhs corresponds to the value
1211  * in (e)ax. The return value is the the flags value for the resulting type.
1212  */
1213 {
1214     unsigned ltype, rtype;
1215     unsigned result;
1216
1217     /* Get the type spec from the flags */
1218     ltype = lhs & CF_TYPE;
1219     rtype = rhs & CF_TYPE;
1220
1221     /* Check if a conversion is needed */
1222     if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1223         /* We must promote the primary register to long */
1224         g_reglong (rhs);
1225         /* Get the new rhs type */
1226         rhs = (rhs & ~CF_TYPE) | CF_LONG;
1227         rtype = CF_LONG;
1228     } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1229         /* We must promote the lhs to long */
1230         if (lhs & CF_REG) {
1231             g_reglong (lhs);
1232         } else {
1233             g_toslong (lhs);
1234         }
1235         /* Get the new rhs type */
1236         lhs = (lhs & ~CF_TYPE) | CF_LONG;
1237         ltype = CF_LONG;
1238     }
1239
1240     /* Determine the result type for the operation:
1241      *  - The result is const if both operands are const.
1242      *  - The result is unsigned if one of the operands is unsigned.
1243      *  - The result is long if one of the operands is long.
1244      *  - Otherwise the result is int sized.
1245      */
1246     result = (lhs & CF_CONST) & (rhs & CF_CONST);
1247     result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1248     if (rtype == CF_LONG || ltype == CF_LONG) {
1249         result |= CF_LONG;
1250     } else {
1251         result |= CF_INT;
1252     }
1253     return result;
1254 }
1255
1256
1257
1258 unsigned g_typecast (unsigned lhs, unsigned rhs)
1259 /* Cast the value in the primary register to the operand size that is flagged
1260  * by the lhs value. Return the result value.
1261  */
1262 {
1263     unsigned ltype, rtype;
1264
1265     /* Get the type spec from the flags */
1266     ltype = lhs & CF_TYPE;
1267     rtype = rhs & CF_TYPE;
1268
1269     /* Check if a conversion is needed */
1270     if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1271         /* We must promote the primary register to long */
1272         g_reglong (rhs);
1273     }
1274
1275     /* Do not need any other action. If the left type is int, and the primary
1276      * register is long, it will be automagically truncated. If the right hand
1277      * side is const, it is not located in the primary register and handled by
1278      * the expression parser code.
1279      */
1280
1281     /* Result is const if the right hand side was const */
1282     lhs |= (rhs & CF_CONST);
1283
1284     /* The resulting type is that of the left hand side (that's why you called
1285      * this function :-)
1286      */
1287     return lhs;
1288 }
1289
1290
1291
1292 void g_scale (unsigned flags, long val)
1293 /* Scale the value in the primary register by the given value. If val is positive,
1294  * scale up, is val is negative, scale down. This function is used to scale
1295  * the operands or results of pointer arithmetic by the size of the type, the
1296  * pointer points to.
1297  */
1298 {
1299     int p2;
1300
1301     /* Value may not be zero */
1302     if (val == 0) {
1303         Internal ("Data type has no size");
1304     } else if (val > 0) {
1305
1306         /* Scale up */
1307         if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1308
1309             /* Factor is 2, 4 or 8, use special function */
1310             switch (flags & CF_TYPE) {
1311
1312                 case CF_CHAR:
1313                     if (flags & CF_FORCECHAR) {
1314                         while (p2--) {
1315                             AddCodeLine ("\tasl\ta");
1316                         }
1317                         break;
1318                     }
1319                     /* FALLTHROUGH */
1320
1321                 case CF_INT:
1322                     if (FavourSize || p2 >= 3) {
1323                         if (flags & CF_UNSIGNED) {
1324                             AddCodeLine ("\tjsr\tshlax%d", p2);
1325                         } else {
1326                             AddCodeLine ("\tjsr\taslax%d", p2);
1327                         }
1328                     } else {
1329                         AddCodeLine ("\tstx\ttmp1");
1330                         while (p2--) {
1331                             AddCodeLine ("\tasl\ta");
1332                             AddCodeLine ("\trol\ttmp1");
1333                         }
1334                         AddCodeLine ("\tldx\ttmp1");
1335                     }
1336                     break;
1337
1338                 case CF_LONG:
1339                     if (flags & CF_UNSIGNED) {
1340                         AddCodeLine ("\tjsr\tshleax%d", p2);
1341                     } else {
1342                         AddCodeLine ("\tjsr\tasleax%d", p2);
1343                     }
1344                     break;
1345
1346                 default:
1347                     typeerror (flags);
1348
1349             }
1350
1351         } else if (val != 1) {
1352
1353             /* Use a multiplication instead */
1354             g_mul (flags | CF_CONST, val);
1355
1356         }
1357
1358     } else {
1359
1360         /* Scale down */
1361         val = -val;
1362         if ((p2 = powerof2 (val)) > 0 && p2 <= 3) {
1363
1364             /* Factor is 2, 4 or 8, use special function */
1365             switch (flags & CF_TYPE) {
1366
1367                 case CF_CHAR:
1368                     if (flags & CF_FORCECHAR) {
1369                         if (flags & CF_UNSIGNED) {
1370                             while (p2--) {
1371                                 AddCodeLine ("\tlsr\ta");
1372                             }
1373                             break;
1374                         } else if (p2 <= 2) {
1375                             AddCodeLine ("\tcmp\t#$80");
1376                             AddCodeLine ("\tror\ta");
1377                             break;
1378                         }
1379                     }
1380                     /* FALLTHROUGH */
1381
1382                 case CF_INT:
1383                     if (flags & CF_UNSIGNED) {
1384                         if (FavourSize || p2 >= 3) {
1385                             AddCodeLine ("\tjsr\tlsrax%d", p2);
1386                         } else {
1387                             AddCodeLine ("\tstx\ttmp1");
1388                             while (p2--) {
1389                                 AddCodeLine ("\tlsr\ttmp1");
1390                                 AddCodeLine ("\tror\ta");
1391                             }
1392                             AddCodeLine ("\tldx\ttmp1");
1393                         }
1394                     } else {
1395                         if (FavourSize || p2 >= 3) {
1396                             AddCodeLine ("\tjsr\tasrax%d", p2);
1397                         } else {
1398                             AddCodeLine ("\tstx\ttmp1");
1399                             while (p2--) {
1400                                 AddCodeLine ("\tcpx\t#$80");
1401                                 AddCodeLine ("\tror\ttmp1");
1402                                 AddCodeLine ("\tror\ta");
1403                             }
1404                             AddCodeLine ("\tldx\ttmp1");
1405                         }
1406                     }
1407                     break;
1408
1409                 case CF_LONG:
1410                     if (flags & CF_UNSIGNED) {
1411                         AddCodeLine ("\tjsr\tlsreax%d", p2);
1412                     } else {
1413                         AddCodeLine ("\tjsr\tasreax%d", p2);
1414                     }
1415                     break;
1416
1417                 default:
1418                     typeerror (flags);
1419
1420             }
1421
1422         } else if (val != 1) {
1423
1424             /* Use a division instead */
1425             g_div (flags | CF_CONST, val);
1426
1427         }
1428     }
1429 }
1430
1431
1432
1433 /*****************************************************************************/
1434 /*              Adds and subs of variables fix a fixed address               */
1435 /*****************************************************************************/
1436
1437
1438
1439 void g_addlocal (unsigned flags, int offs)
1440 /* Add a local variable to ax */
1441 {
1442     /* Correct the offset and check it */
1443     offs -= oursp;
1444     CheckLocalOffs (offs);
1445
1446     switch (flags & CF_TYPE) {
1447
1448         case CF_CHAR:
1449             AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1450             AddCodeLine ("\tclc");
1451             AddCodeLine ("\tadc\t(sp),y");
1452             AddCodeLine ("\tbcc\t*+3");
1453             AddCodeLine ("\tinx");
1454             AddCodeHint ("x:!");
1455             break;
1456
1457         case CF_INT:
1458             AddCodeLine ("\tldy\t#$%02X", offs & 0xFF);
1459             AddCodeLine ("\tclc");
1460             AddCodeLine ("\tadc\t(sp),y");
1461             AddCodeLine ("\tpha");
1462             AddCodeLine ("\ttxa");
1463             AddCodeLine ("\tiny");
1464             AddCodeLine ("\tadc\t(sp),y");
1465             AddCodeLine ("\ttax");
1466             AddCodeLine ("\tpla");
1467             break;
1468
1469         case CF_LONG:
1470             /* Do it the old way */
1471             g_push (flags, 0);
1472             g_getlocal (flags, offs);
1473             g_add (flags, 0);
1474             break;
1475
1476         default:
1477             typeerror (flags);
1478
1479     }
1480 }
1481
1482
1483
1484 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1485 /* Add a static variable to ax */
1486 {
1487     /* Create the correct label name */
1488     char* lbuf = GetLabelName (flags, label, offs);
1489
1490     switch (flags & CF_TYPE) {
1491
1492         case CF_CHAR:
1493             AddCodeLine ("\tclc");
1494             AddCodeLine ("\tadc\t%s", lbuf);
1495             AddCodeLine ("\tbcc\t*+3");
1496             AddCodeLine ("\tinx");
1497             AddCodeHint ("x:!");
1498             break;
1499
1500         case CF_INT:
1501             AddCodeLine ("\tclc");
1502             AddCodeLine ("\tadc\t%s", lbuf);
1503             AddCodeLine ("\ttay");
1504             AddCodeLine ("\ttxa");
1505             AddCodeLine ("\tadc\t%s+1", lbuf);
1506             AddCodeLine ("\ttax");
1507             AddCodeLine ("\ttya");
1508             break;
1509
1510         case CF_LONG:
1511             /* Do it the old way */
1512             g_push (flags, 0);
1513             g_getstatic (flags, label, offs);
1514             g_add (flags, 0);
1515             break;
1516
1517         default:
1518             typeerror (flags);
1519
1520     }
1521 }
1522
1523
1524
1525 /*****************************************************************************/
1526 /*             Compares of ax with a variable with fixed address             */
1527 /*****************************************************************************/
1528
1529
1530
1531 void g_cmplocal (unsigned flags, int offs)
1532 /* Compare a local variable to ax */
1533 {
1534     Internal ("g_cmplocal not implemented");
1535 }
1536
1537
1538
1539 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1540 /* Compare a static variable to ax */
1541 {
1542     Internal ("g_cmpstatic not implemented");
1543 }
1544
1545
1546
1547 /*****************************************************************************/
1548 /*                           Special op= functions                           */
1549 /*****************************************************************************/
1550
1551
1552
1553 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1554                     unsigned long val)
1555 /* Emit += for a static variable */
1556 {
1557     /* Create the correct label name */
1558     char* lbuf = GetLabelName (flags, label, offs);
1559
1560     /* Check the size and determine operation */
1561     switch (flags & CF_TYPE) {
1562
1563         case CF_CHAR:
1564             if (flags & CF_FORCECHAR) {
1565                 AddCodeLine ("\tldx\t#$00");
1566                 if (flags & CF_CONST) {
1567                     if (val == 1) {
1568                         AddCodeLine ("\tinc\t%s", lbuf);
1569                         AddCodeLine ("\tlda\t%s", lbuf);
1570                     } else {
1571                         AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1572                         AddCodeLine ("\tclc");
1573                         AddCodeLine ("\tadc\t%s", lbuf);
1574                         AddCodeLine ("\tsta\t%s", lbuf);
1575                     }
1576                 } else {
1577                     AddCodeLine ("\tclc");
1578                     AddCodeLine ("\tadc\t%s", lbuf);
1579                     AddCodeLine ("\tsta\t%s", lbuf);
1580                 }
1581                 if ((flags & CF_UNSIGNED) == 0) {
1582                     AddCodeLine ("\tbpl\t*+3");
1583                     AddCodeLine ("\tdex");
1584                     AddCodeHint ("x:!");                /* Invalidate X */
1585                 }
1586                 break;
1587             }
1588             /* FALLTHROUGH */
1589
1590         case CF_INT:
1591             if (flags & CF_CONST) {
1592                 if (val == 1) {
1593                     label = GetLabel ();
1594                     AddCodeLine ("\tinc\t%s", lbuf);
1595                     AddCodeLine ("\tbne\tL%04X", (int)label);
1596                     AddCodeLine ("\tinc\t%s+1", lbuf);
1597                     g_defloclabel (label);
1598                     AddCodeLine ("\tlda\t%s", lbuf);            /* Hmmm... */
1599                     AddCodeLine ("\tldx\t%s+1", lbuf);
1600                 } else {
1601                     AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1602                     AddCodeLine ("\tclc");
1603                     AddCodeLine ("\tadc\t%s", lbuf);
1604                     AddCodeLine ("\tsta\t%s", lbuf);
1605                     if (val < 0x100) {
1606                         label = GetLabel ();
1607                         AddCodeLine ("\tbcc\tL%04X", (int)label);
1608                         AddCodeLine ("\tinc\t%s+1", lbuf);
1609                         g_defloclabel (label);
1610                         AddCodeLine ("\tldx\t%s+1", lbuf);
1611                     } else {
1612                         AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1613                         AddCodeLine ("\tadc\t%s+1", lbuf);
1614                         AddCodeLine ("\tsta\t%s+1", lbuf);
1615                         AddCodeLine ("\ttax");
1616                         AddCodeLine ("\tlda\t%s", lbuf);
1617                     }
1618                 }
1619             } else {
1620                 AddCodeLine ("\tclc");
1621                 AddCodeLine ("\tadc\t%s", lbuf);
1622                 AddCodeLine ("\tsta\t%s", lbuf);
1623                 AddCodeLine ("\ttxa");
1624                 AddCodeLine ("\tadc\t%s+1", lbuf);
1625                 AddCodeLine ("\tsta\t%s+1", lbuf);
1626                 AddCodeLine ("\ttax");
1627                 AddCodeLine ("\tlda\t%s", lbuf);
1628             }
1629             break;
1630
1631         case CF_LONG:
1632             if (flags & CF_CONST) {
1633                 if (val < 0x100) {
1634                     AddCodeLine ("\tldy\t#<(%s)", lbuf);
1635                     AddCodeLine ("\tsty\tptr1");
1636                     AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1637                     if (val == 1) {
1638                         AddCodeLine ("\tjsr\tladdeq1");
1639                     } else {
1640                         AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1641                         AddCodeLine ("\tjsr\tladdeqa");
1642                     }
1643                 } else {
1644                     g_getstatic (flags, label, offs);
1645                     g_inc (flags, val);
1646                     g_putstatic (flags, label, offs);
1647                 }
1648             } else {
1649                 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1650                 AddCodeLine ("\tsty\tptr1");
1651                 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1652                 AddCodeLine ("\tjsr\tladdeq");
1653             }
1654             break;
1655
1656         default:
1657             typeerror (flags);
1658     }
1659 }
1660
1661
1662
1663 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1664 /* Emit += for a local variable */
1665 {
1666     /* Calculate the true offset, check it, load it into Y */
1667     offs -= oursp;
1668     CheckLocalOffs (offs);
1669
1670     /* Check the size and determine operation */
1671     switch (flags & CF_TYPE) {
1672
1673         case CF_CHAR:
1674             if (flags & CF_FORCECHAR) {
1675                 if (offs == 0) {
1676                     AddCodeLine ("\tldx\t#$00");
1677                     if (flags & CF_CONST) {
1678                         AddCodeLine ("\tclc");
1679                         AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1680                         AddCodeLine ("\tadc\t(sp,x)");
1681                         AddCodeLine ("\tsta\t(sp,x)");
1682                     } else {
1683                         AddCodeLine ("\tclc");
1684                         AddCodeLine ("\tadc\t(sp,x)");
1685                         AddCodeLine ("\tsta\t(sp,x)");
1686                     }
1687                 } else {
1688                     ldyconst (offs);
1689                     AddCodeLine ("\tldx\t#$00");
1690                     if (flags & CF_CONST) {
1691                         AddCodeLine ("\tclc");
1692                         AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1693                         AddCodeLine ("\tadc\t(sp),y");
1694                         AddCodeLine ("\tsta\t(sp),y");
1695                     } else {
1696                         AddCodeLine ("\tclc");
1697                         AddCodeLine ("\tadc\t(sp),y");
1698                         AddCodeLine ("\tsta\t(sp),y");
1699                     }
1700                 }
1701                 if ((flags & CF_UNSIGNED) == 0) {
1702                     AddCodeLine ("\tbpl\t*+3");
1703                     AddCodeLine ("\tdex");
1704                     AddCodeHint ("x:!");        /* Invalidate X */
1705                 }
1706                 break;
1707             }
1708             /* FALLTHROUGH */
1709
1710         case CF_INT:
1711             if (flags & CF_CONST) {
1712                 g_getimmed (flags, val, 0);
1713             }
1714             if (offs == 0) {
1715                 AddCodeLine ("\tjsr\taddeq0sp");
1716             } else {
1717                 ldyconst (offs);
1718                 AddCodeLine ("\tjsr\taddeqysp");
1719             }
1720             break;
1721
1722         case CF_LONG:
1723             if (flags & CF_CONST) {
1724                 g_getimmed (flags, val, 0);
1725             }
1726             if (offs == 0) {
1727                 AddCodeLine ("\tjsr\tladdeq0sp");
1728             } else {
1729                 ldyconst (offs);
1730                 AddCodeLine ("\tjsr\tladdeqysp");
1731             }
1732             break;
1733
1734         default:
1735             typeerror (flags);
1736     }
1737 }
1738
1739
1740
1741 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1742 /* Emit += for the location with address in ax */
1743 {
1744     /* If the offset is too large for a byte register, add the high byte
1745      * of the offset to the primary. Beware: We need a special correction
1746      * if the offset in the low byte will overflow in the operation.
1747      */
1748     offs = MakeByteOffs (flags, offs);
1749
1750     /* Check the size and determine operation */
1751     switch (flags & CF_TYPE) {
1752
1753         case CF_CHAR:
1754             AddCodeLine ("\tsta\tptr1");
1755             AddCodeLine ("\tstx\tptr1+1");
1756             if (offs == 0) {
1757                 AddCodeLine ("\tldx\t#$00");
1758                 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1759                 AddCodeLine ("\tclc");
1760                 AddCodeLine ("\tadc\t(ptr1,x)");
1761                 AddCodeLine ("\tsta\t(ptr1,x)");
1762             } else {
1763                 AddCodeLine ("\tldy\t#$%02X", offs);
1764                 AddCodeLine ("\tldx\t#$00");
1765                 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1766                 AddCodeLine ("\tclc");
1767                 AddCodeLine ("\tadc\t(ptr1),y");
1768                 AddCodeLine ("\tsta\t(ptr1),y");
1769             }
1770             break;
1771
1772         case CF_INT:
1773             if (!FavourSize) {
1774                 /* Lots of code, use only if size is not important */
1775                 AddCodeLine ("\tsta\tptr1");
1776                 AddCodeLine ("\tstx\tptr1+1");
1777                 AddCodeLine ("\tldy\t#$%02X", offs);
1778                 AddCodeLine ("\tlda\t#$%02X", (int)(val & 0xFF));
1779                 AddCodeLine ("\tclc");
1780                 AddCodeLine ("\tadc\t(ptr1),y");
1781                 AddCodeLine ("\tsta\t(ptr1),y");
1782                 AddCodeLine ("\tpha");
1783                 AddCodeLine ("\tiny");
1784                 AddCodeLine ("\tlda\t#$%02X", (unsigned char)(val >> 8));
1785                 AddCodeLine ("\tadc\t(ptr1),y");
1786                 AddCodeLine ("\tsta\t(ptr1),y");
1787                 AddCodeLine ("\ttax");
1788                 AddCodeLine ("\tpla");
1789                 break;
1790             }
1791             /* FALL THROUGH */
1792
1793         case CF_LONG:
1794             AddCodeLine ("\tjsr\tpushax");      /* Push the address */
1795             push (flags);                       /* Correct the internal sp */
1796             g_getind (flags, offs);             /* Fetch the value */
1797             g_inc (flags, val);                 /* Increment value in primary */
1798             g_putind (flags, offs);             /* Store the value back */
1799             break;
1800
1801         default:
1802             typeerror (flags);
1803     }
1804 }
1805
1806
1807
1808 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1809                     unsigned long val)
1810 /* Emit -= for a static variable */
1811 {
1812     /* Create the correct label name */
1813     char* lbuf = GetLabelName (flags, label, offs);
1814
1815     /* Check the size and determine operation */
1816     switch (flags & CF_TYPE) {
1817
1818         case CF_CHAR:
1819             if (flags & CF_FORCECHAR) {
1820                 AddCodeLine ("\tldx\t#$00");
1821                 if (flags & CF_CONST) {
1822                     if (val == 1) {
1823                         AddCodeLine ("\tdec\t%s", lbuf);
1824                         AddCodeLine ("\tlda\t%s", lbuf);
1825                     } else {
1826                         AddCodeLine ("\tsec");
1827                         AddCodeLine ("\tlda\t%s", lbuf);
1828                         AddCodeLine ("\tsbc\t#$%02X", (int)(val & 0xFF));
1829                         AddCodeLine ("\tsta\t%s", lbuf);
1830                     }
1831                 } else {
1832                     AddCodeLine ("\tsec");
1833                     AddCodeLine ("\tsta\ttmp1");
1834                     AddCodeLine ("\tlda\t%s", lbuf);
1835                     AddCodeLine ("\tsbc\ttmp1");
1836                     AddCodeLine ("\tsta\t%s", lbuf);
1837                 }
1838                 if ((flags & CF_UNSIGNED) == 0) {
1839                     AddCodeLine ("\tbpl\t*+3");
1840                     AddCodeLine ("\tdex");
1841                     AddCodeHint ("x:!");                /* Invalidate X */
1842                 }
1843                 break;
1844             }
1845             /* FALLTHROUGH */
1846
1847         case CF_INT:
1848             AddCodeLine ("\tsec");
1849             if (flags & CF_CONST) {
1850                 AddCodeLine ("\tlda\t%s", lbuf);
1851                 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1852                 AddCodeLine ("\tsta\t%s", lbuf);
1853                 if (val < 0x100) {
1854                     label = GetLabel ();
1855                     AddCodeLine ("\tbcs\tL%04X", (unsigned)label);
1856                     AddCodeLine ("\tdec\t%s+1", lbuf);
1857                     g_defloclabel (label);
1858                     AddCodeLine ("\tldx\t%s+1", lbuf);
1859                 } else {
1860                     AddCodeLine ("\tlda\t%s+1", lbuf);
1861                     AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
1862                     AddCodeLine ("\tsta\t%s+1", lbuf);
1863                     AddCodeLine ("\ttax");
1864                     AddCodeLine ("\tlda\t%s", lbuf);
1865                 }
1866             } else {
1867                 AddCodeLine ("\tsta\ttmp1");
1868                 AddCodeLine ("\tlda\t%s", lbuf);
1869                 AddCodeLine ("\tsbc\ttmp1");
1870                 AddCodeLine ("\tsta\t%s", lbuf);
1871                 AddCodeLine ("\tstx\ttmp1");
1872                 AddCodeLine ("\tlda\t%s+1", lbuf);
1873                 AddCodeLine ("\tsbc\ttmp1");
1874                 AddCodeLine ("\tsta\t%s+1", lbuf);
1875                 AddCodeLine ("\ttax");
1876                 AddCodeLine ("\tlda\t%s", lbuf);
1877             }
1878             break;
1879
1880         case CF_LONG:
1881             if (flags & CF_CONST) {
1882                 if (val < 0x100) {
1883                     AddCodeLine ("\tldy\t#<(%s)", lbuf);
1884                     AddCodeLine ("\tsty\tptr1");
1885                     AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1886                     if (val == 1) {
1887                         AddCodeLine ("\tjsr\tlsubeq1");
1888                     } else {
1889                         AddCodeLine ("\tlda\t#$%02X", (unsigned char)val);
1890                         AddCodeLine ("\tjsr\tlsubeqa");
1891                     }
1892                 } else {
1893                     g_getstatic (flags, label, offs);
1894                     g_dec (flags, val);
1895                     g_putstatic (flags, label, offs);
1896                 }
1897             } else {
1898                 AddCodeLine ("\tldy\t#<(%s)", lbuf);
1899                 AddCodeLine ("\tsty\tptr1");
1900                 AddCodeLine ("\tldy\t#>(%s+1)", lbuf);
1901                 AddCodeLine ("\tjsr\tlsubeq");
1902             }
1903             break;
1904
1905         default:
1906             typeerror (flags);
1907     }
1908 }
1909
1910
1911
1912 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1913 /* Emit -= for a local variable */
1914 {
1915     /* Calculate the true offset, check it, load it into Y */
1916     offs -= oursp;
1917     CheckLocalOffs (offs);
1918
1919     /* Check the size and determine operation */
1920     switch (flags & CF_TYPE) {
1921
1922         case CF_CHAR:
1923             if (flags & CF_FORCECHAR) {
1924                 ldyconst (offs);
1925                 AddCodeLine ("\tldx\t#$00");
1926                 AddCodeLine ("\tsec");
1927                 if (flags & CF_CONST) {
1928                     AddCodeLine ("\tlda\t(sp),y");
1929                     AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1930                 } else {
1931                     AddCodeLine ("\tsta\ttmp1");
1932                     AddCodeLine ("\tlda\t(sp),y");
1933                     AddCodeLine ("\tsbc\ttmp1");
1934                 }
1935                 AddCodeLine ("\tsta\t(sp),y");
1936                 if ((flags & CF_UNSIGNED) == 0) {
1937                     AddCodeLine ("\tbpl\t*+3");
1938                     AddCodeLine ("\tdex");
1939                     AddCodeHint ("x:!");                /* Invalidate X */
1940                 }
1941                 break;
1942             }
1943             /* FALLTHROUGH */
1944
1945         case CF_INT:
1946             if (flags & CF_CONST) {
1947                 g_getimmed (flags, val, 0);
1948             }
1949             if (offs == 0) {
1950                 AddCodeLine ("\tjsr\tsubeq0sp");
1951             } else {
1952                 ldyconst (offs);
1953                 AddCodeLine ("\tjsr\tsubeqysp");
1954             }
1955             break;
1956
1957         case CF_LONG:
1958             if (flags & CF_CONST) {
1959                 g_getimmed (flags, val, 0);
1960             }
1961             if (offs == 0) {
1962                 AddCodeLine ("\tjsr\tlsubeq0sp");
1963             } else {
1964                 ldyconst (offs);
1965                 AddCodeLine ("\tjsr\tlsubeqysp");
1966             }
1967             break;
1968
1969         default:
1970             typeerror (flags);
1971     }
1972 }
1973
1974
1975
1976 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1977 /* Emit -= for the location with address in ax */
1978 {
1979     /* If the offset is too large for a byte register, add the high byte
1980      * of the offset to the primary. Beware: We need a special correction
1981      * if the offset in the low byte will overflow in the operation.
1982      */
1983     offs = MakeByteOffs (flags, offs);
1984
1985     /* Check the size and determine operation */
1986     switch (flags & CF_TYPE) {
1987
1988         case CF_CHAR:
1989             AddCodeLine ("\tsta\tptr1");
1990             AddCodeLine ("\tstx\tptr1+1");
1991             if (offs == 0) {
1992                 AddCodeLine ("\tldx\t#$00");
1993                 AddCodeLine ("\tlda\t(ptr1,x)");
1994                 AddCodeLine ("\tsec");
1995                 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
1996                 AddCodeLine ("\tsta\t(ptr1,x)");
1997             } else {
1998                 AddCodeLine ("\tldy\t#$%02X", offs);
1999                 AddCodeLine ("\tldx\t#$00");
2000                 AddCodeLine ("\tlda\t(ptr1),y");
2001                 AddCodeLine ("\tsec");
2002                 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2003                 AddCodeLine ("\tsta\t(ptr1),y");
2004             }
2005             break;
2006
2007         case CF_INT:
2008             if (!FavourSize) {
2009                 /* Lots of code, use only if size is not important */
2010                 AddCodeLine ("\tsta\tptr1");
2011                 AddCodeLine ("\tstx\tptr1+1");
2012                 AddCodeLine ("\tldy\t#$%02X", offs);
2013                 AddCodeLine ("\tlda\t(ptr1),y");
2014                 AddCodeLine ("\tsec");
2015                 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
2016                 AddCodeLine ("\tsta\t(ptr1),y");
2017                 AddCodeLine ("\tpha");
2018                 AddCodeLine ("\tiny");
2019                 AddCodeLine ("\tlda\t(ptr1),y");
2020                 AddCodeLine ("\tsbc\t#$%02X", (unsigned char)(val >> 8));
2021                 AddCodeLine ("\tsta\t(ptr1),y");
2022                 AddCodeLine ("\ttax");
2023                 AddCodeLine ("\tpla");
2024                 break;
2025             }
2026             /* FALL THROUGH */
2027
2028         case CF_LONG:
2029             AddCodeLine ("\tjsr\tpushax");      /* Push the address */
2030             push (flags);                       /* Correct the internal sp */
2031             g_getind (flags, offs);             /* Fetch the value */
2032             g_dec (flags, val);                 /* Increment value in primary */
2033             g_putind (flags, offs);             /* Store the value back */
2034             break;
2035
2036         default:
2037             typeerror (flags);
2038     }
2039 }
2040
2041
2042
2043 /*****************************************************************************/
2044 /*                 Add a variable address to the value in ax                 */
2045 /*****************************************************************************/
2046
2047
2048
2049 void g_addaddr_local (unsigned flags, int offs)
2050 /* Add the address of a local variable to ax */
2051 {
2052     /* Add the offset */
2053     offs -= oursp;
2054     if (offs != 0) {
2055         /* We cannot address more then 256 bytes of locals anyway */
2056         CheckLocalOffs (offs);
2057         AddCodeLine ("\tclc");
2058         AddCodeLine ("\tadc\t#$%02X", offs & 0xFF);
2059         AddCodeLine ("\tbcc\t*+4");     /* Do also skip the CLC insn below */
2060         AddCodeLine ("\tinx");
2061         AddCodeHint ("x:!");                    /* Invalidate X */
2062     }
2063
2064     /* Add the current stackpointer value */
2065     AddCodeLine ("\tclc");
2066     AddCodeLine ("\tadc\tsp");
2067     AddCodeLine ("\ttay");
2068     AddCodeLine ("\ttxa");
2069     AddCodeLine ("\tadc\tsp+1");
2070     AddCodeLine ("\ttax");
2071     AddCodeLine ("\ttya");
2072 }
2073
2074
2075
2076 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2077 /* Add the address of a static variable to ax */
2078 {
2079     /* Create the correct label name */
2080     char* lbuf = GetLabelName (flags, label, offs);
2081
2082     /* Add the address to the current ax value */
2083     AddCodeLine ("\tclc");
2084     AddCodeLine ("\tadc\t#<(%s)", lbuf);
2085     AddCodeLine ("\ttay");
2086     AddCodeLine ("\ttxa");
2087     AddCodeLine ("\tadc\t#>(%s)", lbuf);
2088     AddCodeLine ("\ttax");
2089     AddCodeLine ("\ttya");
2090 }
2091
2092
2093
2094 /*****************************************************************************/
2095 /*                                                                           */
2096 /*****************************************************************************/
2097
2098
2099
2100 void g_save (unsigned flags)
2101 /* Copy primary register to hold register. */
2102 {
2103     /* Check the size and determine operation */
2104     switch (flags & CF_TYPE) {
2105
2106         case CF_CHAR:
2107             if (flags & CF_FORCECHAR) {
2108                 AddCodeLine ("\tpha");
2109                 break;
2110             }
2111             /* FALLTHROUGH */
2112
2113         case CF_INT:
2114             AddCodeLine ("\tsta\tregsave");
2115             AddCodeLine ("\tstx\tregsave+1");
2116             break;
2117
2118         case CF_LONG:
2119             AddCodeLine ("\tjsr\tsaveeax");
2120             break;
2121
2122         default:
2123             typeerror (flags);
2124     }
2125 }
2126
2127
2128
2129 void g_restore (unsigned flags)
2130 /* Copy hold register to P. */
2131 {
2132     /* Check the size and determine operation */
2133     switch (flags & CF_TYPE) {
2134
2135         case CF_CHAR:
2136             if (flags & CF_FORCECHAR) {
2137                 AddCodeLine ("\tpla");
2138                 break;
2139             }
2140             /* FALLTHROUGH */
2141
2142         case CF_INT:
2143             AddCodeLine ("\tlda\tregsave");
2144             AddCodeLine ("\tldx\tregsave+1");
2145             break;
2146
2147         case CF_LONG:
2148             AddCodeLine ("\tjsr\tresteax");
2149             break;
2150
2151         default:
2152             typeerror (flags);
2153     }
2154 }
2155
2156
2157
2158 void g_cmp (unsigned flags, unsigned long val)
2159 /* Immidiate compare. The primary register will not be changed, Z flag
2160  * will be set.
2161  */
2162 {
2163     /* Check the size and determine operation */
2164     switch (flags & CF_TYPE) {
2165
2166         case CF_CHAR:
2167             if (flags & CF_FORCECHAR) {
2168                 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2169                 break;
2170             }
2171             /* FALLTHROUGH */
2172
2173         case CF_INT:
2174             AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
2175             AddCodeLine ("\tbne\t*+4");
2176             AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
2177             break;
2178
2179         case CF_LONG:
2180             Internal ("g_cmp: Long compares not implemented");
2181             break;
2182
2183         default:
2184             typeerror (flags);
2185     }
2186 }
2187
2188
2189
2190 static void oper (unsigned flags, unsigned long val, char** subs)
2191 /* Encode a binary operation. subs is a pointer to four groups of three
2192  * strings:
2193  *      0-2     --> Operate on ints
2194  *      3-5     --> Operate on unsigneds
2195  *      6-8     --> Operate on longs
2196  *      9-11    --> Operate on unsigned longs
2197  *
2198  * The first subroutine names in each string group is used to encode an
2199  * operation with a zero constant, the second to encode an operation with
2200  * a 8 bit constant, and the third is used in all other cases.
2201  */
2202 {
2203     unsigned offs;
2204
2205     /* Determine the offset into the array */
2206     offs = (flags & CF_UNSIGNED)? 3 : 0;
2207     switch (flags & CF_TYPE) {
2208         case CF_CHAR:
2209         case CF_INT:
2210             break;
2211
2212         case CF_LONG:
2213             offs += 6;
2214             break;
2215
2216         default:
2217             typeerror (flags);
2218     }
2219
2220     /* Encode the operation */
2221     if (flags & CF_CONST) {
2222         /* Constant value given */
2223         if (val == 0 && subs [offs+0]) {
2224             /* Special case: constant with value zero */
2225             AddCodeLine ("\tjsr\t%s", subs [offs+0]);
2226         } else if (val < 0x100 && subs [offs+1]) {
2227             /* Special case: constant with high byte zero */
2228             ldaconst (val);             /* Load low byte */
2229             AddCodeLine ("\tjsr\t%s", subs [offs+1]);
2230         } else {
2231             /* Others: arbitrary constant value */
2232             g_getimmed (flags, val, 0);                 /* Load value */
2233             AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2234         }
2235     } else {
2236         /* Value not constant (is already in (e)ax) */
2237         AddCodeLine ("\tjsr\t%s", subs [offs+2]);
2238     }
2239
2240     /* The operation will pop it's argument */
2241     pop (flags);
2242 }
2243
2244
2245
2246 void g_test (unsigned flags)
2247 /* Force a test to set cond codes right */
2248 {
2249     switch (flags & CF_TYPE) {
2250
2251         case CF_CHAR:
2252             if (flags & CF_FORCECHAR) {
2253                 AddCodeLine ("\ttax");
2254                 break;
2255             }
2256             /* FALLTHROUGH */
2257
2258         case CF_INT:
2259             AddCodeLine ("\tstx\ttmp1");
2260             AddCodeLine ("\tora\ttmp1");
2261             break;
2262
2263         case CF_LONG:
2264             if (flags & CF_UNSIGNED) {
2265                 AddCodeLine ("\tjsr\tutsteax");
2266             } else {
2267                 AddCodeLine ("\tjsr\ttsteax");
2268             }
2269             break;
2270
2271         default:
2272             typeerror (flags);
2273
2274     }
2275 }
2276
2277
2278
2279 void g_push (unsigned flags, unsigned long val)
2280 /* Push the primary register or a constant value onto the stack */
2281 {
2282     unsigned char hi;
2283
2284     if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2285
2286         /* We have a constant 8 or 16 bit value */
2287         if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2288
2289             /* Handle as 8 bit value */
2290             if (FavourSize && val <= 2) {
2291                 AddCodeLine ("\tjsr\tpushc%d", (int) val);
2292             } else {
2293                 ldaconst (val);
2294                 AddCodeLine ("\tjsr\tpusha");
2295             }
2296
2297         } else {
2298
2299             /* Handle as 16 bit value */
2300             hi = (unsigned char) (val >> 8);
2301             if (val <= 7) {
2302                 AddCodeLine ("\tjsr\tpush%u", (unsigned) val);
2303             } else if (hi == 0 || hi == 0xFF) {
2304                 /* Use special function */
2305                 ldaconst (val);
2306                 AddCodeLine ("\tjsr\t%s", (hi == 0)? "pusha0" : "pushaFF");
2307             } else {
2308                 /* Long way ... */
2309                 g_getimmed (flags, val, 0);
2310                 AddCodeLine ("\tjsr\tpushax");
2311             }
2312         }
2313
2314     } else {
2315
2316         /* Value is not 16 bit or not constant */
2317         if (flags & CF_CONST) {
2318             /* Constant 32 bit value, load into eax */
2319             g_getimmed (flags, val, 0);
2320         }
2321
2322         /* Push the primary register */
2323         switch (flags & CF_TYPE) {
2324
2325             case CF_CHAR:
2326                 if (flags & CF_FORCECHAR) {
2327                     /* Handle as char */
2328                     AddCodeLine ("\tjsr\tpusha");
2329                     break;
2330                 }
2331                 /* FALL THROUGH */
2332             case CF_INT:
2333                 AddCodeLine ("\tjsr\tpushax");
2334                 break;
2335
2336             case CF_LONG:
2337                 AddCodeLine ("\tjsr\tpusheax");
2338                 break;
2339
2340             default:
2341                 typeerror (flags);
2342
2343         }
2344
2345     }
2346
2347     /* Adjust the stack offset */
2348     push (flags);
2349 }
2350
2351
2352
2353 void g_swap (unsigned flags)
2354 /* Swap the primary register and the top of the stack. flags give the type
2355  * of *both* values (must have same size).
2356  */
2357 {
2358     switch (flags & CF_TYPE) {
2359
2360         case CF_CHAR:
2361         case CF_INT:
2362             AddCodeLine ("\tjsr\tswapstk");
2363             break;
2364
2365         case CF_LONG:
2366             AddCodeLine ("\tjsr\tswapestk");
2367             break;
2368
2369         default:
2370             typeerror (flags);
2371
2372     }
2373 }
2374
2375
2376
2377 void g_call (unsigned flags, char* lbl, unsigned argsize)
2378 /* Call the specified subroutine name */
2379 {
2380     if ((flags & CF_FIXARGC) == 0) {
2381         /* Pass arg count */
2382         ldyconst (argsize);
2383     }
2384     AddCodeLine ("\tjsr\t_%s", lbl);
2385     oursp += argsize;                   /* callee pops args */
2386 }
2387
2388
2389
2390 void g_callind (unsigned flags, unsigned argsize)
2391 /* Call subroutine with address in AX */
2392 {
2393     if ((flags & CF_FIXARGC) == 0) {
2394         /* Pass arg count */
2395         ldyconst (argsize);
2396     }
2397     AddCodeLine ("\tjsr\tcallax");      /* do the call */
2398     oursp += argsize;                   /* callee pops args */
2399 }
2400
2401
2402
2403 void g_jump (unsigned label)
2404 /* Jump to specified internal label number */
2405 {
2406     AddCodeLine ("\tjmp\tL%04X", label);
2407 }
2408
2409
2410
2411 void g_switch (unsigned flags)
2412 /* Output switch statement preample */
2413 {
2414     switch (flags & CF_TYPE) {
2415
2416         case CF_CHAR:
2417         case CF_INT:
2418             AddCodeLine ("\tjsr\tswitch");
2419             break;
2420
2421         case CF_LONG:
2422             AddCodeLine ("\tjsr\tlswitch");
2423             break;
2424
2425         default:
2426             typeerror (flags);
2427
2428     }
2429 }
2430
2431
2432
2433 void g_case (unsigned flags, unsigned label, unsigned long val)
2434 /* Create table code for one case selector */
2435 {
2436     switch (flags & CF_TYPE) {
2437
2438         case CF_CHAR:
2439         case CF_INT:
2440             AddCodeLine ("\t.word\t$%04X, L%04X",
2441                          (unsigned)(val & 0xFFFF),
2442                          (unsigned)(label & 0xFFFF));
2443             break;
2444
2445         case CF_LONG:
2446             AddCodeLine ("\t.dword\t$%08lX", val);
2447             AddCodeLine ("\t.word\tL%04X", label & 0xFFFF);
2448             break;
2449
2450         default:
2451             typeerror (flags);
2452
2453     }
2454 }
2455
2456
2457
2458 void g_truejump (unsigned flags, unsigned label)
2459 /* Jump to label if zero flag clear */
2460 {
2461     if (flags & CF_SHORT) {
2462         AddCodeLine ("\tbne\tL%04X", label);
2463     } else {
2464         AddCodeLine ("\tjne\tL%04X", label);
2465     }
2466 }
2467
2468
2469
2470 void g_falsejump (unsigned flags, unsigned label)
2471 /* Jump to label if zero flag set */
2472 {
2473     if (flags & CF_SHORT) {
2474         AddCodeLine ("\tbeq\tL%04X", label);
2475     } else {
2476         AddCodeLine ("\tjeq\tL%04X", label);
2477     }
2478 }
2479
2480
2481
2482 static void mod_internal (int k, char* verb1, char* verb2)
2483 {
2484     if (k <= 8) {
2485         AddCodeLine ("\tjsr\t%ssp%c", verb1, k + '0');
2486     } else {
2487         CheckLocalOffs (k);
2488         ldyconst (k);
2489         AddCodeLine ("\tjsr\t%ssp", verb2);
2490     }
2491 }
2492
2493
2494
2495 void g_space (int space)
2496 /* Create or drop space on the stack */
2497 {
2498     if (space < 0) {
2499         mod_internal (-space, "inc", "addy");
2500     } else if (space > 0) {
2501         mod_internal (space, "dec", "suby");
2502     }
2503 }
2504
2505
2506
2507 void g_add (unsigned flags, unsigned long val)
2508 /* Primary = TOS + Primary */
2509 {
2510     static char* ops [12] = {
2511         0,              "tosadda0",     "tosaddax",
2512         0,              "tosadda0",     "tosaddax",
2513         0,              0,              "tosaddeax",
2514         0,              0,              "tosaddeax",
2515     };
2516
2517     if (flags & CF_CONST) {
2518         flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2519         g_push (flags & ~CF_CONST, 0);
2520     }
2521     oper (flags, val, ops);
2522 }
2523
2524
2525
2526 void g_sub (unsigned flags, unsigned long val)
2527 /* Primary = TOS - Primary */
2528 {
2529     static char* ops [12] = {
2530         0,              "tossuba0",     "tossubax",
2531         0,              "tossuba0",     "tossubax",
2532         0,              0,              "tossubeax",
2533         0,              0,              "tossubeax",
2534     };
2535
2536     if (flags & CF_CONST) {
2537         flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2538         g_push (flags & ~CF_CONST, 0);
2539     }
2540     oper (flags, val, ops);
2541 }
2542
2543
2544
2545 void g_rsub (unsigned flags, unsigned long val)
2546 /* Primary = Primary - TOS */
2547 {
2548     static char* ops [12] = {
2549         0,              "tosrsuba0",    "tosrsubax",
2550         0,              "tosrsuba0",    "tosrsubax",
2551         0,              0,              "tosrsubeax",
2552         0,              0,              "tosrsubeax",
2553     };
2554     oper (flags, val, ops);
2555 }
2556
2557
2558
2559 void g_mul (unsigned flags, unsigned long val)
2560 /* Primary = TOS * Primary */
2561 {
2562     static char* ops [12] = {
2563         0,              "tosmula0",     "tosmulax",
2564         0,              "tosumula0",    "tosumulax",
2565         0,              0,              "tosmuleax",
2566         0,              0,              "tosumuleax",
2567     };
2568
2569     int p2;
2570
2571     /* Do strength reduction if the value is constant and a power of two */
2572     if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2573         /* Generate a shift instead */
2574         g_asl (flags, p2);
2575         return;
2576     }
2577
2578     /* If the right hand side is const, the lhs is not on stack but still
2579      * in the primary register.
2580      */
2581     if (flags & CF_CONST) {
2582
2583         switch (flags & CF_TYPE) {
2584
2585             case CF_CHAR:
2586                 if (flags & CF_FORCECHAR) {
2587                     /* Handle some special cases */
2588                     switch (val) {
2589
2590                         case 3:
2591                             AddCodeLine ("\tsta\ttmp1");
2592                             AddCodeLine ("\tasl\ta");
2593                             AddCodeLine ("\tclc");
2594                             AddCodeLine ("\tadc\ttmp1");
2595                             return;
2596
2597                         case 5:
2598                             AddCodeLine ("\tsta\ttmp1");
2599                             AddCodeLine ("\tasl\ta");
2600                             AddCodeLine ("\tasl\ta");
2601                             AddCodeLine ("\tclc");
2602                             AddCodeLine ("\tadc\ttmp1");
2603                             return;
2604
2605                         case 10:
2606                             AddCodeLine ("\tsta\ttmp1");
2607                             AddCodeLine ("\tasl\ta");
2608                             AddCodeLine ("\tasl\ta");
2609                             AddCodeLine ("\tclc");
2610                             AddCodeLine ("\tadc\ttmp1");
2611                             AddCodeLine ("\tasl\ta");
2612                             return;
2613                     }
2614                 }
2615                 /* FALLTHROUGH */
2616
2617             case CF_INT:
2618                 break;
2619
2620             case CF_LONG:
2621                 break;
2622
2623             default:
2624                 typeerror (flags);
2625         }
2626
2627         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2628          * into the normal, non-optimized stuff.
2629          */
2630         flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2631         g_push (flags & ~CF_CONST, 0);
2632
2633     }
2634
2635     /* Use long way over the stack */
2636     oper (flags, val, ops);
2637 }
2638
2639
2640
2641 void g_div (unsigned flags, unsigned long val)
2642 /* Primary = TOS / Primary */
2643 {
2644     static char* ops [12] = {
2645         0,              "tosdiva0",     "tosdivax",
2646         0,              "tosudiva0",    "tosudivax",
2647         0,              0,              "tosdiveax",
2648         0,              0,              "tosudiveax",
2649     };
2650
2651     /* Do strength reduction if the value is constant and a power of two */
2652     int p2;
2653     if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2654         /* Generate a shift instead */
2655         g_asr (flags, p2);
2656     } else {
2657         /* Generate a division */
2658         if (flags & CF_CONST) {
2659             /* lhs is not on stack */
2660             flags &= ~CF_FORCECHAR;     /* Handle chars as ints */
2661             g_push (flags & ~CF_CONST, 0);
2662         }
2663         oper (flags, val, ops);
2664     }
2665 }
2666
2667
2668
2669 void g_mod (unsigned flags, unsigned long val)
2670 /* Primary = TOS % Primary */
2671 {
2672     static char* ops [12] = {
2673         0,              "tosmoda0",     "tosmodax",
2674         0,              "tosumoda0",    "tosumodax",
2675         0,              0,              "tosmodeax",
2676         0,              0,              "tosumodeax",
2677     };
2678     int p2;
2679
2680     /* Check if we can do some cost reduction */
2681     if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2682         /* We can do that with an AND operation */
2683         g_and (flags, val - 1);
2684     } else {
2685         /* Do it the hard way... */
2686         if (flags & CF_CONST) {
2687             /* lhs is not on stack */
2688             flags &= ~CF_FORCECHAR;     /* Handle chars as ints */
2689             g_push (flags & ~CF_CONST, 0);
2690         }
2691         oper (flags, val, ops);
2692     }
2693 }
2694
2695
2696
2697 void g_or (unsigned flags, unsigned long val)
2698 /* Primary = TOS | Primary */
2699 {
2700     static char* ops [12] = {
2701         0,              "tosora0",      "tosorax",
2702         0,              "tosora0",      "tosorax",
2703         0,              0,              "tosoreax",
2704         0,              0,              "tosoreax",
2705     };
2706
2707     /* If the right hand side is const, the lhs is not on stack but still
2708      * in the primary register.
2709      */
2710     if (flags & CF_CONST) {
2711
2712         switch (flags & CF_TYPE) {
2713
2714             case CF_CHAR:
2715                 if (flags & CF_FORCECHAR) {
2716                     if ((val & 0xFF) != 0xFF) {
2717                         AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2718                     }
2719                     return;
2720                 }
2721                 /* FALLTHROUGH */
2722
2723             case CF_INT:
2724                 if (val <= 0xFF) {
2725                     AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2726                     return;
2727                 }
2728                 break;
2729
2730             case CF_LONG:
2731                 if (val <= 0xFF) {
2732                     AddCodeLine ("\tora\t#$%02X", (unsigned char)val);
2733                     return;
2734                 }
2735                 break;
2736
2737             default:
2738                 typeerror (flags);
2739         }
2740
2741         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2742          * into the normal, non-optimized stuff.
2743          */
2744         g_push (flags & ~CF_CONST, 0);
2745
2746     }
2747
2748     /* Use long way over the stack */
2749     oper (flags, val, ops);
2750 }
2751
2752
2753
2754 void g_xor (unsigned flags, unsigned long val)
2755 /* Primary = TOS ^ Primary */
2756 {
2757     static char* ops [12] = {
2758         0,              "tosxora0",     "tosxorax",
2759         0,              "tosxora0",     "tosxorax",
2760         0,              0,              "tosxoreax",
2761         0,              0,              "tosxoreax",
2762     };
2763
2764
2765     /* If the right hand side is const, the lhs is not on stack but still
2766      * in the primary register.
2767      */
2768     if (flags & CF_CONST) {
2769
2770         switch (flags & CF_TYPE) {
2771
2772             case CF_CHAR:
2773                 if (flags & CF_FORCECHAR) {
2774                     if ((val & 0xFF) != 0) {
2775                         AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2776                     }
2777                     return;
2778                 }
2779                 /* FALLTHROUGH */
2780
2781             case CF_INT:
2782                 if (val <= 0xFF) {
2783                     if (val != 0) {
2784                         AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2785                     }
2786                     return;
2787                 } else if ((val & 0xFF) == 0) {
2788                     AddCodeLine ("\tpha");
2789                     AddCodeLine ("\ttxa");
2790                     AddCodeLine ("\teor\t#$%02X", (unsigned char)(val >> 8));
2791                     AddCodeLine ("\ttax");
2792                     AddCodeLine ("\tpla");
2793                     return;
2794                 }
2795                 break;
2796
2797             case CF_LONG:
2798                 if (val <= 0xFF) {
2799                     if (val != 0) {
2800                         AddCodeLine ("\teor\t#$%02X", (unsigned char)val);
2801                     }
2802                     return;
2803                 }
2804                 break;
2805
2806             default:
2807                 typeerror (flags);
2808         }
2809
2810         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2811          * into the normal, non-optimized stuff.
2812          */
2813         g_push (flags & ~CF_CONST, 0);
2814
2815     }
2816
2817     /* Use long way over the stack */
2818     oper (flags, val, ops);
2819 }
2820
2821
2822
2823 void g_and (unsigned flags, unsigned long val)
2824 /* Primary = TOS & Primary */
2825 {
2826     static char* ops [12] = {
2827         0,              "tosanda0",     "tosandax",
2828         0,              "tosanda0",     "tosandax",
2829         0,              0,              "tosandeax",
2830         0,              0,              "tosandeax",
2831     };
2832
2833     /* If the right hand side is const, the lhs is not on stack but still
2834      * in the primary register.
2835      */
2836     if (flags & CF_CONST) {
2837
2838         switch (flags & CF_TYPE) {
2839
2840             case CF_CHAR:
2841                 if (flags & CF_FORCECHAR) {
2842                     AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2843                     return;
2844                 }
2845                 /* FALLTHROUGH */
2846             case CF_INT:
2847                 if ((val & 0xFFFF) != 0xFFFF) {
2848                     if (val <= 0xFF) {
2849                         ldxconst (0);
2850                         if (val == 0) {
2851                             ldaconst (0);
2852                         } else if (val != 0xFF) {
2853                             AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2854                         }
2855                     } else if ((val & 0xFF00) == 0xFF00) {
2856                         AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2857                     } else if ((val & 0x00FF) == 0x0000) {
2858                         AddCodeLine ("\ttxa");
2859                         AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2860                         AddCodeLine ("\ttax");
2861                         ldaconst (0);
2862                     } else {
2863                         AddCodeLine ("\ttay");
2864                         AddCodeLine ("\ttxa");
2865                         AddCodeLine ("\tand\t#$%02X", (unsigned char)(val >> 8));
2866                         AddCodeLine ("\ttax");
2867                         AddCodeLine ("\ttya");
2868                         if ((val & 0x00FF) != 0x00FF) {
2869                             AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2870                         }
2871                     }
2872                 }
2873                 return;
2874
2875             case CF_LONG:
2876                 if (val <= 0xFF) {
2877                     ldxconst (0);
2878                     AddCodeLine ("\tstx\tsreg+1");
2879                     AddCodeLine ("\tstx\tsreg");
2880                     if ((val & 0xFF) != 0xFF) {
2881                          AddCodeLine ("\tand\t#$%02X", (unsigned char)val);
2882                     }
2883                     return;
2884                 } else if (val == 0xFF00) {
2885                     ldaconst (0);
2886                     AddCodeLine ("\tsta\tsreg+1");
2887                     AddCodeLine ("\tsta\tsreg");
2888                     return;
2889                 }
2890                 break;
2891
2892             default:
2893                 typeerror (flags);
2894         }
2895
2896         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2897          * into the normal, non-optimized stuff.
2898          */
2899         g_push (flags & ~CF_CONST, 0);
2900
2901     }
2902
2903     /* Use long way over the stack */
2904     oper (flags, val, ops);
2905 }
2906
2907
2908
2909 void g_asr (unsigned flags, unsigned long val)
2910 /* Primary = TOS >> Primary */
2911 {
2912     static char* ops [12] = {
2913         0,              "tosasra0",     "tosasrax",
2914         0,              "tosshra0",     "tosshrax",
2915         0,              0,              "tosasreax",
2916         0,              0,              "tosshreax",
2917     };
2918
2919     /* If the right hand side is const, the lhs is not on stack but still
2920      * in the primary register.
2921      */
2922     if (flags & CF_CONST) {
2923
2924         switch (flags & CF_TYPE) {
2925
2926             case CF_CHAR:
2927             case CF_INT:
2928                 if (val >= 1 && val <= 3) {
2929                     if (flags & CF_UNSIGNED) {
2930                         AddCodeLine ("\tjsr\tshrax%ld", val);
2931                     } else {
2932                         AddCodeLine ("\tjsr\tasrax%ld", val);
2933                     }
2934                     return;
2935                 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2936                     AddCodeLine ("\ttxa");
2937                     ldxconst (0);
2938                     return;
2939                 }
2940                 break;
2941
2942             case CF_LONG:
2943                 if (val >= 1 && val <= 3) {
2944                     if (flags & CF_UNSIGNED) {
2945                         AddCodeLine ("\tjsr\tshreax%ld", val);
2946                     } else {
2947                         AddCodeLine ("\tjsr\tasreax%ld", val);
2948                     }
2949                     return;
2950                 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2951                     AddCodeLine ("\ttxa");
2952                     AddCodeLine ("\tldx\tsreg");
2953                     AddCodeLine ("\tldy\tsreg+1");
2954                     AddCodeLine ("\tsty\tsreg");
2955                     AddCodeLine ("\tldy\t#$00");
2956                     AddCodeLine ("\tsty\tsreg+1");
2957                     return;
2958                 } else if (val == 16) {
2959                     AddCodeLine ("\tldy\t#$00");
2960                     AddCodeLine ("\tldx\tsreg+1");
2961                     if ((flags & CF_UNSIGNED) == 0) {
2962                         AddCodeLine ("\tbpl\t*+3");
2963                         AddCodeLine ("\tdey");
2964                         AddCodeHint ("y:!");
2965                     }
2966                     AddCodeLine ("\tlda\tsreg");
2967                     AddCodeLine ("\tsty\tsreg+1");
2968                     AddCodeLine ("\tsty\tsreg");
2969                     return;
2970                 }
2971                 break;
2972
2973             default:
2974                 typeerror (flags);
2975         }
2976
2977         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2978          * into the normal, non-optimized stuff.
2979          */
2980         g_push (flags & ~CF_CONST, 0);
2981
2982     }
2983
2984     /* Use long way over the stack */
2985     oper (flags, val, ops);
2986 }
2987
2988
2989
2990 void g_asl (unsigned flags, unsigned long val)
2991 /* Primary = TOS << Primary */
2992 {
2993     static char* ops [12] = {
2994         0,              "tosasla0",     "tosaslax",
2995         0,              "tosshla0",     "tosshlax",
2996         0,              0,              "tosasleax",
2997         0,              0,              "tosshleax",
2998     };
2999
3000
3001     /* If the right hand side is const, the lhs is not on stack but still
3002      * in the primary register.
3003      */
3004     if (flags & CF_CONST) {
3005
3006         switch (flags & CF_TYPE) {
3007
3008             case CF_CHAR:
3009             case CF_INT:
3010                 if (val >= 1 && val <= 3) {
3011                     if (flags & CF_UNSIGNED) {
3012                         AddCodeLine ("\tjsr\tshlax%ld", val);
3013                     } else {
3014                         AddCodeLine ("\tjsr\taslax%ld", val);
3015                     }
3016                     return;
3017                 } else if (val == 8) {
3018                     AddCodeLine ("\ttax");
3019                     AddCodeLine ("\tlda\t#$00");
3020                     return;
3021                 }
3022                 break;
3023
3024             case CF_LONG:
3025                 if (val >= 1 && val <= 3) {
3026                     if (flags & CF_UNSIGNED) {
3027                         AddCodeLine ("\tjsr\tshleax%ld", val);
3028                     } else {
3029                         AddCodeLine ("\tjsr\tasleax%ld", val);
3030                     }
3031                     return;
3032                 } else if (val == 8) {
3033                     AddCodeLine ("\tldy\tsreg");
3034                     AddCodeLine ("\tsty\tsreg+1");
3035                     AddCodeLine ("\tstx\tsreg");
3036                     AddCodeLine ("\ttax");
3037                     AddCodeLine ("\tlda\t#$00");
3038                     return;
3039                 } else if (val == 16) {
3040                     AddCodeLine ("\tstx\tsreg+1");
3041                     AddCodeLine ("\tsta\tsreg");
3042                     AddCodeLine ("\tlda\t#$00");
3043                     AddCodeLine ("\ttax");
3044                     return;
3045                 }
3046                 break;
3047
3048             default:
3049                 typeerror (flags);
3050         }
3051
3052         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3053          * into the normal, non-optimized stuff.
3054          */
3055         g_push (flags & ~CF_CONST, 0);
3056
3057     }
3058
3059     /* Use long way over the stack */
3060     oper (flags, val, ops);
3061 }
3062
3063
3064
3065 void g_neg (unsigned flags)
3066 /* Primary = -Primary */
3067 {
3068     switch (flags & CF_TYPE) {
3069
3070         case CF_CHAR:
3071         case CF_INT:
3072             AddCodeLine ("\tjsr\tnegax");
3073             break;
3074
3075         case CF_LONG:
3076             AddCodeLine ("\tjsr\tnegeax");
3077             break;
3078
3079         default:
3080             typeerror (flags);
3081     }
3082 }
3083
3084
3085
3086 void g_bneg (unsigned flags)
3087 /* Primary = !Primary */
3088 {
3089     switch (flags & CF_TYPE) {
3090
3091         case CF_CHAR:
3092             AddCodeLine ("\tjsr\tbnega");
3093             break;
3094
3095         case CF_INT:
3096             AddCodeLine ("\tjsr\tbnegax");
3097             break;
3098
3099         case CF_LONG:
3100             AddCodeLine ("\tjsr\tbnegeax");
3101             break;
3102
3103         default:
3104             typeerror (flags);
3105     }
3106 }
3107
3108
3109
3110 void g_com (unsigned flags)
3111 /* Primary = ~Primary */
3112 {
3113     switch (flags & CF_TYPE) {
3114
3115         case CF_CHAR:
3116         case CF_INT:
3117             AddCodeLine ("\tjsr\tcomplax");
3118             break;
3119
3120         case CF_LONG:
3121             AddCodeLine ("\tjsr\tcompleax");
3122             break;
3123
3124         default:
3125             typeerror (flags);
3126     }
3127 }
3128
3129
3130
3131 void g_inc (unsigned flags, unsigned long val)
3132 /* Increment the primary register by a given number */
3133 {
3134     /* Don't inc by zero */
3135     if (val == 0) {
3136         return;
3137     }
3138
3139     /* Generate code for the supported types */
3140     flags &= ~CF_CONST;
3141     switch (flags & CF_TYPE) {
3142
3143         case CF_CHAR:
3144             if (flags & CF_FORCECHAR) {
3145                 if (CPU == CPU_65C02 && val <= 2) {
3146                     while (val--) {
3147                         AddCodeLine ("\tina");
3148                     }
3149                 } else {
3150                     AddCodeLine ("\tclc");
3151                     AddCodeLine ("\tadc\t#$%02X", (unsigned char)val);
3152                 }
3153                 break;
3154             }
3155             /* FALLTHROUGH */
3156
3157         case CF_INT:
3158             if (CPU == CPU_65C02 && val == 1) {
3159                 AddCodeLine ("\tina");
3160                 AddCodeLine ("\tbne\t*+3");
3161                 AddCodeLine ("\tinx");
3162                 /* Tell the optimizer that the X register may be invalid */
3163                 AddCodeHint ("x:!");
3164             } else if (FavourSize) {
3165                 /* Use jsr calls */
3166                 if (val <= 8) {
3167                     AddCodeLine ("\tjsr\tincax%lu", val);
3168                 } else if (val <= 255) {
3169                     ldyconst (val);
3170                     AddCodeLine ("\tjsr\tincaxy");
3171                 } else {
3172                     g_add (flags | CF_CONST, val);
3173                 }
3174             } else {
3175                 /* Inline the code */
3176                 if (val < 0x300) {
3177                     if ((val & 0xFF) != 0) {
3178                         AddCodeLine ("\tclc");
3179                         AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3180                         AddCodeLine ("\tbcc\t*+3");
3181                         AddCodeLine ("\tinx");
3182                         /* Tell the optimizer that the X register may be invalid */
3183                         AddCodeHint ("x:!");
3184                     }
3185                     if (val >= 0x100) {
3186                         AddCodeLine ("\tinx");
3187                     }
3188                     if (val >= 0x200) {
3189                         AddCodeLine ("\tinx");
3190                     }
3191                 } else {
3192                     AddCodeLine ("\tclc");
3193                     if ((val & 0xFF) != 0) {
3194                         AddCodeLine ("\tadc\t#$%02X", (unsigned char) val);
3195                         /* Tell the optimizer that the X register may be invalid */
3196                         AddCodeHint ("x:!");
3197                     }
3198                     AddCodeLine ("\tpha");
3199                     AddCodeLine ("\ttxa");
3200                     AddCodeLine ("\tadc\t#$%02X", (unsigned char) (val >> 8));
3201                     AddCodeLine ("\ttax");
3202                     AddCodeLine ("\tpla");
3203                 }
3204             }
3205             break;
3206
3207         case CF_LONG:
3208             if (val <= 255) {
3209                 ldyconst (val);
3210                 AddCodeLine ("\tjsr\tinceaxy");
3211             } else {
3212                 g_add (flags | CF_CONST, val);
3213             }
3214             break;
3215
3216         default:
3217             typeerror (flags);
3218
3219     }
3220 }
3221
3222
3223
3224 void g_dec (unsigned flags, unsigned long val)
3225 /* Decrement the primary register by a given number */
3226 {
3227     /* Don't dec by zero */
3228     if (val == 0) {
3229         return;
3230     }
3231
3232     /* Generate code for the supported types */
3233     flags &= ~CF_CONST;
3234     switch (flags & CF_TYPE) {
3235
3236         case CF_CHAR:
3237             if (flags & CF_FORCECHAR) {
3238                 if (CPU == CPU_65C02 && val <= 2) {
3239                     while (val--) {
3240                         AddCodeLine ("\tdea");
3241                     }
3242                 } else {
3243                     AddCodeLine ("\tsec");
3244                     AddCodeLine ("\tsbc\t#$%02X", (unsigned char)val);
3245                 }
3246                 break;
3247             }
3248             /* FALLTHROUGH */
3249
3250         case CF_INT:
3251             if (val <= 2) {
3252                 AddCodeLine ("\tjsr\tdecax%d", (int) val);
3253             } else if (val <= 255) {
3254                 ldyconst (val);
3255                 AddCodeLine ("\tjsr\tdecaxy");
3256             } else {
3257                 g_sub (flags | CF_CONST, val);
3258             }
3259             break;
3260
3261         case CF_LONG:
3262             if (val <= 255) {
3263                 ldyconst (val);
3264                 AddCodeLine ("\tjsr\tdeceaxy");
3265             } else {
3266                 g_sub (flags | CF_CONST, val);
3267             }
3268             break;
3269
3270         default:
3271             typeerror (flags);
3272
3273     }
3274 }
3275
3276
3277
3278 /*
3279  * Following are the conditional operators. They compare the TOS against
3280  * the primary and put a literal 1 in the primary if the condition is
3281  * true, otherwise they clear the primary register
3282  */
3283
3284
3285
3286 void g_eq (unsigned flags, unsigned long val)
3287 /* Test for equal */
3288 {
3289     static char* ops [12] = {
3290         "toseq00",      "toseqa0",      "toseqax",
3291         "toseq00",      "toseqa0",      "toseqax",
3292         0,              0,              "toseqeax",
3293         0,              0,              "toseqeax",
3294     };
3295
3296     /* If the right hand side is const, the lhs is not on stack but still
3297      * in the primary register.
3298      */
3299     if (flags & CF_CONST) {
3300
3301         switch (flags & CF_TYPE) {
3302
3303             case CF_CHAR:
3304                 if (flags & CF_FORCECHAR) {
3305                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3306                     AddCodeLine ("\tjsr\tbooleq");
3307                     return;
3308                 }
3309                 /* FALLTHROUGH */
3310
3311             case CF_INT:
3312                 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3313                 AddCodeLine ("\tbne\t*+4");
3314                 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3315                 AddCodeLine ("\tjsr\tbooleq");
3316                 return;
3317
3318             case CF_LONG:
3319                 break;
3320
3321             default:
3322                 typeerror (flags);
3323         }
3324
3325         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3326          * into the normal, non-optimized stuff.
3327          */
3328         g_push (flags & ~CF_CONST, 0);
3329
3330     }
3331
3332     /* Use long way over the stack */
3333     oper (flags, val, ops);
3334 }
3335
3336
3337
3338 void g_ne (unsigned flags, unsigned long val)
3339 /* Test for not equal */
3340 {
3341     static char* ops [12] = {
3342         "tosne00",      "tosnea0",      "tosneax",
3343         "tosne00",      "tosnea0",      "tosneax",
3344         0,              0,              "tosneeax",
3345         0,              0,              "tosneeax",
3346     };
3347
3348
3349     /* If the right hand side is const, the lhs is not on stack but still
3350      * in the primary register.
3351      */
3352     if (flags & CF_CONST) {
3353
3354         switch (flags & CF_TYPE) {
3355
3356             case CF_CHAR:
3357                 if (flags & CF_FORCECHAR) {
3358                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3359                     AddCodeLine ("\tjsr\tboolne");
3360                     return;
3361                 }
3362                 /* FALLTHROUGH */
3363
3364             case CF_INT:
3365                 AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3366                 AddCodeLine ("\tbne\t*+4");
3367                 AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3368                 AddCodeLine ("\tjsr\tboolne");
3369                 return;
3370
3371             case CF_LONG:
3372                 break;
3373
3374             default:
3375                 typeerror (flags);
3376         }
3377
3378         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3379          * into the normal, non-optimized stuff.
3380          */
3381         g_push (flags & ~CF_CONST, 0);
3382
3383     }
3384
3385     /* Use long way over the stack */
3386     oper (flags, val, ops);
3387 }
3388
3389
3390
3391 void g_lt (unsigned flags, unsigned long val)
3392 /* Test for less than */
3393 {
3394     static char* ops [12] = {
3395         "toslt00",      "toslta0",      "tosltax",
3396         "tosult00",     "tosulta0",     "tosultax",
3397         0,              0,              "toslteax",
3398         0,              0,              "tosulteax",
3399     };
3400
3401     /* If the right hand side is const, the lhs is not on stack but still
3402      * in the primary register.
3403      */
3404     if (flags & CF_CONST) {
3405
3406         /* Give a warning in some special cases */
3407         if ((flags & CF_UNSIGNED) && val == 0) {
3408             Warning ("Condition is never true");
3409         }
3410
3411         /* Look at the type */
3412         switch (flags & CF_TYPE) {
3413
3414             case CF_CHAR:
3415                 if (flags & CF_FORCECHAR) {
3416                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3417                     if (flags & CF_UNSIGNED) {
3418                         AddCodeLine ("\tjsr\tboolult");
3419                     } else {
3420                         AddCodeLine ("\tjsr\tboollt");
3421                     }
3422                     return;
3423                 }
3424                 /* FALLTHROUGH */
3425
3426             case CF_INT:
3427                 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3428                     /* If we have a signed compare against zero, we only need to
3429                      * test the high byte.
3430                      */
3431                     AddCodeLine ("\ttxa");
3432                     AddCodeLine ("\tjsr\tboollt");
3433                     return;
3434                 }
3435                 /* Direct code only for unsigned data types */
3436                 if (flags & CF_UNSIGNED) {
3437                     AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3438                     AddCodeLine ("\tbne\t*+4");
3439                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3440                     AddCodeLine ("\tjsr\tboolult");
3441                     return;
3442                 }
3443                 break;
3444
3445             case CF_LONG:
3446                 break;
3447
3448             default:
3449                 typeerror (flags);
3450         }
3451
3452         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3453          * into the normal, non-optimized stuff.
3454          */
3455         g_push (flags & ~CF_CONST, 0);
3456
3457     }
3458
3459     /* Use long way over the stack */
3460     oper (flags, val, ops);
3461 }
3462
3463
3464
3465 void g_le (unsigned flags, unsigned long val)
3466 /* Test for less than or equal to */
3467 {
3468     static char* ops [12] = {
3469         "tosle00",      "toslea0",      "tosleax",
3470         "tosule00",     "tosulea0",     "tosuleax",
3471         0,              0,              "tosleeax",
3472         0,              0,              "tosuleeax",
3473     };
3474
3475
3476     /* If the right hand side is const, the lhs is not on stack but still
3477      * in the primary register.
3478      */
3479     if (flags & CF_CONST) {
3480
3481         /* Look at the type */
3482         switch (flags & CF_TYPE) {
3483
3484             case CF_CHAR:
3485                 if (flags & CF_FORCECHAR) {
3486                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3487                     if (flags & CF_UNSIGNED) {
3488                         AddCodeLine ("\tjsr\tboolule");
3489                     } else {
3490                         AddCodeLine ("\tjsr\tboolle");
3491                     }
3492                     return;
3493                 }
3494                 /* FALLTHROUGH */
3495
3496             case CF_INT:
3497                 if (flags & CF_UNSIGNED) {
3498                     AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3499                     AddCodeLine ("\tbne\t*+4");
3500                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3501                     AddCodeLine ("\tjsr\tboolule");
3502                     return;
3503                 }
3504                 break;
3505
3506             case CF_LONG:
3507                 break;
3508
3509             default:
3510                 typeerror (flags);
3511         }
3512
3513         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3514          * into the normal, non-optimized stuff.
3515          */
3516         g_push (flags & ~CF_CONST, 0);
3517
3518     }
3519
3520     /* Use long way over the stack */
3521     oper (flags, val, ops);
3522 }
3523
3524
3525
3526 void g_gt (unsigned flags, unsigned long val)
3527 /* Test for greater than */
3528 {
3529     static char* ops [12] = {
3530         "tosgt00",      "tosgta0",      "tosgtax",
3531         "tosugt00",     "tosugta0",     "tosugtax",
3532         0,              0,              "tosgteax",
3533         0,              0,              "tosugteax",
3534     };
3535
3536
3537     /* If the right hand side is const, the lhs is not on stack but still
3538      * in the primary register.
3539      */
3540     if (flags & CF_CONST) {
3541
3542         /* Look at the type */
3543         switch (flags & CF_TYPE) {
3544
3545             case CF_CHAR:
3546                 if (flags & CF_FORCECHAR) {
3547                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3548                     if (flags & CF_UNSIGNED) {
3549                         /* If we have a compare > 0, we will replace it by
3550                          * != 0 here, since both are identical but the latter
3551                          * is easier to optimize.
3552                          */
3553                         if (val & 0xFF) {
3554                             AddCodeLine ("\tjsr\tboolugt");
3555                         } else {
3556                             AddCodeLine ("\tjsr\tboolne");
3557                         }
3558                     } else {
3559                         AddCodeLine ("\tjsr\tboolgt");
3560                     }
3561                     return;
3562                 }
3563                 /* FALLTHROUGH */
3564
3565             case CF_INT:
3566                 if (flags & CF_UNSIGNED) {
3567                     /* If we have a compare > 0, we will replace it by
3568                      * != 0 here, since both are identical but the latter
3569                      * is easier to optimize.
3570                      */
3571                     if ((val & 0xFFFF) == 0) {
3572                         AddCodeLine ("\tstx\ttmp1");
3573                         AddCodeLine ("\tora\ttmp1");
3574                         AddCodeLine ("\tjsr\tboolne");
3575                     } else {
3576                         AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3577                         AddCodeLine ("\tbne\t*+4");
3578                         AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3579                         AddCodeLine ("\tjsr\tboolugt");
3580                     }
3581                     return;
3582                 }
3583                 break;
3584
3585             case CF_LONG:
3586                 break;
3587
3588             default:
3589                 typeerror (flags);
3590         }
3591
3592         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3593          * into the normal, non-optimized stuff.
3594          */
3595         g_push (flags & ~CF_CONST, 0);
3596
3597     }
3598
3599     /* Use long way over the stack */
3600     oper (flags, val, ops);
3601 }
3602
3603
3604
3605 void g_ge (unsigned flags, unsigned long val)
3606 /* Test for greater than or equal to */
3607 {
3608     static char* ops [12] = {
3609         "tosge00",      "tosgea0",      "tosgeax",
3610         "tosuge00",     "tosugea0",     "tosugeax",
3611         0,              0,              "tosgeeax",
3612         0,              0,              "tosugeeax",
3613     };
3614
3615
3616     /* If the right hand side is const, the lhs is not on stack but still
3617      * in the primary register.
3618      */
3619     if (flags & CF_CONST) {
3620
3621         /* Give a warning in some special cases */
3622         if ((flags & CF_UNSIGNED) && val == 0) {
3623             Warning ("Condition is always true");
3624         }
3625
3626         /* Look at the type */
3627         switch (flags & CF_TYPE) {
3628
3629             case CF_CHAR:
3630                 if (flags & CF_FORCECHAR) {
3631                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3632                     if (flags & CF_UNSIGNED) {
3633                         AddCodeLine ("\tjsr\tbooluge");
3634                     } else {
3635                         AddCodeLine ("\tjsr\tboolge");
3636                     }
3637                     return;
3638                 }
3639                 /* FALLTHROUGH */
3640
3641             case CF_INT:
3642                 if (flags & CF_UNSIGNED) {
3643                     AddCodeLine ("\tcpx\t#$%02X", (unsigned char)(val >> 8));
3644                     AddCodeLine ("\tbne\t*+4");
3645                     AddCodeLine ("\tcmp\t#$%02X", (unsigned char)val);
3646                     AddCodeLine ("\tjsr\tbooluge");
3647                     return;
3648                 }
3649                 break;
3650
3651             case CF_LONG:
3652                 break;
3653
3654             default:
3655                 typeerror (flags);
3656         }
3657
3658         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3659          * into the normal, non-optimized stuff.
3660          */
3661         g_push (flags & ~CF_CONST, 0);
3662
3663     }
3664
3665     /* Use long way over the stack */
3666     oper (flags, val, ops);
3667 }
3668
3669
3670
3671 /*****************************************************************************/
3672 /*                         Allocating static storage                         */
3673 /*****************************************************************************/
3674
3675
3676
3677 void g_res (unsigned n)
3678 /* Reserve static storage, n bytes */
3679 {
3680     AddCodeLine ("\t.res\t%u,$00", n);
3681 }
3682
3683
3684
3685 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3686 /* Define data with the size given in flags */
3687 {
3688     if (flags & CF_CONST) {
3689
3690         /* Numeric constant */
3691         switch (flags & CF_TYPE) {
3692
3693             case CF_CHAR:
3694                 AddCodeLine ("\t.byte\t$%02lX", val & 0xFF);
3695                 break;
3696
3697             case CF_INT:
3698                 AddCodeLine ("\t.word\t$%04lX", val & 0xFFFF);
3699                 break;
3700
3701             case CF_LONG:
3702                 AddCodeLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3703                 break;
3704
3705             default:
3706                 typeerror (flags);
3707                 break;
3708
3709         }
3710
3711     } else {
3712
3713         /* Create the correct label name */
3714         const char* Label = GetLabelName (flags, val, offs);
3715
3716         /* Labels are always 16 bit */
3717         AddCodeLine ("\t.word\t%s", Label);
3718
3719     }
3720 }
3721
3722
3723
3724 void g_defbytes (const unsigned char* Bytes, unsigned Count)
3725 /* Output a row of bytes as a constant */
3726 {
3727     unsigned Chunk;
3728     char Buf [128];
3729     char* B;
3730
3731     /* Output the stuff */
3732     while (Count) {
3733
3734         /* How many go into this line? */
3735         if ((Chunk = Count) > 16) {
3736             Chunk = 16;
3737         }
3738         Count -= Chunk;
3739
3740         /* Output one line */
3741         strcpy (Buf, "\t.byte\t");
3742         B = Buf + 7;
3743         do {
3744             B += sprintf (B, "$%02X", *Bytes++ & 0xFF);
3745             if (--Chunk) {
3746                 *B++ = ',';
3747             }
3748         } while (Chunk);
3749
3750         /* Output the line */
3751         AddCodeLine (Buf);
3752     }
3753 }
3754
3755
3756
3757 void g_zerobytes (unsigned n)
3758 /* Output n bytes of data initialized with zero */
3759 {
3760     AddCodeLine ("\t.res\t%u,$00", n);
3761 }
3762
3763
3764
3765 /*****************************************************************************/
3766 /*                          Inlined known functions                          */
3767 /*****************************************************************************/
3768
3769
3770
3771 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3772 /* Inline the strlen() function */
3773 {
3774     /* We need a label in both cases */
3775     unsigned label = GetLabel ();
3776
3777     /* Two different encodings */
3778     if (flags & CF_CONST) {
3779
3780         /* The address of the string is constant. Create the correct label name */
3781         char* lbuf = GetLabelName (flags, val, offs);
3782
3783         /* Generate the strlen code */
3784         AddCodeLine ("\tldy\t#$FF");
3785         g_defloclabel (label);
3786         AddCodeLine ("\tiny");
3787         AddCodeLine ("\tlda\t%s,y", lbuf);
3788         AddCodeLine ("\tbne\tL%04X", label);
3789         AddCodeLine ("\ttax");
3790         AddCodeLine ("\ttya");
3791
3792     } else {
3793
3794         /* Address not constant but in primary */
3795         if (FavourSize) {
3796             /* This is too much code, so call strlen instead of inlining */
3797             AddCodeLine ("\tjsr\t_strlen");
3798         } else {
3799             /* Inline the function */
3800             AddCodeLine ("\tsta\tptr1");
3801             AddCodeLine ("\tstx\tptr1+1");
3802             AddCodeLine ("\tldy\t#$FF");
3803             g_defloclabel (label);
3804             AddCodeLine ("\tiny");
3805             AddCodeLine ("\tlda\t(ptr1),y");
3806             AddCodeLine ("\tbne\tL%04X", label);
3807             AddCodeLine ("\ttax");
3808             AddCodeLine ("\ttya");
3809         }
3810     }
3811 }
3812
3813
3814