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