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