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