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