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