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