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