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