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