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