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