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