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