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