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