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