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