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