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