]> git.sur5r.net Git - cc65/blob - src/cc65/codegen.c
Simplify code generation
[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             ldyconst (Offs);
1115             AddCodeLine ("jsr staxspidx");
1116             break;
1117
1118         case CF_LONG:
1119             ldyconst (Offs);
1120             AddCodeLine ("jsr steaxspidx");
1121             break;
1122
1123         default:
1124             typeerror (Flags);
1125
1126     }
1127
1128     /* Pop the argument which is always a pointer */
1129     pop (CF_PTR);
1130 }
1131
1132
1133
1134 /*****************************************************************************/
1135 /*                    type conversion and similiar stuff                     */
1136 /*****************************************************************************/
1137
1138
1139
1140 void g_toslong (unsigned flags)
1141 /* Make sure, the value on TOS is a long. Convert if necessary */
1142 {
1143     switch (flags & CF_TYPE) {
1144
1145         case CF_CHAR:
1146         case CF_INT:
1147             if (flags & CF_UNSIGNED) {
1148                 AddCodeLine ("jsr tosulong");
1149             } else {
1150                 AddCodeLine ("jsr toslong");
1151             }
1152             push (CF_INT);
1153             break;
1154
1155         case CF_LONG:
1156             break;
1157
1158         default:
1159             typeerror (flags);
1160     }
1161 }
1162
1163
1164
1165 void g_tosint (unsigned flags)
1166 /* Make sure, the value on TOS is an int. Convert if necessary */
1167 {
1168     switch (flags & CF_TYPE) {
1169
1170         case CF_CHAR:
1171         case CF_INT:
1172             break;
1173
1174         case CF_LONG:
1175             AddCodeLine ("jsr tosint");
1176             pop (CF_INT);
1177             break;
1178
1179         default:
1180             typeerror (flags);
1181     }
1182 }
1183
1184
1185
1186 void g_reglong (unsigned flags)
1187 /* Make sure, the value in the primary register a long. Convert if necessary */
1188 {
1189     switch (flags & CF_TYPE) {
1190
1191         case CF_CHAR:
1192         case CF_INT:
1193             if (flags & CF_UNSIGNED) {
1194                 if (CodeSizeFactor >= 200) {
1195                     ldyconst (0);
1196                     AddCodeLine ("sty sreg");
1197                     AddCodeLine ("sty sreg+1");
1198                 } else {
1199                     AddCodeLine ("jsr axulong");
1200                 }
1201             } else {
1202                 AddCodeLine ("jsr axlong");
1203             }
1204             break;
1205
1206         case CF_LONG:
1207             break;
1208
1209         default:
1210             typeerror (flags);
1211     }
1212 }
1213
1214
1215
1216 unsigned g_typeadjust (unsigned lhs, unsigned rhs)
1217 /* Adjust the integer operands before doing a binary operation. lhs is a flags
1218  * value, that corresponds to the value on TOS, rhs corresponds to the value
1219  * in (e)ax. The return value is the the flags value for the resulting type.
1220  */
1221 {
1222     unsigned ltype, rtype;
1223     unsigned result;
1224
1225     /* Get the type spec from the flags */
1226     ltype = lhs & CF_TYPE;
1227     rtype = rhs & CF_TYPE;
1228
1229     /* Check if a conversion is needed */
1230     if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1231         /* We must promote the primary register to long */
1232         g_reglong (rhs);
1233         /* Get the new rhs type */
1234         rhs = (rhs & ~CF_TYPE) | CF_LONG;
1235         rtype = CF_LONG;
1236     } else if (ltype != CF_LONG && (lhs & CF_CONST) == 0 && rtype == CF_LONG) {
1237         /* We must promote the lhs to long */
1238         if (lhs & CF_REG) {
1239             g_reglong (lhs);
1240         } else {
1241             g_toslong (lhs);
1242         }
1243         /* Get the new rhs type */
1244         lhs = (lhs & ~CF_TYPE) | CF_LONG;
1245         ltype = CF_LONG;
1246     }
1247
1248     /* Determine the result type for the operation:
1249      *  - The result is const if both operands are const.
1250      *  - The result is unsigned if one of the operands is unsigned.
1251      *  - The result is long if one of the operands is long.
1252      *  - Otherwise the result is int sized.
1253      */
1254     result = (lhs & CF_CONST) & (rhs & CF_CONST);
1255     result |= (lhs & CF_UNSIGNED) | (rhs & CF_UNSIGNED);
1256     if (rtype == CF_LONG || ltype == CF_LONG) {
1257         result |= CF_LONG;
1258     } else {
1259         result |= CF_INT;
1260     }
1261     return result;
1262 }
1263
1264
1265
1266 unsigned g_typecast (unsigned lhs, unsigned rhs)
1267 /* Cast the value in the primary register to the operand size that is flagged
1268  * by the lhs value. Return the result value.
1269  */
1270 {
1271     unsigned ltype, rtype;
1272
1273     /* Get the type spec from the flags */
1274     ltype = lhs & CF_TYPE;
1275     rtype = rhs & CF_TYPE;
1276
1277     /* Check if a conversion is needed */
1278     if (ltype == CF_LONG && rtype != CF_LONG && (rhs & CF_CONST) == 0) {
1279         /* We must promote the primary register to long */
1280         g_reglong (rhs);
1281     }
1282
1283     /* Do not need any other action. If the left type is int, and the primary
1284      * register is long, it will be automagically truncated. If the right hand
1285      * side is const, it is not located in the primary register and handled by
1286      * the expression parser code.
1287      */
1288
1289     /* Result is const if the right hand side was const */
1290     lhs |= (rhs & CF_CONST);
1291
1292     /* The resulting type is that of the left hand side (that's why you called
1293      * this function :-)
1294      */
1295     return lhs;
1296 }
1297
1298
1299
1300 void g_scale (unsigned flags, long val)
1301 /* Scale the value in the primary register by the given value. If val is positive,
1302  * scale up, is val is negative, scale down. This function is used to scale
1303  * the operands or results of pointer arithmetic by the size of the type, the
1304  * pointer points to.
1305  */
1306 {
1307     int p2;
1308
1309     /* Value may not be zero */
1310     if (val == 0) {
1311         Internal ("Data type has no size");
1312     } else if (val > 0) {
1313
1314         /* Scale up */
1315         if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1316
1317             /* Factor is 2, 4, 8 and 16, use special function */
1318             switch (flags & CF_TYPE) {
1319
1320                 case CF_CHAR:
1321                     if (flags & CF_FORCECHAR) {
1322                         while (p2--) {
1323                             AddCodeLine ("asl a");
1324                         }
1325                         break;
1326                     }
1327                     /* FALLTHROUGH */
1328
1329                 case CF_INT:
1330                     if (CodeSizeFactor >= (p2+1)*130U) {
1331                         AddCodeLine ("stx tmp1");
1332                         while (p2--) {
1333                             AddCodeLine ("asl a");
1334                             AddCodeLine ("rol tmp1");
1335                         }
1336                         AddCodeLine ("ldx tmp1");
1337                     } else {
1338                         if (flags & CF_UNSIGNED) {
1339                             AddCodeLine ("jsr shlax%d", p2);
1340                         } else {
1341                             AddCodeLine ("jsr aslax%d", p2);
1342                         }
1343                     }
1344                     break;
1345
1346                 case CF_LONG:
1347                     if (flags & CF_UNSIGNED) {
1348                         AddCodeLine ("jsr shleax%d", p2);
1349                     } else {
1350                         AddCodeLine ("jsr asleax%d", p2);
1351                     }
1352                     break;
1353
1354                 default:
1355                     typeerror (flags);
1356
1357             }
1358
1359         } else if (val != 1) {
1360
1361             /* Use a multiplication instead */
1362             g_mul (flags | CF_CONST, val);
1363
1364         }
1365
1366     } else {
1367
1368         /* Scale down */
1369         val = -val;
1370         if ((p2 = powerof2 (val)) > 0 && p2 <= 4) {
1371
1372             /* Factor is 2, 4, 8 and 16 use special function */
1373             switch (flags & CF_TYPE) {
1374
1375                 case CF_CHAR:
1376                     if (flags & CF_FORCECHAR) {
1377                         if (flags & CF_UNSIGNED) {
1378                             while (p2--) {
1379                                 AddCodeLine ("lsr a");
1380                             }
1381                             break;
1382                         } else if (p2 <= 2) {
1383                             AddCodeLine ("cmp #$80");
1384                             AddCodeLine ("ror a");
1385                             break;
1386                         }
1387                     }
1388                     /* FALLTHROUGH */
1389
1390                 case CF_INT:
1391                     if (flags & CF_UNSIGNED) {
1392                         if (CodeSizeFactor >= (p2+1)*130U) {
1393                             AddCodeLine ("stx tmp1");
1394                             while (p2--) {
1395                                 AddCodeLine ("lsr tmp1");
1396                                 AddCodeLine ("ror a");
1397                             }
1398                             AddCodeLine ("ldx tmp1");
1399                         } else {
1400                             AddCodeLine ("jsr lsrax%d", p2);
1401                         }
1402                     } else {
1403                         if (CodeSizeFactor >= (p2+1)*150U) {
1404                             AddCodeLine ("stx tmp1");
1405                             while (p2--) {
1406                                 AddCodeLine ("cpx #$80");
1407                                 AddCodeLine ("ror tmp1");
1408                                 AddCodeLine ("ror a");
1409                             }
1410                             AddCodeLine ("ldx tmp1");
1411                         } else {
1412                             AddCodeLine ("jsr asrax%d", p2);
1413                         }
1414                     }
1415                     break;
1416
1417                 case CF_LONG:
1418                     if (flags & CF_UNSIGNED) {
1419                         AddCodeLine ("jsr lsreax%d", p2);
1420                     } else {
1421                         AddCodeLine ("jsr asreax%d", p2);
1422                     }
1423                     break;
1424
1425                 default:
1426                     typeerror (flags);
1427
1428             }
1429
1430         } else if (val != 1) {
1431
1432             /* Use a division instead */
1433             g_div (flags | CF_CONST, val);
1434
1435         }
1436     }
1437 }
1438
1439
1440
1441 /*****************************************************************************/
1442 /*              Adds and subs of variables fix a fixed address               */
1443 /*****************************************************************************/
1444
1445
1446
1447 void g_addlocal (unsigned flags, int offs)
1448 /* Add a local variable to ax */
1449 {
1450     unsigned L;
1451
1452     /* Correct the offset and check it */
1453     offs -= oursp;
1454     CheckLocalOffs (offs);
1455
1456     switch (flags & CF_TYPE) {
1457
1458         case CF_CHAR:
1459             L = GetLocalLabel();
1460             AddCodeLine ("ldy #$%02X", offs & 0xFF);
1461             AddCodeLine ("clc");
1462             AddCodeLine ("adc (sp),y");
1463             AddCodeLine ("bcc %s", LocalLabelName (L));
1464             AddCodeLine ("inx");
1465             g_defcodelabel (L);
1466             break;
1467
1468         case CF_INT:
1469             AddCodeLine ("ldy #$%02X", offs & 0xFF);
1470             AddCodeLine ("clc");
1471             AddCodeLine ("adc (sp),y");
1472             AddCodeLine ("pha");
1473             AddCodeLine ("txa");
1474             AddCodeLine ("iny");
1475             AddCodeLine ("adc (sp),y");
1476             AddCodeLine ("tax");
1477             AddCodeLine ("pla");
1478             break;
1479
1480         case CF_LONG:
1481             /* Do it the old way */
1482             g_push (flags, 0);
1483             g_getlocal (flags, offs);
1484             g_add (flags, 0);
1485             break;
1486
1487         default:
1488             typeerror (flags);
1489
1490     }
1491 }
1492
1493
1494
1495 void g_addstatic (unsigned flags, unsigned long label, unsigned offs)
1496 /* Add a static variable to ax */
1497 {
1498     unsigned L;
1499
1500     /* Create the correct label name */
1501     char* lbuf = GetLabelName (flags, label, offs);
1502
1503     switch (flags & CF_TYPE) {
1504
1505         case CF_CHAR:
1506             L = GetLocalLabel();
1507             AddCodeLine ("clc");
1508             AddCodeLine ("adc %s", lbuf);
1509             AddCodeLine ("bcc %s", LocalLabelName (L));
1510             AddCodeLine ("inx");
1511             g_defcodelabel (L);
1512             break;
1513
1514         case CF_INT:
1515             AddCodeLine ("clc");
1516             AddCodeLine ("adc %s", lbuf);
1517             AddCodeLine ("tay");
1518             AddCodeLine ("txa");
1519             AddCodeLine ("adc %s+1", lbuf);
1520             AddCodeLine ("tax");
1521             AddCodeLine ("tya");
1522             break;
1523
1524         case CF_LONG:
1525             /* Do it the old way */
1526             g_push (flags, 0);
1527             g_getstatic (flags, label, offs);
1528             g_add (flags, 0);
1529             break;
1530
1531         default:
1532             typeerror (flags);
1533
1534     }
1535 }
1536
1537
1538
1539 /*****************************************************************************/
1540 /*             Compares of ax with a variable with fixed address             */
1541 /*****************************************************************************/
1542
1543
1544
1545 void g_cmplocal (unsigned flags, int offs)
1546 /* Compare a local variable to ax */
1547 {
1548     Internal ("g_cmplocal not implemented");
1549 }
1550
1551
1552
1553 void g_cmpstatic (unsigned flags, unsigned label, unsigned offs)
1554 /* Compare a static variable to ax */
1555 {
1556     Internal ("g_cmpstatic not implemented");
1557 }
1558
1559
1560
1561 /*****************************************************************************/
1562 /*                           Special op= functions                           */
1563 /*****************************************************************************/
1564
1565
1566
1567 void g_addeqstatic (unsigned flags, unsigned long label, unsigned offs,
1568                     unsigned long val)
1569 /* Emit += for a static variable */
1570 {
1571     /* Create the correct label name */
1572     char* lbuf = GetLabelName (flags, label, offs);
1573
1574     /* Check the size and determine operation */
1575     switch (flags & CF_TYPE) {
1576
1577         case CF_CHAR:
1578             if (flags & CF_FORCECHAR) {
1579                 AddCodeLine ("ldx #$00");
1580                 if (flags & CF_CONST) {
1581                     if (val == 1) {
1582                         AddCodeLine ("inc %s", lbuf);
1583                         AddCodeLine ("lda %s", lbuf);
1584                     } else {
1585                         AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1586                         AddCodeLine ("clc");
1587                         AddCodeLine ("adc %s", lbuf);
1588                         AddCodeLine ("sta %s", lbuf);
1589                     }
1590                 } else {
1591                     AddCodeLine ("clc");
1592                     AddCodeLine ("adc %s", lbuf);
1593                     AddCodeLine ("sta %s", lbuf);
1594                 }
1595                 if ((flags & CF_UNSIGNED) == 0) {
1596                     unsigned L = GetLocalLabel();
1597                     AddCodeLine ("bpl %s", LocalLabelName (L));
1598                     AddCodeLine ("dex");
1599                     g_defcodelabel (L);
1600                 }
1601                 break;
1602             }
1603             /* FALLTHROUGH */
1604
1605         case CF_INT:
1606             if (flags & CF_CONST) {
1607                 if (val == 1) {
1608                     unsigned L = GetLocalLabel ();
1609                     AddCodeLine ("inc %s", lbuf);
1610                     AddCodeLine ("bne %s", LocalLabelName (L));
1611                     AddCodeLine ("inc %s+1", lbuf);
1612                     g_defcodelabel (L);
1613                     AddCodeLine ("lda %s", lbuf);               /* Hmmm... */
1614                     AddCodeLine ("ldx %s+1", lbuf);
1615                 } else {
1616                     AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1617                     AddCodeLine ("clc");
1618                     AddCodeLine ("adc %s", lbuf);
1619                     AddCodeLine ("sta %s", lbuf);
1620                     if (val < 0x100) {
1621                         unsigned L = GetLocalLabel ();
1622                         AddCodeLine ("bcc %s", LocalLabelName (L));
1623                         AddCodeLine ("inc %s+1", lbuf);
1624                         g_defcodelabel (L);
1625                         AddCodeLine ("ldx %s+1", lbuf);
1626                     } else {
1627                         AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1628                         AddCodeLine ("adc %s+1", lbuf);
1629                         AddCodeLine ("sta %s+1", lbuf);
1630                         AddCodeLine ("tax");
1631                         AddCodeLine ("lda %s", lbuf);
1632                     }
1633                 }
1634             } else {
1635                 AddCodeLine ("clc");
1636                 AddCodeLine ("adc %s", lbuf);
1637                 AddCodeLine ("sta %s", lbuf);
1638                 AddCodeLine ("txa");
1639                 AddCodeLine ("adc %s+1", lbuf);
1640                 AddCodeLine ("sta %s+1", lbuf);
1641                 AddCodeLine ("tax");
1642                 AddCodeLine ("lda %s", lbuf);
1643             }
1644             break;
1645
1646         case CF_LONG:
1647             if (flags & CF_CONST) {
1648                 if (val < 0x100) {
1649                     AddCodeLine ("ldy #<(%s)", lbuf);
1650                     AddCodeLine ("sty ptr1");
1651                     AddCodeLine ("ldy #>(%s+1)", lbuf);
1652                     if (val == 1) {
1653                         AddCodeLine ("jsr laddeq1");
1654                     } else {
1655                         AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1656                         AddCodeLine ("jsr laddeqa");
1657                     }
1658                 } else {
1659                     g_getstatic (flags, label, offs);
1660                     g_inc (flags, val);
1661                     g_putstatic (flags, label, offs);
1662                 }
1663             } else {
1664                 AddCodeLine ("ldy #<(%s)", lbuf);
1665                 AddCodeLine ("sty ptr1");
1666                 AddCodeLine ("ldy #>(%s+1)", lbuf);
1667                 AddCodeLine ("jsr laddeq");
1668             }
1669             break;
1670
1671         default:
1672             typeerror (flags);
1673     }
1674 }
1675
1676
1677
1678 void g_addeqlocal (unsigned flags, int offs, unsigned long val)
1679 /* Emit += for a local variable */
1680 {
1681     /* Calculate the true offset, check it, load it into Y */
1682     offs -= oursp;
1683     CheckLocalOffs (offs);
1684
1685     /* Check the size and determine operation */
1686     switch (flags & CF_TYPE) {
1687
1688         case CF_CHAR:
1689             if (flags & CF_FORCECHAR) {
1690                 ldyconst (offs);
1691                 AddCodeLine ("ldx #$00");
1692                 if (flags & CF_CONST) {
1693                     AddCodeLine ("clc");
1694                     AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1695                     AddCodeLine ("adc (sp),y");
1696                     AddCodeLine ("sta (sp),y");
1697                 } else {
1698                     AddCodeLine ("clc");
1699                     AddCodeLine ("adc (sp),y");
1700                     AddCodeLine ("sta (sp),y");
1701                 }
1702                 if ((flags & CF_UNSIGNED) == 0) {
1703                     unsigned L = GetLocalLabel();
1704                     AddCodeLine ("bpl %s", LocalLabelName (L));
1705                     AddCodeLine ("dex");
1706                     g_defcodelabel (L);
1707                 }
1708                 break;
1709             }
1710             /* FALLTHROUGH */
1711
1712         case CF_INT:
1713             if (flags & CF_CONST) {
1714                 g_getimmed (flags, val, 0);
1715             }
1716             if (offs == 0) {
1717                 AddCodeLine ("jsr addeq0sp");
1718             } else {
1719                 ldyconst (offs);
1720                 AddCodeLine ("jsr addeqysp");
1721             }
1722             break;
1723
1724         case CF_LONG:
1725             if (flags & CF_CONST) {
1726                 g_getimmed (flags, val, 0);
1727             }
1728             if (offs == 0) {
1729                 AddCodeLine ("jsr laddeq0sp");
1730             } else {
1731                 ldyconst (offs);
1732                 AddCodeLine ("jsr laddeqysp");
1733             }
1734             break;
1735
1736         default:
1737             typeerror (flags);
1738     }
1739 }
1740
1741
1742
1743 void g_addeqind (unsigned flags, unsigned offs, unsigned long val)
1744 /* Emit += for the location with address in ax */
1745 {
1746     /* If the offset is too large for a byte register, add the high byte
1747      * of the offset to the primary. Beware: We need a special correction
1748      * if the offset in the low byte will overflow in the operation.
1749      */
1750     offs = MakeByteOffs (flags, offs);
1751
1752     /* Check the size and determine operation */
1753     switch (flags & CF_TYPE) {
1754
1755         case CF_CHAR:
1756             AddCodeLine ("sta ptr1");
1757             AddCodeLine ("stx ptr1+1");
1758             AddCodeLine ("ldy #$%02X", offs);
1759             AddCodeLine ("ldx #$00");
1760             AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1761             AddCodeLine ("clc");
1762             AddCodeLine ("adc (ptr1),y");
1763             AddCodeLine ("sta (ptr1),y");
1764             break;
1765
1766         case CF_INT:
1767             if (CodeSizeFactor >= 200) {
1768                 /* Lots of code, use only if size is not important */
1769                 AddCodeLine ("sta ptr1");
1770                 AddCodeLine ("stx ptr1+1");
1771                 AddCodeLine ("ldy #$%02X", offs);
1772                 AddCodeLine ("lda #$%02X", (int)(val & 0xFF));
1773                 AddCodeLine ("clc");
1774                 AddCodeLine ("adc (ptr1),y");
1775                 AddCodeLine ("sta (ptr1),y");
1776                 AddCodeLine ("pha");
1777                 AddCodeLine ("iny");
1778                 AddCodeLine ("lda #$%02X", (unsigned char)(val >> 8));
1779                 AddCodeLine ("adc (ptr1),y");
1780                 AddCodeLine ("sta (ptr1),y");
1781                 AddCodeLine ("tax");
1782                 AddCodeLine ("pla");
1783                 break;
1784             }
1785             /* FALL THROUGH */
1786
1787         case CF_LONG:
1788             AddCodeLine ("jsr pushax");         /* Push the address */
1789             push (flags);                       /* Correct the internal sp */
1790             g_getind (flags, offs);             /* Fetch the value */
1791             g_inc (flags, val);                 /* Increment value in primary */
1792             g_putind (flags, offs);             /* Store the value back */
1793             break;
1794
1795         default:
1796             typeerror (flags);
1797     }
1798 }
1799
1800
1801
1802 void g_subeqstatic (unsigned flags, unsigned long label, unsigned offs,
1803                     unsigned long val)
1804 /* Emit -= for a static variable */
1805 {
1806     /* Create the correct label name */
1807     char* lbuf = GetLabelName (flags, label, offs);
1808
1809     /* Check the size and determine operation */
1810     switch (flags & CF_TYPE) {
1811
1812         case CF_CHAR:
1813             if (flags & CF_FORCECHAR) {
1814                 AddCodeLine ("ldx #$00");
1815                 if (flags & CF_CONST) {
1816                     if (val == 1) {
1817                         AddCodeLine ("dec %s", lbuf);
1818                         AddCodeLine ("lda %s", lbuf);
1819                     } else {
1820                         AddCodeLine ("sec");
1821                         AddCodeLine ("lda %s", lbuf);
1822                         AddCodeLine ("sbc #$%02X", (int)(val & 0xFF));
1823                         AddCodeLine ("sta %s", lbuf);
1824                     }
1825                 } else {
1826                     AddCodeLine ("sec");
1827                     AddCodeLine ("sta tmp1");
1828                     AddCodeLine ("lda %s", lbuf);
1829                     AddCodeLine ("sbc tmp1");
1830                     AddCodeLine ("sta %s", lbuf);
1831                 }
1832                 if ((flags & CF_UNSIGNED) == 0) {
1833                     unsigned L = GetLocalLabel();
1834                     AddCodeLine ("bpl %s", LocalLabelName (L));
1835                     AddCodeLine ("dex");
1836                     g_defcodelabel (L);
1837                 }
1838                 break;
1839             }
1840             /* FALLTHROUGH */
1841
1842         case CF_INT:
1843             AddCodeLine ("sec");
1844             if (flags & CF_CONST) {
1845                 AddCodeLine ("lda %s", lbuf);
1846                 AddCodeLine ("sbc #$%02X", (unsigned char)val);
1847                 AddCodeLine ("sta %s", lbuf);
1848                 if (val < 0x100) {
1849                     unsigned L = GetLocalLabel ();
1850                     AddCodeLine ("bcs %s", LocalLabelName (L));
1851                     AddCodeLine ("dec %s+1", lbuf);
1852                     g_defcodelabel (L);
1853                     AddCodeLine ("ldx %s+1", lbuf);
1854                 } else {
1855                     AddCodeLine ("lda %s+1", lbuf);
1856                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
1857                     AddCodeLine ("sta %s+1", lbuf);
1858                     AddCodeLine ("tax");
1859                     AddCodeLine ("lda %s", lbuf);
1860                 }
1861             } else {
1862                 AddCodeLine ("sta tmp1");
1863                 AddCodeLine ("lda %s", lbuf);
1864                 AddCodeLine ("sbc tmp1");
1865                 AddCodeLine ("sta %s", lbuf);
1866                 AddCodeLine ("stx tmp1");
1867                 AddCodeLine ("lda %s+1", lbuf);
1868                 AddCodeLine ("sbc tmp1");
1869                 AddCodeLine ("sta %s+1", lbuf);
1870                 AddCodeLine ("tax");
1871                 AddCodeLine ("lda %s", lbuf);
1872             }
1873             break;
1874
1875         case CF_LONG:
1876             if (flags & CF_CONST) {
1877                 if (val < 0x100) {
1878                     AddCodeLine ("ldy #<(%s)", lbuf);
1879                     AddCodeLine ("sty ptr1");
1880                     AddCodeLine ("ldy #>(%s+1)", lbuf);
1881                     if (val == 1) {
1882                         AddCodeLine ("jsr lsubeq1");
1883                     } else {
1884                         AddCodeLine ("lda #$%02X", (unsigned char)val);
1885                         AddCodeLine ("jsr lsubeqa");
1886                     }
1887                 } else {
1888                     g_getstatic (flags, label, offs);
1889                     g_dec (flags, val);
1890                     g_putstatic (flags, label, offs);
1891                 }
1892             } else {
1893                 AddCodeLine ("ldy #<(%s)", lbuf);
1894                 AddCodeLine ("sty ptr1");
1895                 AddCodeLine ("ldy #>(%s+1)", lbuf);
1896                 AddCodeLine ("jsr lsubeq");
1897             }
1898             break;
1899
1900         default:
1901             typeerror (flags);
1902     }
1903 }
1904
1905
1906
1907 void g_subeqlocal (unsigned flags, int offs, unsigned long val)
1908 /* Emit -= for a local variable */
1909 {
1910     /* Calculate the true offset, check it, load it into Y */
1911     offs -= oursp;
1912     CheckLocalOffs (offs);
1913
1914     /* Check the size and determine operation */
1915     switch (flags & CF_TYPE) {
1916
1917         case CF_CHAR:
1918             if (flags & CF_FORCECHAR) {
1919                 ldyconst (offs);
1920                 AddCodeLine ("ldx #$00");
1921                 AddCodeLine ("sec");
1922                 if (flags & CF_CONST) {
1923                     AddCodeLine ("lda (sp),y");
1924                     AddCodeLine ("sbc #$%02X", (unsigned char)val);
1925                 } else {
1926                     AddCodeLine ("sta tmp1");
1927                     AddCodeLine ("lda (sp),y");
1928                     AddCodeLine ("sbc tmp1");
1929                 }
1930                 AddCodeLine ("sta (sp),y");
1931                 if ((flags & CF_UNSIGNED) == 0) {
1932                     unsigned L = GetLocalLabel();
1933                     AddCodeLine ("bpl %s", LocalLabelName (L));
1934                     AddCodeLine ("dex");
1935                     g_defcodelabel (L);
1936                 }
1937                 break;
1938             }
1939             /* FALLTHROUGH */
1940
1941         case CF_INT:
1942             if (flags & CF_CONST) {
1943                 g_getimmed (flags, val, 0);
1944             }
1945             if (offs == 0) {
1946                 AddCodeLine ("jsr subeq0sp");
1947             } else {
1948                 ldyconst (offs);
1949                 AddCodeLine ("jsr subeqysp");
1950             }
1951             break;
1952
1953         case CF_LONG:
1954             if (flags & CF_CONST) {
1955                 g_getimmed (flags, val, 0);
1956             }
1957             if (offs == 0) {
1958                 AddCodeLine ("jsr lsubeq0sp");
1959             } else {
1960                 ldyconst (offs);
1961                 AddCodeLine ("jsr lsubeqysp");
1962             }
1963             break;
1964
1965         default:
1966             typeerror (flags);
1967     }
1968 }
1969
1970
1971
1972 void g_subeqind (unsigned flags, unsigned offs, unsigned long val)
1973 /* Emit -= for the location with address in ax */
1974 {
1975     /* If the offset is too large for a byte register, add the high byte
1976      * of the offset to the primary. Beware: We need a special correction
1977      * if the offset in the low byte will overflow in the operation.
1978      */
1979     offs = MakeByteOffs (flags, offs);
1980
1981     /* Check the size and determine operation */
1982     switch (flags & CF_TYPE) {
1983
1984         case CF_CHAR:
1985             AddCodeLine ("sta ptr1");
1986             AddCodeLine ("stx ptr1+1");
1987             AddCodeLine ("ldy #$%02X", offs);
1988             AddCodeLine ("ldx #$00");
1989             AddCodeLine ("lda (ptr1),y");
1990             AddCodeLine ("sec");
1991             AddCodeLine ("sbc #$%02X", (unsigned char)val);
1992             AddCodeLine ("sta (ptr1),y");
1993             break;
1994
1995         case CF_INT:
1996             if (CodeSizeFactor >= 200) {
1997                 /* Lots of code, use only if size is not important */
1998                 AddCodeLine ("sta ptr1");
1999                 AddCodeLine ("stx ptr1+1");
2000                 AddCodeLine ("ldy #$%02X", offs);
2001                 AddCodeLine ("lda (ptr1),y");
2002                 AddCodeLine ("sec");
2003                 AddCodeLine ("sbc #$%02X", (unsigned char)val);
2004                 AddCodeLine ("sta (ptr1),y");
2005                 AddCodeLine ("pha");
2006                 AddCodeLine ("iny");
2007                 AddCodeLine ("lda (ptr1),y");
2008                 AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
2009                 AddCodeLine ("sta (ptr1),y");
2010                 AddCodeLine ("tax");
2011                 AddCodeLine ("pla");
2012                 break;
2013             }
2014             /* FALL THROUGH */
2015
2016         case CF_LONG:
2017             AddCodeLine ("jsr pushax");         /* Push the address */
2018             push (flags);                       /* Correct the internal sp */
2019             g_getind (flags, offs);             /* Fetch the value */
2020             g_dec (flags, val);                 /* Increment value in primary */
2021             g_putind (flags, offs);             /* Store the value back */
2022             break;
2023
2024         default:
2025             typeerror (flags);
2026     }
2027 }
2028
2029
2030
2031 /*****************************************************************************/
2032 /*                 Add a variable address to the value in ax                 */
2033 /*****************************************************************************/
2034
2035
2036
2037 void g_addaddr_local (unsigned flags, int offs)
2038 /* Add the address of a local variable to ax */
2039 {
2040     unsigned L = 0;
2041
2042     /* Add the offset */
2043     offs -= oursp;
2044     if (offs != 0) {
2045         /* We cannot address more then 256 bytes of locals anyway */
2046         L = GetLocalLabel();
2047         CheckLocalOffs (offs);
2048         AddCodeLine ("clc");
2049         AddCodeLine ("adc #$%02X", offs & 0xFF);
2050         /* Do also skip the CLC insn below */
2051         AddCodeLine ("bcc %s", LocalLabelName (L));
2052         AddCodeLine ("inx");
2053     }
2054
2055     /* Add the current stackpointer value */
2056     AddCodeLine ("clc");
2057     if (L != 0) {
2058         /* Label was used above */
2059         g_defcodelabel (L);
2060     }
2061     AddCodeLine ("adc sp");
2062     AddCodeLine ("tay");
2063     AddCodeLine ("txa");
2064     AddCodeLine ("adc sp+1");
2065     AddCodeLine ("tax");
2066     AddCodeLine ("tya");
2067 }
2068
2069
2070
2071 void g_addaddr_static (unsigned flags, unsigned long label, unsigned offs)
2072 /* Add the address of a static variable to ax */
2073 {
2074     /* Create the correct label name */
2075     char* lbuf = GetLabelName (flags, label, offs);
2076
2077     /* Add the address to the current ax value */
2078     AddCodeLine ("clc");
2079     AddCodeLine ("adc #<(%s)", lbuf);
2080     AddCodeLine ("tay");
2081     AddCodeLine ("txa");
2082     AddCodeLine ("adc #>(%s)", lbuf);
2083     AddCodeLine ("tax");
2084     AddCodeLine ("tya");
2085 }
2086
2087
2088
2089 /*****************************************************************************/
2090 /*                                                                           */
2091 /*****************************************************************************/
2092
2093
2094
2095 void g_save (unsigned flags)
2096 /* Copy primary register to hold register. */
2097 {
2098     /* Check the size and determine operation */
2099     switch (flags & CF_TYPE) {
2100
2101         case CF_CHAR:
2102             if (flags & CF_FORCECHAR) {
2103                 AddCodeLine ("pha");
2104                 break;
2105             }
2106             /* FALLTHROUGH */
2107
2108         case CF_INT:
2109             AddCodeLine ("sta regsave");
2110             AddCodeLine ("stx regsave+1");
2111             break;
2112
2113         case CF_LONG:
2114             AddCodeLine ("jsr saveeax");
2115             break;
2116
2117         default:
2118             typeerror (flags);
2119     }
2120 }
2121
2122
2123
2124 void g_restore (unsigned flags)
2125 /* Copy hold register to primary. */
2126 {
2127     /* Check the size and determine operation */
2128     switch (flags & CF_TYPE) {
2129
2130         case CF_CHAR:
2131             if (flags & CF_FORCECHAR) {
2132                 AddCodeLine ("pla");
2133                 break;
2134             }
2135             /* FALLTHROUGH */
2136
2137         case CF_INT:
2138             AddCodeLine ("lda regsave");
2139             AddCodeLine ("ldx regsave+1");
2140             break;
2141
2142         case CF_LONG:
2143             AddCodeLine ("jsr resteax");
2144             break;
2145
2146         default:
2147             typeerror (flags);
2148     }
2149 }
2150
2151
2152
2153 void g_cmp (unsigned flags, unsigned long val)
2154 /* Immidiate compare. The primary register will not be changed, Z flag
2155  * will be set.
2156  */
2157 {
2158     unsigned L;
2159
2160     /* Check the size and determine operation */
2161     switch (flags & CF_TYPE) {
2162
2163         case CF_CHAR:
2164             if (flags & CF_FORCECHAR) {
2165                 AddCodeLine ("cmp #$%02X", (unsigned char)val);
2166                 break;
2167             }
2168             /* FALLTHROUGH */
2169
2170         case CF_INT:
2171             L = GetLocalLabel();
2172             AddCodeLine ("cmp #$%02X", (unsigned char)val);
2173             AddCodeLine ("bne %s", LocalLabelName (L));
2174             AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
2175             g_defcodelabel (L);
2176             break;
2177
2178         case CF_LONG:
2179             Internal ("g_cmp: Long compares not implemented");
2180             break;
2181
2182         default:
2183             typeerror (flags);
2184     }
2185 }
2186
2187
2188
2189 static void oper (unsigned flags, unsigned long val, char** subs)
2190 /* Encode a binary operation. subs is a pointer to four groups of three
2191  * strings:
2192  *      0-2     --> Operate on ints
2193  *      3-5     --> Operate on unsigneds
2194  *      6-8     --> Operate on longs
2195  *      9-11    --> Operate on unsigned longs
2196  *
2197  * The first subroutine names in each string group is used to encode an
2198  * operation with a zero constant, the second to encode an operation with
2199  * a 8 bit constant, and the third is used in all other cases.
2200  */
2201 {
2202     unsigned offs;
2203
2204     /* Determine the offset into the array */
2205     offs = (flags & CF_UNSIGNED)? 3 : 0;
2206     switch (flags & CF_TYPE) {
2207         case CF_CHAR:
2208         case CF_INT:
2209             break;
2210
2211         case CF_LONG:
2212             offs += 6;
2213             break;
2214
2215         default:
2216             typeerror (flags);
2217     }
2218
2219     /* Encode the operation */
2220     if (flags & CF_CONST) {
2221         /* Constant value given */
2222         if (val == 0 && subs [offs+0]) {
2223             /* Special case: constant with value zero */
2224             AddCodeLine ("jsr %s", subs [offs+0]);
2225         } else if (val < 0x100 && subs [offs+1]) {
2226             /* Special case: constant with high byte zero */
2227             ldaconst (val);             /* Load low byte */
2228             AddCodeLine ("jsr %s", subs [offs+1]);
2229         } else {
2230             /* Others: arbitrary constant value */
2231             g_getimmed (flags, val, 0);                 /* Load value */
2232             AddCodeLine ("jsr %s", subs [offs+2]);
2233         }
2234     } else {
2235         /* Value not constant (is already in (e)ax) */
2236         AddCodeLine ("jsr %s", subs [offs+2]);
2237     }
2238
2239     /* The operation will pop it's argument */
2240     pop (flags);
2241 }
2242
2243
2244
2245 void g_test (unsigned flags)
2246 /* Test the value in the primary and set the condition codes */
2247 {
2248     switch (flags & CF_TYPE) {
2249
2250         case CF_CHAR:
2251             if (flags & CF_FORCECHAR) {
2252                 AddCodeLine ("tax");
2253                 break;
2254             }
2255             /* FALLTHROUGH */
2256
2257         case CF_INT:
2258             AddCodeLine ("stx tmp1");
2259             AddCodeLine ("ora tmp1");
2260             break;
2261
2262         case CF_LONG:
2263             if (flags & CF_UNSIGNED) {
2264                 AddCodeLine ("jsr utsteax");
2265             } else {
2266                 AddCodeLine ("jsr tsteax");
2267             }
2268             break;
2269
2270         default:
2271             typeerror (flags);
2272
2273     }
2274 }
2275
2276
2277
2278 void g_push (unsigned flags, unsigned long val)
2279 /* Push the primary register or a constant value onto the stack */
2280 {
2281     unsigned char hi;
2282
2283     if (flags & CF_CONST && (flags & CF_TYPE) != CF_LONG) {
2284
2285         /* We have a constant 8 or 16 bit value */
2286         if ((flags & CF_TYPE) == CF_CHAR && (flags & CF_FORCECHAR)) {
2287
2288             /* Handle as 8 bit value */
2289             if (CodeSizeFactor >= 165 || val > 2) {
2290                 ldaconst (val);
2291                 AddCodeLine ("jsr pusha");
2292             } else {
2293                 AddCodeLine ("jsr pushc%d", (int) val);
2294             }
2295
2296         } else {
2297
2298             /* Handle as 16 bit value */
2299             hi = (unsigned char) (val >> 8);
2300             if (val <= 7) {
2301                 AddCodeLine ("jsr push%u", (unsigned) val);
2302             } else if (hi == 0 || hi == 0xFF) {
2303                 /* Use special function */
2304                 ldaconst (val);
2305                 AddCodeLine ("jsr %s", (hi == 0)? "pusha0" : "pushaFF");
2306             } else {
2307                 /* Long way ... */
2308                 g_getimmed (flags, val, 0);
2309                 AddCodeLine ("jsr pushax");
2310             }
2311         }
2312
2313     } else {
2314
2315         /* Value is not 16 bit or not constant */
2316         if (flags & CF_CONST) {
2317             /* Constant 32 bit value, load into eax */
2318             g_getimmed (flags, val, 0);
2319         }
2320
2321         /* Push the primary register */
2322         switch (flags & CF_TYPE) {
2323
2324             case CF_CHAR:
2325                 if (flags & CF_FORCECHAR) {
2326                     /* Handle as char */
2327                     AddCodeLine ("jsr pusha");
2328                     break;
2329                 }
2330                 /* FALL THROUGH */
2331             case CF_INT:
2332                 AddCodeLine ("jsr pushax");
2333                 break;
2334
2335             case CF_LONG:
2336                 AddCodeLine ("jsr pusheax");
2337                 break;
2338
2339             default:
2340                 typeerror (flags);
2341
2342         }
2343
2344     }
2345
2346     /* Adjust the stack offset */
2347     push (flags);
2348 }
2349
2350
2351
2352 void g_swap (unsigned flags)
2353 /* Swap the primary register and the top of the stack. flags give the type
2354  * of *both* values (must have same size).
2355  */
2356 {
2357     switch (flags & CF_TYPE) {
2358
2359         case CF_CHAR:
2360         case CF_INT:
2361             AddCodeLine ("jsr swapstk");
2362             break;
2363
2364         case CF_LONG:
2365             AddCodeLine ("jsr swapestk");
2366             break;
2367
2368         default:
2369             typeerror (flags);
2370
2371     }
2372 }
2373
2374
2375
2376 void g_call (unsigned Flags, const char* Label, unsigned ArgSize)
2377 /* Call the specified subroutine name */
2378 {
2379     if ((Flags & CF_FIXARGC) == 0) {
2380         /* Pass the argument count */
2381         ldyconst (ArgSize);
2382     }
2383     AddCodeLine ("jsr _%s", Label);
2384     oursp += ArgSize;           /* callee pops args */
2385 }
2386
2387
2388
2389 void g_callind (unsigned Flags, unsigned ArgSize)
2390 /* Call subroutine with address in AX */
2391 {
2392     if ((Flags & CF_FIXARGC) == 0) {
2393         /* Pass arg count */
2394         ldyconst (ArgSize);
2395     }
2396     AddCodeLine ("jsr callax"); /* do the call */
2397     oursp += ArgSize;                   /* callee pops args */
2398 }
2399
2400
2401
2402 void g_jump (unsigned Label)
2403 /* Jump to specified internal label number */
2404 {
2405     AddCodeLine ("jmp %s", LocalLabelName (Label));
2406 }
2407
2408
2409
2410 void g_switch (unsigned Flags)
2411 /* Output switch statement preamble */
2412 {
2413     switch (Flags & CF_TYPE) {
2414
2415         case CF_CHAR:
2416         case CF_INT:
2417             AddCodeLine ("jsr switch");
2418             break;
2419
2420         case CF_LONG:
2421             AddCodeLine ("jsr lswitch");
2422             break;
2423
2424         default:
2425             typeerror (Flags);
2426
2427     }
2428 }
2429
2430
2431
2432 void g_case (unsigned flags, unsigned label, unsigned long val)
2433 /* Create table code for one case selector */
2434 {
2435     switch (flags & CF_TYPE) {
2436
2437         case CF_CHAR:
2438         case CF_INT:
2439             AddCodeLine (".word $%04X, %s",
2440                          (unsigned)(val & 0xFFFF),
2441                          LocalLabelName (label));
2442             break;
2443
2444         case CF_LONG:
2445             AddCodeLine (".dword $%08lX", val);
2446             AddCodeLine (".word %s", LocalLabelName (label));
2447             break;
2448
2449         default:
2450             typeerror (flags);
2451
2452     }
2453 }
2454
2455
2456
2457 void g_truejump (unsigned flags, unsigned label)
2458 /* Jump to label if zero flag clear */
2459 {
2460     AddCodeLine ("jne %s", LocalLabelName (label));
2461 }
2462
2463
2464
2465 void g_falsejump (unsigned flags, unsigned label)
2466 /* Jump to label if zero flag set */
2467 {
2468     AddCodeLine ("jeq %s", LocalLabelName (label));
2469 }
2470
2471
2472
2473 static void mod_internal (int k, char* verb1, char* verb2)
2474 {
2475     if (k <= 8) {
2476         AddCodeLine ("jsr %ssp%c", verb1, k + '0');
2477     } else {
2478         CheckLocalOffs (k);
2479         ldyconst (k);
2480         AddCodeLine ("jsr %ssp", verb2);
2481     }
2482 }
2483
2484
2485
2486 void g_space (int space)
2487 /* Create or drop space on the stack */
2488 {
2489     if (space < 0) {
2490         mod_internal (-space, "inc", "addy");
2491     } else if (space > 0) {
2492         mod_internal (space, "dec", "suby");
2493     }
2494 }
2495
2496
2497
2498 void g_cstackcheck (void)
2499 /* Check for a C stack overflow */
2500 {
2501     AddCodeLine ("jsr cstkchk");
2502 }
2503
2504
2505
2506 void g_stackcheck (void)
2507 /* Check for a stack overflow */
2508 {
2509     AddCodeLine ("jsr stkchk");
2510 }
2511
2512
2513
2514 void g_add (unsigned flags, unsigned long val)
2515 /* Primary = TOS + Primary */
2516 {
2517     static char* ops [12] = {
2518         0,              "tosadda0",     "tosaddax",
2519         0,              "tosadda0",     "tosaddax",
2520         0,              0,              "tosaddeax",
2521         0,              0,              "tosaddeax",
2522     };
2523
2524     if (flags & CF_CONST) {
2525         flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2526         g_push (flags & ~CF_CONST, 0);
2527     }
2528     oper (flags, val, ops);
2529 }
2530
2531
2532
2533 void g_sub (unsigned flags, unsigned long val)
2534 /* Primary = TOS - Primary */
2535 {
2536     static char* ops [12] = {
2537         0,              "tossuba0",     "tossubax",
2538         0,              "tossuba0",     "tossubax",
2539         0,              0,              "tossubeax",
2540         0,              0,              "tossubeax",
2541     };
2542
2543     if (flags & CF_CONST) {
2544         flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2545         g_push (flags & ~CF_CONST, 0);
2546     }
2547     oper (flags, val, ops);
2548 }
2549
2550
2551
2552 void g_rsub (unsigned flags, unsigned long val)
2553 /* Primary = Primary - TOS */
2554 {
2555     static char* ops [12] = {
2556         0,              "tosrsuba0",    "tosrsubax",
2557         0,              "tosrsuba0",    "tosrsubax",
2558         0,              0,              "tosrsubeax",
2559         0,              0,              "tosrsubeax",
2560     };
2561     oper (flags, val, ops);
2562 }
2563
2564
2565
2566 void g_mul (unsigned flags, unsigned long val)
2567 /* Primary = TOS * Primary */
2568 {
2569     static char* ops [12] = {
2570         0,              "tosmula0",     "tosmulax",
2571         0,              "tosumula0",    "tosumulax",
2572         0,              0,              "tosmuleax",
2573         0,              0,              "tosumuleax",
2574     };
2575
2576     int p2;
2577
2578     /* Do strength reduction if the value is constant and a power of two */
2579     if (flags & CF_CONST && (p2 = powerof2 (val)) >= 0) {
2580         /* Generate a shift instead */
2581         g_asl (flags, p2);
2582         return;
2583     }
2584
2585     /* If the right hand side is const, the lhs is not on stack but still
2586      * in the primary register.
2587      */
2588     if (flags & CF_CONST) {
2589
2590         switch (flags & CF_TYPE) {
2591
2592             case CF_CHAR:
2593                 if (flags & CF_FORCECHAR) {
2594                     /* Handle some special cases */
2595                     switch (val) {
2596
2597                         case 3:
2598                             AddCodeLine ("sta tmp1");
2599                             AddCodeLine ("asl a");
2600                             AddCodeLine ("clc");
2601                             AddCodeLine ("adc tmp1");
2602                             return;
2603
2604                         case 5:
2605                             AddCodeLine ("sta tmp1");
2606                             AddCodeLine ("asl a");
2607                             AddCodeLine ("asl a");
2608                             AddCodeLine ("clc");
2609                             AddCodeLine ("adc tmp1");
2610                             return;
2611
2612                         case 10:
2613                             AddCodeLine ("sta tmp1");
2614                             AddCodeLine ("asl a");
2615                             AddCodeLine ("asl a");
2616                             AddCodeLine ("clc");
2617                             AddCodeLine ("adc tmp1");
2618                             AddCodeLine ("asl a");
2619                             return;
2620                     }
2621                 }
2622                 /* FALLTHROUGH */
2623
2624             case CF_INT:
2625                 break;
2626
2627             case CF_LONG:
2628                 break;
2629
2630             default:
2631                 typeerror (flags);
2632         }
2633
2634         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2635          * into the normal, non-optimized stuff.
2636          */
2637         flags &= ~CF_FORCECHAR; /* Handle chars as ints */
2638         g_push (flags & ~CF_CONST, 0);
2639
2640     }
2641
2642     /* Use long way over the stack */
2643     oper (flags, val, ops);
2644 }
2645
2646
2647
2648 void g_div (unsigned flags, unsigned long val)
2649 /* Primary = TOS / Primary */
2650 {
2651     static char* ops [12] = {
2652         0,              "tosdiva0",     "tosdivax",
2653         0,              "tosudiva0",    "tosudivax",
2654         0,              0,              "tosdiveax",
2655         0,              0,              "tosudiveax",
2656     };
2657
2658     /* Do strength reduction if the value is constant and a power of two */
2659     int p2;
2660     if ((flags & CF_CONST) && (p2 = powerof2 (val)) >= 0) {
2661         /* Generate a shift instead */
2662         g_asr (flags, p2);
2663     } else {
2664         /* Generate a division */
2665         if (flags & CF_CONST) {
2666             /* lhs is not on stack */
2667             flags &= ~CF_FORCECHAR;     /* Handle chars as ints */
2668             g_push (flags & ~CF_CONST, 0);
2669         }
2670         oper (flags, val, ops);
2671     }
2672 }
2673
2674
2675
2676 void g_mod (unsigned flags, unsigned long val)
2677 /* Primary = TOS % Primary */
2678 {
2679     static char* ops [12] = {
2680         0,              "tosmoda0",     "tosmodax",
2681         0,              "tosumoda0",    "tosumodax",
2682         0,              0,              "tosmodeax",
2683         0,              0,              "tosumodeax",
2684     };
2685     int p2;
2686
2687     /* Check if we can do some cost reduction */
2688     if ((flags & CF_CONST) && (flags & CF_UNSIGNED) && val != 0xFFFFFFFF && (p2 = powerof2 (val)) >= 0) {
2689         /* We can do that with an AND operation */
2690         g_and (flags, val - 1);
2691     } else {
2692         /* Do it the hard way... */
2693         if (flags & CF_CONST) {
2694             /* lhs is not on stack */
2695             flags &= ~CF_FORCECHAR;     /* Handle chars as ints */
2696             g_push (flags & ~CF_CONST, 0);
2697         }
2698         oper (flags, val, ops);
2699     }
2700 }
2701
2702
2703
2704 void g_or (unsigned flags, unsigned long val)
2705 /* Primary = TOS | Primary */
2706 {
2707     static char* ops [12] = {
2708         0,              "tosora0",      "tosorax",
2709         0,              "tosora0",      "tosorax",
2710         0,              0,              "tosoreax",
2711         0,              0,              "tosoreax",
2712     };
2713
2714     /* If the right hand side is const, the lhs is not on stack but still
2715      * in the primary register.
2716      */
2717     if (flags & CF_CONST) {
2718
2719         switch (flags & CF_TYPE) {
2720
2721             case CF_CHAR:
2722                 if (flags & CF_FORCECHAR) {
2723                     if ((val & 0xFF) != 0xFF) {
2724                         AddCodeLine ("ora #$%02X", (unsigned char)val);
2725                     }
2726                     return;
2727                 }
2728                 /* FALLTHROUGH */
2729
2730             case CF_INT:
2731                 if (val <= 0xFF) {
2732                     AddCodeLine ("ora #$%02X", (unsigned char)val);
2733                     return;
2734                 }
2735                 break;
2736
2737             case CF_LONG:
2738                 if (val <= 0xFF) {
2739                     AddCodeLine ("ora #$%02X", (unsigned char)val);
2740                     return;
2741                 }
2742                 break;
2743
2744             default:
2745                 typeerror (flags);
2746         }
2747
2748         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2749          * into the normal, non-optimized stuff.
2750          */
2751         g_push (flags & ~CF_CONST, 0);
2752
2753     }
2754
2755     /* Use long way over the stack */
2756     oper (flags, val, ops);
2757 }
2758
2759
2760
2761 void g_xor (unsigned flags, unsigned long val)
2762 /* Primary = TOS ^ Primary */
2763 {
2764     static char* ops [12] = {
2765         0,              "tosxora0",     "tosxorax",
2766         0,              "tosxora0",     "tosxorax",
2767         0,              0,              "tosxoreax",
2768         0,              0,              "tosxoreax",
2769     };
2770
2771
2772     /* If the right hand side is const, the lhs is not on stack but still
2773      * in the primary register.
2774      */
2775     if (flags & CF_CONST) {
2776
2777         switch (flags & CF_TYPE) {
2778
2779             case CF_CHAR:
2780                 if (flags & CF_FORCECHAR) {
2781                     if ((val & 0xFF) != 0) {
2782                         AddCodeLine ("eor #$%02X", (unsigned char)val);
2783                     }
2784                     return;
2785                 }
2786                 /* FALLTHROUGH */
2787
2788             case CF_INT:
2789                 if (val <= 0xFF) {
2790                     if (val != 0) {
2791                         AddCodeLine ("eor #$%02X", (unsigned char)val);
2792                     }
2793                     return;
2794                 } else if ((val & 0xFF) == 0) {
2795                     AddCodeLine ("pha");
2796                     AddCodeLine ("txa");
2797                     AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2798                     AddCodeLine ("tax");
2799                     AddCodeLine ("pla");
2800                     return;
2801                 }
2802                 break;
2803
2804             case CF_LONG:
2805                 if (val <= 0xFF) {
2806                     if (val != 0) {
2807                         AddCodeLine ("eor #$%02X", (unsigned char)val);
2808                     }
2809                     return;
2810                 }
2811                 break;
2812
2813             default:
2814                 typeerror (flags);
2815         }
2816
2817         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2818          * into the normal, non-optimized stuff.
2819          */
2820         g_push (flags & ~CF_CONST, 0);
2821
2822     }
2823
2824     /* Use long way over the stack */
2825     oper (flags, val, ops);
2826 }
2827
2828
2829
2830 void g_and (unsigned flags, unsigned long val)
2831 /* Primary = TOS & Primary */
2832 {
2833     static char* ops [12] = {
2834         0,              "tosanda0",     "tosandax",
2835         0,              "tosanda0",     "tosandax",
2836         0,              0,              "tosandeax",
2837         0,              0,              "tosandeax",
2838     };
2839
2840     /* If the right hand side is const, the lhs is not on stack but still
2841      * in the primary register.
2842      */
2843     if (flags & CF_CONST) {
2844
2845         switch (flags & CF_TYPE) {
2846
2847             case CF_CHAR:
2848                 if (flags & CF_FORCECHAR) {
2849                     AddCodeLine ("and #$%02X", (unsigned char)val);
2850                     return;
2851                 }
2852                 /* FALLTHROUGH */
2853             case CF_INT:
2854                 if ((val & 0xFFFF) != 0xFFFF) {
2855                     if (val <= 0xFF) {
2856                         ldxconst (0);
2857                         if (val == 0) {
2858                             ldaconst (0);
2859                         } else if (val != 0xFF) {
2860                             AddCodeLine ("and #$%02X", (unsigned char)val);
2861                         }
2862                     } else if ((val & 0xFF00) == 0xFF00) {
2863                         AddCodeLine ("and #$%02X", (unsigned char)val);
2864                     } else if ((val & 0x00FF) == 0x0000) {
2865                         AddCodeLine ("txa");
2866                         AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2867                         AddCodeLine ("tax");
2868                         ldaconst (0);
2869                     } else {
2870                         AddCodeLine ("tay");
2871                         AddCodeLine ("txa");
2872                         AddCodeLine ("and #$%02X", (unsigned char)(val >> 8));
2873                         AddCodeLine ("tax");
2874                         AddCodeLine ("tya");
2875                         if ((val & 0x00FF) != 0x00FF) {
2876                             AddCodeLine ("and #$%02X", (unsigned char)val);
2877                         }
2878                     }
2879                 }
2880                 return;
2881
2882             case CF_LONG:
2883                 if (val <= 0xFF) {
2884                     ldxconst (0);
2885                     AddCodeLine ("stx sreg+1");
2886                     AddCodeLine ("stx sreg");
2887                     if ((val & 0xFF) != 0xFF) {
2888                          AddCodeLine ("and #$%02X", (unsigned char)val);
2889                     }
2890                     return;
2891                 } else if (val == 0xFF00) {
2892                     ldaconst (0);
2893                     AddCodeLine ("sta sreg+1");
2894                     AddCodeLine ("sta sreg");
2895                     return;
2896                 }
2897                 break;
2898
2899             default:
2900                 typeerror (flags);
2901         }
2902
2903         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2904          * into the normal, non-optimized stuff.
2905          */
2906         g_push (flags & ~CF_CONST, 0);
2907
2908     }
2909
2910     /* Use long way over the stack */
2911     oper (flags, val, ops);
2912 }
2913
2914
2915
2916 void g_asr (unsigned flags, unsigned long val)
2917 /* Primary = TOS >> Primary */
2918 {
2919     static char* ops [12] = {
2920         0,              "tosasra0",     "tosasrax",
2921         0,              "tosshra0",     "tosshrax",
2922         0,              0,              "tosasreax",
2923         0,              0,              "tosshreax",
2924     };
2925
2926     /* If the right hand side is const, the lhs is not on stack but still
2927      * in the primary register.
2928      */
2929     if (flags & CF_CONST) {
2930
2931         switch (flags & CF_TYPE) {
2932
2933             case CF_CHAR:
2934             case CF_INT:
2935                 if (val >= 1 && val <= 4) {
2936                     if (flags & CF_UNSIGNED) {
2937                         AddCodeLine ("jsr shrax%ld", val);
2938                     } else {
2939                         AddCodeLine ("jsr asrax%ld", val);
2940                     }
2941                     return;
2942                 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2943                     AddCodeLine ("txa");
2944                     ldxconst (0);
2945                     return;
2946                 }
2947                 break;
2948
2949             case CF_LONG:
2950                 if (val >= 1 && val <= 4) {
2951                     if (flags & CF_UNSIGNED) {
2952                         AddCodeLine ("jsr shreax%ld", val);
2953                     } else {
2954                         AddCodeLine ("jsr asreax%ld", val);
2955                     }
2956                     return;
2957                 } else if (val == 8 && (flags & CF_UNSIGNED)) {
2958                     AddCodeLine ("txa");
2959                     AddCodeLine ("ldx sreg");
2960                     AddCodeLine ("ldy sreg+1");
2961                     AddCodeLine ("sty sreg");
2962                     AddCodeLine ("ldy #$00");
2963                     AddCodeLine ("sty sreg+1");
2964                     return;
2965                 } else if (val == 16) {
2966                     AddCodeLine ("ldy #$00");
2967                     AddCodeLine ("ldx sreg+1");
2968                     if ((flags & CF_UNSIGNED) == 0) {
2969                         unsigned L = GetLocalLabel();
2970                         AddCodeLine ("bpl %s", LocalLabelName (L));
2971                         AddCodeLine ("dey");
2972                         g_defcodelabel (L);
2973                     }
2974                     AddCodeLine ("lda sreg");
2975                     AddCodeLine ("sty sreg+1");
2976                     AddCodeLine ("sty sreg");
2977                     return;
2978                 }
2979                 break;
2980
2981             default:
2982                 typeerror (flags);
2983         }
2984
2985         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2986          * into the normal, non-optimized stuff.
2987          */
2988         g_push (flags & ~CF_CONST, 0);
2989
2990     }
2991
2992     /* Use long way over the stack */
2993     oper (flags, val, ops);
2994 }
2995
2996
2997
2998 void g_asl (unsigned flags, unsigned long val)
2999 /* Primary = TOS << Primary */
3000 {
3001     static char* ops [12] = {
3002         0,              "tosasla0",     "tosaslax",
3003         0,              "tosshla0",     "tosshlax",
3004         0,              0,              "tosasleax",
3005         0,              0,              "tosshleax",
3006     };
3007
3008
3009     /* If the right hand side is const, the lhs is not on stack but still
3010      * in the primary register.
3011      */
3012     if (flags & CF_CONST) {
3013
3014         switch (flags & CF_TYPE) {
3015
3016             case CF_CHAR:
3017             case CF_INT:
3018                 if (val >= 1 && val <= 4) {
3019                     if (flags & CF_UNSIGNED) {
3020                         AddCodeLine ("jsr shlax%ld", val);
3021                     } else {
3022                         AddCodeLine ("jsr aslax%ld", val);
3023                     }
3024                     return;
3025                 } else if (val == 8) {
3026                     AddCodeLine ("tax");
3027                     AddCodeLine ("lda #$00");
3028                     return;
3029                 }
3030                 break;
3031
3032             case CF_LONG:
3033                 if (val >= 1 && val <= 4) {
3034                     if (flags & CF_UNSIGNED) {
3035                         AddCodeLine ("jsr shleax%ld", val);
3036                     } else {
3037                         AddCodeLine ("jsr asleax%ld", val);
3038                     }
3039                     return;
3040                 } else if (val == 8) {
3041                     AddCodeLine ("ldy sreg");
3042                     AddCodeLine ("sty sreg+1");
3043                     AddCodeLine ("stx sreg");
3044                     AddCodeLine ("tax");
3045                     AddCodeLine ("lda #$00");
3046                     return;
3047                 } else if (val == 16) {
3048                     AddCodeLine ("stx sreg+1");
3049                     AddCodeLine ("sta sreg");
3050                     AddCodeLine ("lda #$00");
3051                     AddCodeLine ("tax");
3052                     return;
3053                 }
3054                 break;
3055
3056             default:
3057                 typeerror (flags);
3058         }
3059
3060         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3061          * into the normal, non-optimized stuff.
3062          */
3063         g_push (flags & ~CF_CONST, 0);
3064
3065     }
3066
3067     /* Use long way over the stack */
3068     oper (flags, val, ops);
3069 }
3070
3071
3072
3073 void g_neg (unsigned flags)
3074 /* Primary = -Primary */
3075 {
3076     switch (flags & CF_TYPE) {
3077
3078         case CF_CHAR:
3079         case CF_INT:
3080             AddCodeLine ("jsr negax");
3081             break;
3082
3083         case CF_LONG:
3084             AddCodeLine ("jsr negeax");
3085             break;
3086
3087         default:
3088             typeerror (flags);
3089     }
3090 }
3091
3092
3093
3094 void g_bneg (unsigned flags)
3095 /* Primary = !Primary */
3096 {
3097     switch (flags & CF_TYPE) {
3098
3099         case CF_CHAR:
3100             AddCodeLine ("jsr bnega");
3101             break;
3102
3103         case CF_INT:
3104             AddCodeLine ("jsr bnegax");
3105             break;
3106
3107         case CF_LONG:
3108             AddCodeLine ("jsr bnegeax");
3109             break;
3110
3111         default:
3112             typeerror (flags);
3113     }
3114 }
3115
3116
3117
3118 void g_com (unsigned flags)
3119 /* Primary = ~Primary */
3120 {
3121     switch (flags & CF_TYPE) {
3122
3123         case CF_CHAR:
3124         case CF_INT:
3125             AddCodeLine ("jsr complax");
3126             break;
3127
3128         case CF_LONG:
3129             AddCodeLine ("jsr compleax");
3130             break;
3131
3132         default:
3133             typeerror (flags);
3134     }
3135 }
3136
3137
3138
3139 void g_inc (unsigned flags, unsigned long val)
3140 /* Increment the primary register by a given number */
3141 {
3142     /* Don't inc by zero */
3143     if (val == 0) {
3144         return;
3145     }
3146
3147     /* Generate code for the supported types */
3148     flags &= ~CF_CONST;
3149     switch (flags & CF_TYPE) {
3150
3151         case CF_CHAR:
3152             if (flags & CF_FORCECHAR) {
3153                 if (CPU == CPU_65C02 && val <= 2) {
3154                     while (val--) {
3155                         AddCodeLine ("ina");
3156                     }
3157                 } else {
3158                     AddCodeLine ("clc");
3159                     AddCodeLine ("adc #$%02X", (unsigned char)val);
3160                 }
3161                 break;
3162             }
3163             /* FALLTHROUGH */
3164
3165         case CF_INT:
3166             if (CPU == CPU_65C02 && val == 1) {
3167                 unsigned L = GetLocalLabel();
3168                 AddCodeLine ("ina");
3169                 AddCodeLine ("bne %s", LocalLabelName (L));
3170                 AddCodeLine ("inx");
3171                 g_defcodelabel (L);
3172             } else if (CodeSizeFactor < 200) {
3173                 /* Use jsr calls */
3174                 if (val <= 8) {
3175                     AddCodeLine ("jsr incax%lu", val);
3176                 } else if (val <= 255) {
3177                     ldyconst (val);
3178                     AddCodeLine ("jsr incaxy");
3179                 } else {
3180                     g_add (flags | CF_CONST, val);
3181                 }
3182             } else {
3183                 /* Inline the code */
3184                 if (val < 0x300) {
3185                     if ((val & 0xFF) != 0) {
3186                         unsigned L = GetLocalLabel();
3187                         AddCodeLine ("clc");
3188                         AddCodeLine ("adc #$%02X", (unsigned char) val);
3189                         AddCodeLine ("bcc %s", LocalLabelName (L));
3190                         AddCodeLine ("inx");
3191                         g_defcodelabel (L);
3192                     }
3193                     if (val >= 0x100) {
3194                         AddCodeLine ("inx");
3195                     }
3196                     if (val >= 0x200) {
3197                         AddCodeLine ("inx");
3198                     }
3199                 } else {
3200                     AddCodeLine ("clc");
3201                     if ((val & 0xFF) != 0) {
3202                         AddCodeLine ("adc #$%02X", (unsigned char) val);
3203                     }
3204                     AddCodeLine ("pha");
3205                     AddCodeLine ("txa");
3206                     AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3207                     AddCodeLine ("tax");
3208                     AddCodeLine ("pla");
3209                 }
3210             }
3211             break;
3212
3213         case CF_LONG:
3214             if (val <= 255) {
3215                 ldyconst (val);
3216                 AddCodeLine ("jsr inceaxy");
3217             } else {
3218                 g_add (flags | CF_CONST, val);
3219             }
3220             break;
3221
3222         default:
3223             typeerror (flags);
3224
3225     }
3226 }
3227
3228
3229
3230 void g_dec (unsigned flags, unsigned long val)
3231 /* Decrement the primary register by a given number */
3232 {
3233     /* Don't dec by zero */
3234     if (val == 0) {
3235         return;
3236     }
3237
3238     /* Generate code for the supported types */
3239     flags &= ~CF_CONST;
3240     switch (flags & CF_TYPE) {
3241
3242         case CF_CHAR:
3243             if (flags & CF_FORCECHAR) {
3244                 if (CPU == CPU_65C02 && val <= 2) {
3245                     while (val--) {
3246                         AddCodeLine ("dea");
3247                     }
3248                 } else {
3249                     AddCodeLine ("sec");
3250                     AddCodeLine ("sbc #$%02X", (unsigned char)val);
3251                 }
3252                 break;
3253             }
3254             /* FALLTHROUGH */
3255
3256         case CF_INT:
3257             if (CodeSizeFactor < 200) {
3258                 /* Use subroutines */
3259                 if (val <= 8) {
3260                     AddCodeLine ("jsr decax%d", (int) val);
3261                 } else if (val <= 255) {
3262                     ldyconst (val);
3263                     AddCodeLine ("jsr decaxy");
3264                 } else {
3265                     g_sub (flags | CF_CONST, val);
3266                 }
3267             } else {
3268                 /* Inline the code */
3269                 if (val < 0x300) {
3270                     if ((val & 0xFF) != 0) {
3271                         unsigned L = GetLocalLabel();
3272                         AddCodeLine ("sec");
3273                         AddCodeLine ("sbc #$%02X", (unsigned char) val);
3274                         AddCodeLine ("bcs %s", LocalLabelName (L));
3275                         AddCodeLine ("dex");
3276                         g_defcodelabel (L);
3277                     }
3278                     if (val >= 0x100) {
3279                         AddCodeLine ("dex");
3280                     }
3281                     if (val >= 0x200) {
3282                         AddCodeLine ("dex");
3283                     }
3284                 } else {
3285                     AddCodeLine ("sec");
3286                     if ((val & 0xFF) != 0) {
3287                         AddCodeLine ("sbc #$%02X", (unsigned char) val);
3288                     }
3289                     AddCodeLine ("pha");
3290                     AddCodeLine ("txa");
3291                     AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3292                     AddCodeLine ("tax");
3293                     AddCodeLine ("pla");
3294                 }
3295             }
3296             break;
3297
3298         case CF_LONG:
3299             if (val <= 255) {
3300                 ldyconst (val);
3301                 AddCodeLine ("jsr deceaxy");
3302             } else {
3303                 g_sub (flags | CF_CONST, val);
3304             }
3305             break;
3306
3307         default:
3308             typeerror (flags);
3309
3310     }
3311 }
3312
3313
3314
3315 /*
3316  * Following are the conditional operators. They compare the TOS against
3317  * the primary and put a literal 1 in the primary if the condition is
3318  * true, otherwise they clear the primary register
3319  */
3320
3321
3322
3323 void g_eq (unsigned flags, unsigned long val)
3324 /* Test for equal */
3325 {
3326     static char* ops [12] = {
3327         "toseq00",      "toseqa0",      "toseqax",
3328         "toseq00",      "toseqa0",      "toseqax",
3329         0,              0,              "toseqeax",
3330         0,              0,              "toseqeax",
3331     };
3332
3333     unsigned L;
3334
3335     /* If the right hand side is const, the lhs is not on stack but still
3336      * in the primary register.
3337      */
3338     if (flags & CF_CONST) {
3339
3340         switch (flags & CF_TYPE) {
3341
3342             case CF_CHAR:
3343                 if (flags & CF_FORCECHAR) {
3344                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3345                     AddCodeLine ("jsr booleq");
3346                     return;
3347                 }
3348                 /* FALLTHROUGH */
3349
3350             case CF_INT:
3351                 L = GetLocalLabel();
3352                 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3353                 AddCodeLine ("bne %s", LocalLabelName (L));
3354                 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3355                 g_defcodelabel (L);
3356                 AddCodeLine ("jsr booleq");
3357                 return;
3358
3359             case CF_LONG:
3360                 break;
3361
3362             default:
3363                 typeerror (flags);
3364         }
3365
3366         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3367          * into the normal, non-optimized stuff.
3368          */
3369         g_push (flags & ~CF_CONST, 0);
3370
3371     }
3372
3373     /* Use long way over the stack */
3374     oper (flags, val, ops);
3375 }
3376
3377
3378
3379 void g_ne (unsigned flags, unsigned long val)
3380 /* Test for not equal */
3381 {
3382     static char* ops [12] = {
3383         "tosne00",      "tosnea0",      "tosneax",
3384         "tosne00",      "tosnea0",      "tosneax",
3385         0,              0,              "tosneeax",
3386         0,              0,              "tosneeax",
3387     };
3388
3389     unsigned L;
3390
3391     /* If the right hand side is const, the lhs is not on stack but still
3392      * in the primary register.
3393      */
3394     if (flags & CF_CONST) {
3395
3396         switch (flags & CF_TYPE) {
3397
3398             case CF_CHAR:
3399                 if (flags & CF_FORCECHAR) {
3400                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3401                     AddCodeLine ("jsr boolne");
3402                     return;
3403                 }
3404                 /* FALLTHROUGH */
3405
3406             case CF_INT:
3407                 L = GetLocalLabel();
3408                 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3409                 AddCodeLine ("bne %s", LocalLabelName (L));
3410                 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3411                 g_defcodelabel (L);
3412                 AddCodeLine ("jsr boolne");
3413                 return;
3414
3415             case CF_LONG:
3416                 break;
3417
3418             default:
3419                 typeerror (flags);
3420         }
3421
3422         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3423          * into the normal, non-optimized stuff.
3424          */
3425         g_push (flags & ~CF_CONST, 0);
3426
3427     }
3428
3429     /* Use long way over the stack */
3430     oper (flags, val, ops);
3431 }
3432
3433
3434
3435 void g_lt (unsigned flags, unsigned long val)
3436 /* Test for less than */
3437 {
3438     static char* ops [12] = {
3439         "toslt00",      "toslta0",      "tosltax",
3440         "tosult00",     "tosulta0",     "tosultax",
3441         0,              0,              "toslteax",
3442         0,              0,              "tosulteax",
3443     };
3444
3445     /* If the right hand side is const, the lhs is not on stack but still
3446      * in the primary register.
3447      */
3448     if (flags & CF_CONST) {
3449
3450         /* Give a warning in some special cases */
3451         if ((flags & CF_UNSIGNED) && val == 0) {
3452             Warning ("Condition is never true");
3453         }
3454
3455         /* Look at the type */
3456         switch (flags & CF_TYPE) {
3457
3458             case CF_CHAR:
3459                 if (flags & CF_FORCECHAR) {
3460                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3461                     if (flags & CF_UNSIGNED) {
3462                         AddCodeLine ("jsr boolult");
3463                     } else {
3464                         AddCodeLine ("jsr boollt");
3465                     }
3466                     return;
3467                 }
3468                 /* FALLTHROUGH */
3469
3470             case CF_INT:
3471                 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3472                     /* If we have a signed compare against zero, we only need to
3473                      * test the high byte.
3474                      */
3475                     AddCodeLine ("txa");
3476                     AddCodeLine ("jsr boollt");
3477                     return;
3478                 }
3479                 /* Direct code only for unsigned data types */
3480                 if (flags & CF_UNSIGNED) {
3481                     unsigned L = GetLocalLabel();
3482                     AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3483                     AddCodeLine ("bne %s", LocalLabelName (L));
3484                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3485                     g_defcodelabel (L);
3486                     AddCodeLine ("jsr boolult");
3487                     return;
3488                 }
3489                 break;
3490
3491             case CF_LONG:
3492                 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3493                     /* If we have a signed compare against zero, we only need to
3494                      * test the high byte.
3495                      */
3496                     AddCodeLine ("lda sreg+1");
3497                     AddCodeLine ("jsr boollt");
3498                     return;
3499                 }
3500                 break;
3501
3502             default:
3503                 typeerror (flags);
3504         }
3505
3506         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3507          * into the normal, non-optimized stuff.
3508          */
3509         g_push (flags & ~CF_CONST, 0);
3510
3511     }
3512
3513     /* Use long way over the stack */
3514     oper (flags, val, ops);
3515 }
3516
3517
3518
3519 void g_le (unsigned flags, unsigned long val)
3520 /* Test for less than or equal to */
3521 {
3522     static char* ops [12] = {
3523         "tosle00",      "toslea0",      "tosleax",
3524         "tosule00",     "tosulea0",     "tosuleax",
3525         0,              0,              "tosleeax",
3526         0,              0,              "tosuleeax",
3527     };
3528
3529
3530     /* If the right hand side is const, the lhs is not on stack but still
3531      * in the primary register.
3532      */
3533     if (flags & CF_CONST) {
3534
3535         /* <= is not very effective on the 6502, so try to convert
3536          * it into < if the value is in a valid range.
3537          */
3538
3539         /* Look at the type */
3540         switch (flags & CF_TYPE) {
3541
3542             case CF_CHAR:
3543                 if (flags & CF_FORCECHAR) {
3544                     if (flags & CF_UNSIGNED) {
3545                         if (val < 255) {
3546                             AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3547                             AddCodeLine ("jsr boolult");
3548                         } else {
3549                             AddCodeLine ("cmp #$%02X", (unsigned char)val);
3550                             AddCodeLine ("jsr boolule");
3551                         }
3552                     } else {
3553                         if (val < 127) {
3554                             AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3555                             AddCodeLine ("jsr boollt");
3556                         } else {
3557                             AddCodeLine ("cmp #$%02X", (unsigned char)val);
3558                             AddCodeLine ("jsr boolle");
3559                         }
3560                     }
3561                     return;
3562                 }
3563                 /* FALLTHROUGH */
3564
3565             case CF_INT:
3566                 if (flags & CF_UNSIGNED) {
3567                     unsigned L = GetLocalLabel();
3568                     const char* Name = "boolule";
3569                     if (val < 65535) {
3570                         ++val;
3571                         Name = "boolult";
3572                     }
3573                     AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3574                     AddCodeLine ("bne %s", LocalLabelName (L));
3575                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3576                     g_defcodelabel (L);
3577                     AddCodeLine ("jsr %s", Name);
3578                     return;
3579                 }
3580                 break;
3581
3582             case CF_LONG:
3583                 break;
3584
3585             default:
3586                 typeerror (flags);
3587         }
3588
3589         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3590          * into the normal, non-optimized stuff.
3591          */
3592         g_push (flags & ~CF_CONST, 0);
3593
3594     }
3595
3596     /* Use long way over the stack */
3597     oper (flags, val, ops);
3598 }
3599
3600
3601
3602 void g_gt (unsigned flags, unsigned long val)
3603 /* Test for greater than */
3604 {
3605     static char* ops [12] = {
3606         "tosgt00",      "tosgta0",      "tosgtax",
3607         "tosugt00",     "tosugta0",     "tosugtax",
3608         0,              0,              "tosgteax",
3609         0,              0,              "tosugteax",
3610     };
3611
3612
3613     /* If the right hand side is const, the lhs is not on stack but still
3614      * in the primary register.
3615      */
3616     if (flags & CF_CONST) {
3617
3618         /* > is not very effective on the 6502, so try to convert
3619          * it into >= if the value is in a valid range.
3620          */
3621
3622         /* Look at the type */
3623         switch (flags & CF_TYPE) {
3624
3625             case CF_CHAR:
3626                 if (flags & CF_FORCECHAR) {
3627                     if (flags & CF_UNSIGNED) {
3628                         /* If we have a compare > 0, we will replace it by
3629                          * != 0 here, since both are identical but the latter
3630                          * is easier to optimize.
3631                          */
3632                         if (val == 0) {
3633                             AddCodeLine ("cmp #$%02X", (unsigned char)val);
3634                             AddCodeLine ("jsr boolne");
3635                         } else if (val < 255) {
3636                             AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3637                             AddCodeLine ("jsr booluge");
3638                         } else {
3639                             AddCodeLine ("cmp #$%02X", (unsigned char)val);
3640                             AddCodeLine ("jsr boolugt");
3641                         }
3642                     } else {
3643                         if (val < 127) {
3644                             AddCodeLine ("cmp #$%02X", (unsigned char)val+1);
3645                             AddCodeLine ("jsr boolge");
3646                         } else {
3647                             AddCodeLine ("cmp #$%02X", (unsigned char)val);
3648                             AddCodeLine ("jsr boolgt");
3649                         }
3650                     }
3651                     return;
3652                 }
3653                 /* FALLTHROUGH */
3654
3655             case CF_INT:
3656                 if (flags & CF_UNSIGNED) {
3657                     /* If we have a compare > 0, we will replace it by
3658                      * != 0 here, since both are identical but the latter
3659                      * is easier to optimize.
3660                      */
3661                     if (val == 0) {
3662                         AddCodeLine ("stx tmp1");
3663                         AddCodeLine ("ora tmp1");
3664                         AddCodeLine ("jsr boolne");
3665                     } else {
3666                         unsigned L = GetLocalLabel();
3667                         const char* Name = "boolugt";
3668                         if (val < 65535) {
3669                             ++val;
3670                             Name = "booluge";
3671                         }
3672                         AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3673                         AddCodeLine ("bne %s", LocalLabelName (L));
3674                         AddCodeLine ("cmp #$%02X", (unsigned char)val);
3675                         g_defcodelabel (L);
3676                         AddCodeLine ("jsr %s", Name);
3677                     }
3678                     return;
3679                 }
3680                 break;
3681
3682             case CF_LONG:
3683                 break;
3684
3685             default:
3686                 typeerror (flags);
3687         }
3688
3689         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3690          * into the normal, non-optimized stuff.
3691          */
3692         g_push (flags & ~CF_CONST, 0);
3693
3694     }
3695
3696     /* Use long way over the stack */
3697     oper (flags, val, ops);
3698 }
3699
3700
3701
3702 void g_ge (unsigned flags, unsigned long val)
3703 /* Test for greater than or equal to */
3704 {
3705     static char* ops [12] = {
3706         "tosge00",      "tosgea0",      "tosgeax",
3707         "tosuge00",     "tosugea0",     "tosugeax",
3708         0,              0,              "tosgeeax",
3709         0,              0,              "tosugeeax",
3710     };
3711
3712
3713     /* If the right hand side is const, the lhs is not on stack but still
3714      * in the primary register.
3715      */
3716     if (flags & CF_CONST) {
3717
3718         /* Give a warning in some special cases */
3719         if ((flags & CF_UNSIGNED) && val == 0) {
3720             Warning ("Condition is always true");
3721         }
3722
3723         /* Look at the type */
3724         switch (flags & CF_TYPE) {
3725
3726             case CF_CHAR:
3727                 if (flags & CF_FORCECHAR) {
3728                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3729                     if (flags & CF_UNSIGNED) {
3730                         AddCodeLine ("jsr booluge");
3731                     } else {
3732                         AddCodeLine ("jsr boolge");
3733                     }
3734                     return;
3735                 }
3736                 /* FALLTHROUGH */
3737
3738             case CF_INT:
3739                 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3740                     /* If we have a signed compare against zero, we only need to
3741                      * test the high byte.
3742                      */
3743                     AddCodeLine ("txa");
3744                     AddCodeLine ("jsr boolge");
3745                     return;
3746                 }
3747                 /* Direct code only for unsigned data types */
3748                 if (flags & CF_UNSIGNED) {
3749                     unsigned L = GetLocalLabel();
3750                     AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3751                     AddCodeLine ("bne %s", LocalLabelName (L));
3752                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3753                     g_defcodelabel (L);
3754                     AddCodeLine ("jsr booluge");
3755                     return;
3756                 }
3757                 break;
3758
3759             case CF_LONG:
3760                 if ((flags & CF_UNSIGNED) == 0 && val == 0) {
3761                     /* If we have a signed compare against zero, we only need to
3762                      * test the high byte.
3763                      */
3764                     AddCodeLine ("lda sreg+1");
3765                     AddCodeLine ("jsr boolge");
3766                     return;
3767                 }
3768                 break;
3769
3770             default:
3771                 typeerror (flags);
3772         }
3773
3774         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3775          * into the normal, non-optimized stuff.
3776          */
3777         g_push (flags & ~CF_CONST, 0);
3778
3779     }
3780
3781     /* Use long way over the stack */
3782     oper (flags, val, ops);
3783 }
3784
3785
3786
3787 /*****************************************************************************/
3788 /*                         Allocating static storage                         */
3789 /*****************************************************************************/
3790
3791
3792
3793 void g_res (unsigned n)
3794 /* Reserve static storage, n bytes */
3795 {
3796     AddDataLine ("\t.res\t%u,$00", n);
3797 }
3798
3799
3800
3801 void g_defdata (unsigned flags, unsigned long val, unsigned offs)
3802 /* Define data with the size given in flags */
3803 {
3804     if (flags & CF_CONST) {
3805
3806         /* Numeric constant */
3807         switch (flags & CF_TYPE) {
3808
3809             case CF_CHAR:
3810                 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
3811                 break;
3812
3813             case CF_INT:
3814                 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
3815                 break;
3816
3817             case CF_LONG:
3818                 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
3819                 break;
3820
3821             default:
3822                 typeerror (flags);
3823                 break;
3824
3825         }
3826
3827     } else {
3828
3829         /* Create the correct label name */
3830         const char* Label = GetLabelName (flags, val, offs);
3831
3832         /* Labels are always 16 bit */
3833         AddDataLine ("\t.word\t%s", Label);
3834
3835     }
3836 }
3837
3838
3839
3840 void g_defbytes (const void* Bytes, unsigned Count)
3841 /* Output a row of bytes as a constant */
3842 {
3843     unsigned Chunk;
3844     char Buf [128];
3845     char* B;
3846
3847     /* Cast the buffer pointer */
3848     const unsigned char* Data = (const unsigned char*) Bytes;
3849
3850     /* Output the stuff */
3851     while (Count) {
3852
3853         /* How many go into this line? */
3854         if ((Chunk = Count) > 16) {
3855             Chunk = 16;
3856         }
3857         Count -= Chunk;
3858
3859         /* Output one line */
3860         strcpy (Buf, "\t.byte\t");
3861         B = Buf + 7;
3862         do {
3863             B += sprintf (B, "$%02X", *Data++);
3864             if (--Chunk) {
3865                 *B++ = ',';
3866             }
3867         } while (Chunk);
3868
3869         /* Output the line */
3870         AddDataLine (Buf);
3871     }
3872 }
3873
3874
3875
3876 void g_zerobytes (unsigned n)
3877 /* Output n bytes of data initialized with zero */
3878 {
3879     AddDataLine ("\t.res\t%u,$00", n);
3880 }
3881
3882
3883
3884 /*****************************************************************************/
3885 /*                       User supplied assembler code                        */
3886 /*****************************************************************************/
3887
3888
3889
3890 void g_asmcode (const char* Line, int Len)
3891 /* Output one line of assembler code. If Len is greater than zero, it is used
3892  * as the maximum number of characters to use from Line.
3893  */
3894 {
3895     if (Len >= 0) {
3896         AddCodeLine ("%.*s", Len, Line);
3897     } else {
3898         AddCodeLine ("%s", Line);
3899     }
3900 }
3901
3902
3903
3904 /*****************************************************************************/
3905 /*                          Inlined known functions                          */
3906 /*****************************************************************************/
3907
3908
3909
3910 void g_strlen (unsigned flags, unsigned long val, unsigned offs)
3911 /* Inline the strlen() function */
3912 {
3913     /* We need a label in both cases */
3914     unsigned label = GetLocalLabel ();
3915
3916     /* Two different encodings */
3917     if (flags & CF_CONST) {
3918
3919         /* The address of the string is constant. Create the correct label name */
3920         char* lbuf = GetLabelName (flags, val, offs);
3921
3922         /* Generate the strlen code */
3923         AddCodeLine ("ldy #$FF");
3924         g_defcodelabel (label);
3925         AddCodeLine ("iny");
3926         AddCodeLine ("lda %s,y", lbuf);
3927         AddCodeLine ("bne %s", LocalLabelName (label));
3928         AddCodeLine ("tax");
3929         AddCodeLine ("tya");
3930
3931     } else {
3932
3933         /* Address not constant but in primary */
3934         if (CodeSizeFactor < 400) {
3935             /* This is too much code, so call strlen instead of inlining */
3936             AddCodeLine ("jsr _strlen");
3937         } else {
3938             /* Inline the function */
3939             AddCodeLine ("sta ptr1");
3940             AddCodeLine ("stx ptr1+1");
3941             AddCodeLine ("ldy #$FF");
3942             g_defcodelabel (label);
3943             AddCodeLine ("iny");
3944             AddCodeLine ("lda (ptr1),y");
3945             AddCodeLine ("bne %s", LocalLabelName (label));
3946             AddCodeLine ("tax");
3947             AddCodeLine ("tya");
3948         }
3949     }
3950 }
3951
3952
3953