]> git.sur5r.net Git - cc65/blob - src/cc65/codegen.c
Special code for signed compares against zero
[cc65] / src / cc65 / codegen.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codegen.c                                 */
4 /*                                                                           */
5 /*                            6502 code generator                            */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 52                                              */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdarg.h>
39
40 /* common */
41 #include "check.h"
42 #include "cpu.h"
43 #include "strbuf.h"
44 #include "version.h"
45 #include "xmalloc.h"
46 #include "xsprintf.h"
47
48 /* cc65 */
49 #include "asmcode.h"
50 #include "asmlabel.h"
51 #include "casenode.h"
52 #include "codeseg.h"
53 #include "dataseg.h"
54 #include "error.h"
55 #include "global.h"
56 #include "segments.h"
57 #include "textseg.h"
58 #include "util.h"
59 #include "codegen.h"
60
61
62
63 /*****************************************************************************/
64 /*                                   Data                                    */
65 /*****************************************************************************/
66
67
68
69 /* Compiler relative stack pointer */
70 int oursp       = 0;
71
72
73
74 /*****************************************************************************/
75 /*                                  Helpers                                  */
76 /*****************************************************************************/
77
78
79
80 static void typeerror (unsigned type)
81 /* Print an error message about an invalid operand type */
82 {
83     Internal ("Invalid type in CF flags: %04X, type = %u", type, type & CF_TYPE);
84 }
85
86
87
88 static void CheckLocalOffs (unsigned Offs)
89 /* Check the offset into the stack for 8bit range */
90 {
91     if (Offs >= 256) {
92         /* Too many local vars */
93         Error ("Too many local variables");
94     }
95 }
96
97
98
99 static const char* GetLabelName (unsigned Flags, unsigned long Label, long Offs)
100 {
101     static char Buf [256];              /* Label name */
102
103     /* Create the correct label name */
104     switch (Flags & CF_ADDRMASK) {
105
106         case CF_STATIC:
107             /* Static memory cell */
108             if (Offs) {
109                 xsprintf (Buf, sizeof (Buf), "%s%+ld", LocalLabelName (Label), Offs);
110             } else {
111                 xsprintf (Buf, sizeof (Buf), "%s", LocalLabelName (Label));
112             }
113             break;
114
115         case CF_EXTERNAL:
116             /* External label */
117             if (Offs) {
118                 xsprintf (Buf, sizeof (Buf), "_%s%+ld", (char*) Label, Offs);
119             } else {
120                 xsprintf (Buf, sizeof (Buf), "_%s", (char*) Label);
121             }
122             break;
123
124         case CF_ABSOLUTE:
125             /* Absolute address */
126             xsprintf (Buf, sizeof (Buf), "$%04X", (int)((Label+Offs) & 0xFFFF));
127             break;
128
129         case CF_REGVAR:
130             /* Variable in register bank */
131             xsprintf (Buf, sizeof (Buf), "regbank+%u", (unsigned)((Label+Offs) & 0xFFFF));
132             break;
133
134         default:
135             Internal ("Invalid address flags: %04X", Flags);
136     }
137
138     /* Return a pointer to the static buffer */
139     return Buf;
140 }
141
142
143
144 /*****************************************************************************/
145 /*                            Pre- and postamble                             */
146 /*****************************************************************************/
147
148
149
150 void g_preamble (void)
151 /* Generate the assembler code preamble */
152 {
153     /* Create a new (global) segment list and remember it */
154     PushSegments (0);
155     GS = CS;
156
157     /* Identify the compiler version */
158     AddTextLine (";");
159     AddTextLine ("; File generated by cc65 v %u.%u.%u",
160                  VER_MAJOR, VER_MINOR, VER_PATCH);
161     AddTextLine (";");
162
163     /* Insert some object file options */
164     AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"",
165                     VER_MAJOR, VER_MINOR, VER_PATCH);
166
167     /* If we're producing code for some other CPU, switch the command set */
168     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) != 0) {
2776                         AddCodeLine ("ora #$%02X", (unsigned char)val);
2777                     }
2778                     return;
2779                 }
2780                 /* FALLTHROUGH */
2781
2782             case CF_INT:
2783                 if (val <= 0xFF) {
2784                     if ((val & 0xFF) != 0) {
2785                         AddCodeLine ("ora #$%02X", (unsigned char)val);
2786                     }
2787                     return;
2788                 } else if ((val & 0xFF00) == 0xFF00) {
2789                     if ((val & 0xFF) != 0) {
2790                         AddCodeLine ("ora #$%02X", (unsigned char)val);
2791                     }
2792                     ldxconst (0xFF);
2793                     return;
2794                 }
2795                 break;
2796
2797             case CF_LONG:
2798                 if (val <= 0xFF) {
2799                     if ((val & 0xFF) != 0) {
2800                         AddCodeLine ("ora #$%02X", (unsigned char)val);
2801                     }
2802                     return;
2803                 }
2804                 break;
2805
2806             default:
2807                 typeerror (flags);
2808         }
2809
2810         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2811          * into the normal, non-optimized stuff. Note: The standard stuff will
2812          * always work with ints.
2813          */
2814         flags &= ~CF_FORCECHAR;
2815         g_push (flags & ~CF_CONST, 0);
2816     }
2817
2818     /* Use long way over the stack */
2819     oper (flags, val, ops);
2820 }
2821
2822
2823
2824 void g_xor (unsigned flags, unsigned long val)
2825 /* Primary = TOS ^ Primary */
2826 {
2827     static char* ops [12] = {
2828         0,              "tosxora0",     "tosxorax",
2829         0,              "tosxora0",     "tosxorax",
2830         0,              0,              "tosxoreax",
2831         0,              0,              "tosxoreax",
2832     };
2833
2834
2835     /* If the right hand side is const, the lhs is not on stack but still
2836      * in the primary register.
2837      */
2838     if (flags & CF_CONST) {
2839
2840         switch (flags & CF_TYPE) {
2841
2842             case CF_CHAR:
2843                 if (flags & CF_FORCECHAR) {
2844                     if ((val & 0xFF) != 0) {
2845                         AddCodeLine ("eor #$%02X", (unsigned char)val);
2846                     }
2847                     return;
2848                 }
2849                 /* FALLTHROUGH */
2850
2851             case CF_INT:
2852                 if (val <= 0xFF) {
2853                     if (val != 0) {
2854                         AddCodeLine ("eor #$%02X", (unsigned char)val);
2855                     }
2856                     return;
2857                 } else if ((val & 0xFF) == 0) {
2858                     AddCodeLine ("pha");
2859                     AddCodeLine ("txa");
2860                     AddCodeLine ("eor #$%02X", (unsigned char)(val >> 8));
2861                     AddCodeLine ("tax");
2862                     AddCodeLine ("pla");
2863                     return;
2864                 }
2865                 break;
2866
2867             case CF_LONG:
2868                 if (val <= 0xFF) {
2869                     if (val != 0) {
2870                         AddCodeLine ("eor #$%02X", (unsigned char)val);
2871                     }
2872                     return;
2873                 }
2874                 break;
2875
2876             default:
2877                 typeerror (flags);
2878         }
2879
2880         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2881          * into the normal, non-optimized stuff. Note: The standard stuff will
2882          * always work with ints.
2883          */
2884         flags &= ~CF_FORCECHAR;
2885         g_push (flags & ~CF_CONST, 0);
2886     }
2887
2888     /* Use long way over the stack */
2889     oper (flags, val, ops);
2890 }
2891
2892
2893
2894 void g_and (unsigned Flags, unsigned long Val)
2895 /* Primary = TOS & Primary */
2896 {
2897     static char* ops [12] = {
2898         0,              "tosanda0",     "tosandax",
2899         0,              "tosanda0",     "tosandax",
2900         0,              0,              "tosandeax",
2901         0,              0,              "tosandeax",
2902     };
2903
2904     /* If the right hand side is const, the lhs is not on stack but still
2905      * in the primary register.
2906      */
2907     if (Flags & CF_CONST) {
2908
2909         switch (Flags & CF_TYPE) {
2910
2911             case CF_CHAR:
2912                 if (Flags & CF_FORCECHAR) {
2913                     if ((Val & 0xFF) != 0xFF) {
2914                         AddCodeLine ("and #$%02X", (unsigned char)Val);
2915                     }
2916                     return;
2917                 }
2918                 /* FALLTHROUGH */
2919             case CF_INT:
2920                 if ((Val & 0xFFFF) != 0xFFFF) {
2921                     if (Val <= 0xFF) {
2922                         ldxconst (0);
2923                         if (Val == 0) {
2924                             ldaconst (0);
2925                         } else if (Val != 0xFF) {
2926                             AddCodeLine ("and #$%02X", (unsigned char)Val);
2927                         }
2928                     } else if ((Val & 0xFF00) == 0xFF00) {
2929                         AddCodeLine ("and #$%02X", (unsigned char)Val);
2930                     } else if ((Val & 0x00FF) == 0x0000) {
2931                         AddCodeLine ("txa");
2932                         AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2933                         AddCodeLine ("tax");
2934                         ldaconst (0);
2935                     } else {
2936                         AddCodeLine ("tay");
2937                         AddCodeLine ("txa");
2938                         AddCodeLine ("and #$%02X", (unsigned char)(Val >> 8));
2939                         AddCodeLine ("tax");
2940                         AddCodeLine ("tya");
2941                         if ((Val & 0x00FF) != 0x00FF) {
2942                             AddCodeLine ("and #$%02X", (unsigned char)Val);
2943                         }
2944                     }
2945                 }
2946                 return;
2947
2948             case CF_LONG:
2949                 if (Val <= 0xFF) {
2950                     ldxconst (0);
2951                     AddCodeLine ("stx sreg+1");
2952                     AddCodeLine ("stx sreg");
2953                     if ((Val & 0xFF) != 0xFF) {
2954                          AddCodeLine ("and #$%02X", (unsigned char)Val);
2955                     }
2956                     return;
2957                 } else if (Val == 0xFF00) {
2958                     ldaconst (0);
2959                     AddCodeLine ("sta sreg+1");
2960                     AddCodeLine ("sta sreg");
2961                     return;
2962                 }
2963                 break;
2964
2965             default:
2966                 typeerror (Flags);
2967         }
2968
2969         /* If we go here, we didn't emit code. Push the lhs on stack and fall
2970          * into the normal, non-optimized stuff. Note: The standard stuff will
2971          * always work with ints.
2972          */
2973         Flags &= ~CF_FORCECHAR;
2974         g_push (Flags & ~CF_CONST, 0);
2975     }
2976
2977     /* Use long way over the stack */
2978     oper (Flags, Val, ops);
2979 }
2980
2981
2982
2983 void g_asr (unsigned flags, unsigned long val)
2984 /* Primary = TOS >> Primary */
2985 {
2986     static char* ops [12] = {
2987         0,              "tosasra0",     "tosasrax",
2988         0,              "tosshra0",     "tosshrax",
2989         0,              0,              "tosasreax",
2990         0,              0,              "tosshreax",
2991     };
2992
2993     /* If the right hand side is const, the lhs is not on stack but still
2994      * in the primary register.
2995      */
2996     if (flags & CF_CONST) {
2997
2998         switch (flags & CF_TYPE) {
2999
3000             case CF_CHAR:
3001             case CF_INT:
3002                 if (val >= 8 && (flags & CF_UNSIGNED)) {
3003                     AddCodeLine ("txa");
3004                     ldxconst (0);
3005                     val -= 8;
3006                 }
3007                 if (val == 0) {
3008                     /* Done */
3009                     return;
3010                 } else if (val >= 1 && val <= 4) {
3011                     if (flags & CF_UNSIGNED) {
3012                         AddCodeLine ("jsr shrax%ld", val);
3013                     } else {
3014                         AddCodeLine ("jsr asrax%ld", val);
3015                     }
3016                     return;
3017                 }
3018                 break;
3019
3020             case CF_LONG:
3021                 if (val == 0) {
3022                     /* Nothing to do */
3023                     return;
3024                 } else if (val >= 1 && val <= 4) {
3025                     if (flags & CF_UNSIGNED) {
3026                         AddCodeLine ("jsr shreax%ld", val);
3027                     } else {
3028                         AddCodeLine ("jsr asreax%ld", val);
3029                     }
3030                     return;
3031                 } else if (val == 8 && (flags & CF_UNSIGNED)) {
3032                     AddCodeLine ("txa");
3033                     AddCodeLine ("ldx sreg");
3034                     AddCodeLine ("ldy sreg+1");
3035                     AddCodeLine ("sty sreg");
3036                     AddCodeLine ("ldy #$00");
3037                     AddCodeLine ("sty sreg+1");
3038                     return;
3039                 } else if (val == 16) {
3040                     AddCodeLine ("ldy #$00");
3041                     AddCodeLine ("ldx sreg+1");
3042                     if ((flags & CF_UNSIGNED) == 0) {
3043                         unsigned L = GetLocalLabel();
3044                         AddCodeLine ("bpl %s", LocalLabelName (L));
3045                         AddCodeLine ("dey");
3046                         g_defcodelabel (L);
3047                     }
3048                     AddCodeLine ("lda sreg");
3049                     AddCodeLine ("sty sreg+1");
3050                     AddCodeLine ("sty sreg");
3051                     return;
3052                 }
3053                 break;
3054
3055             default:
3056                 typeerror (flags);
3057         }
3058
3059         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3060          * into the normal, non-optimized stuff. Note: The standard stuff will
3061          * always work with ints.
3062          */
3063         flags &= ~CF_FORCECHAR;
3064         g_push (flags & ~CF_CONST, 0);
3065     }
3066
3067     /* Use long way over the stack */
3068     oper (flags, val, ops);
3069 }
3070
3071
3072
3073 void g_asl (unsigned flags, unsigned long val)
3074 /* Primary = TOS << Primary */
3075 {
3076     static char* ops [12] = {
3077         0,              "tosasla0",     "tosaslax",
3078         0,              "tosshla0",     "tosshlax",
3079         0,              0,              "tosasleax",
3080         0,              0,              "tosshleax",
3081     };
3082
3083
3084     /* If the right hand side is const, the lhs is not on stack but still
3085      * in the primary register.
3086      */
3087     if (flags & CF_CONST) {
3088
3089         switch (flags & CF_TYPE) {
3090
3091             case CF_CHAR:
3092             case CF_INT:
3093                 if (val >= 8) {
3094                     AddCodeLine ("tax");
3095                     AddCodeLine ("lda #$00");
3096                     val -= 8;
3097                 }
3098                 if (val == 0) {
3099                     /* Done */
3100                     return;
3101                 } else if (val >= 1 && val <= 4) {
3102                     if (flags & CF_UNSIGNED) {
3103                         AddCodeLine ("jsr shlax%ld", val);
3104                     } else {
3105                         AddCodeLine ("jsr aslax%ld", val);
3106                     }
3107                     return;
3108                 }
3109                 break;
3110
3111             case CF_LONG:
3112                 if (val == 0) {
3113                     /* Nothing to do */
3114                     return;
3115                 } else if (val >= 1 && val <= 4) {
3116                     if (flags & CF_UNSIGNED) {
3117                         AddCodeLine ("jsr shleax%ld", val);
3118                     } else {
3119                         AddCodeLine ("jsr asleax%ld", val);
3120                     }
3121                     return;
3122                 } else if (val == 8) {
3123                     AddCodeLine ("ldy sreg");
3124                     AddCodeLine ("sty sreg+1");
3125                     AddCodeLine ("stx sreg");
3126                     AddCodeLine ("tax");
3127                     AddCodeLine ("lda #$00");
3128                     return;
3129                 } else if (val == 16) {
3130                     AddCodeLine ("stx sreg+1");
3131                     AddCodeLine ("sta sreg");
3132                     AddCodeLine ("lda #$00");
3133                     AddCodeLine ("tax");
3134                     return;
3135                 }
3136                 break;
3137
3138             default:
3139                 typeerror (flags);
3140         }
3141
3142         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3143          * into the normal, non-optimized stuff. Note: The standard stuff will
3144          * always work with ints.
3145          */
3146         flags &= ~CF_FORCECHAR;
3147         g_push (flags & ~CF_CONST, 0);
3148     }
3149
3150     /* Use long way over the stack */
3151     oper (flags, val, ops);
3152 }
3153
3154
3155
3156 void g_neg (unsigned Flags)
3157 /* Primary = -Primary */
3158 {
3159     switch (Flags & CF_TYPE) {
3160
3161         case CF_CHAR:
3162             if (Flags & CF_FORCECHAR) {
3163                 AddCodeLine ("eor #$FF");
3164                 AddCodeLine ("clc");
3165                 AddCodeLine ("adc #$01");
3166                 return;
3167             }
3168             /* FALLTHROUGH */
3169
3170         case CF_INT:
3171             AddCodeLine ("jsr negax");
3172             break;
3173
3174         case CF_LONG:
3175             AddCodeLine ("jsr negeax");
3176             break;
3177
3178         default:
3179             typeerror (Flags);
3180     }
3181 }
3182
3183
3184
3185 void g_bneg (unsigned flags)
3186 /* Primary = !Primary */
3187 {
3188     switch (flags & CF_TYPE) {
3189
3190         case CF_CHAR:
3191             AddCodeLine ("jsr bnega");
3192             break;
3193
3194         case CF_INT:
3195             AddCodeLine ("jsr bnegax");
3196             break;
3197
3198         case CF_LONG:
3199             AddCodeLine ("jsr bnegeax");
3200             break;
3201
3202         default:
3203             typeerror (flags);
3204     }
3205 }
3206
3207
3208
3209 void g_com (unsigned Flags)
3210 /* Primary = ~Primary */
3211 {
3212     switch (Flags & CF_TYPE) {
3213
3214         case CF_CHAR:
3215             if (Flags & CF_FORCECHAR) {
3216                 AddCodeLine ("eor #$FF");
3217                 return;
3218             }
3219             /* FALLTHROUGH */
3220
3221         case CF_INT:
3222             AddCodeLine ("jsr complax");
3223             break;
3224
3225         case CF_LONG:
3226             AddCodeLine ("jsr compleax");
3227             break;
3228
3229         default:
3230             typeerror (Flags);
3231     }
3232 }
3233
3234
3235
3236 void g_inc (unsigned flags, unsigned long val)
3237 /* Increment the primary register by a given number */
3238 {
3239     /* Don't inc by zero */
3240     if (val == 0) {
3241         return;
3242     }
3243
3244     /* Generate code for the supported types */
3245     flags &= ~CF_CONST;
3246     switch (flags & CF_TYPE) {
3247
3248         case CF_CHAR:
3249             if (flags & CF_FORCECHAR) {
3250                 if (CPU == CPU_65C02 && val <= 2) {
3251                     while (val--) {
3252                         AddCodeLine ("ina");
3253                     }
3254                 } else {
3255                     AddCodeLine ("clc");
3256                     AddCodeLine ("adc #$%02X", (unsigned char)val);
3257                 }
3258                 break;
3259             }
3260             /* FALLTHROUGH */
3261
3262         case CF_INT:
3263             if (CPU == CPU_65C02 && val == 1) {
3264                 unsigned L = GetLocalLabel();
3265                 AddCodeLine ("ina");
3266                 AddCodeLine ("bne %s", LocalLabelName (L));
3267                 AddCodeLine ("inx");
3268                 g_defcodelabel (L);
3269             } else if (CodeSizeFactor < 200) {
3270                 /* Use jsr calls */
3271                 if (val <= 8) {
3272                     AddCodeLine ("jsr incax%lu", val);
3273                 } else if (val <= 255) {
3274                     ldyconst (val);
3275                     AddCodeLine ("jsr incaxy");
3276                 } else {
3277                     g_add (flags | CF_CONST, val);
3278                 }
3279             } else {
3280                 /* Inline the code */
3281                 if (val <= 0x300) {
3282                     if ((val & 0xFF) != 0) {
3283                         unsigned L = GetLocalLabel();
3284                         AddCodeLine ("clc");
3285                         AddCodeLine ("adc #$%02X", (unsigned char) val);
3286                         AddCodeLine ("bcc %s", LocalLabelName (L));
3287                         AddCodeLine ("inx");
3288                         g_defcodelabel (L);
3289                     }
3290                     if (val >= 0x100) {
3291                         AddCodeLine ("inx");
3292                     }
3293                     if (val >= 0x200) {
3294                         AddCodeLine ("inx");
3295                     }
3296                     if (val >= 0x300) {
3297                         AddCodeLine ("inx");
3298                     }
3299                 } else {
3300                     AddCodeLine ("clc");
3301                     if ((val & 0xFF) != 0) {
3302                         AddCodeLine ("adc #$%02X", (unsigned char) val);
3303                     }
3304                     AddCodeLine ("pha");
3305                     AddCodeLine ("txa");
3306                     AddCodeLine ("adc #$%02X", (unsigned char) (val >> 8));
3307                     AddCodeLine ("tax");
3308                     AddCodeLine ("pla");
3309                 }
3310             }
3311             break;
3312
3313         case CF_LONG:
3314             if (val <= 255) {
3315                 ldyconst (val);
3316                 AddCodeLine ("jsr inceaxy");
3317             } else {
3318                 g_add (flags | CF_CONST, val);
3319             }
3320             break;
3321
3322         default:
3323             typeerror (flags);
3324
3325     }
3326 }
3327
3328
3329
3330 void g_dec (unsigned flags, unsigned long val)
3331 /* Decrement the primary register by a given number */
3332 {
3333     /* Don't dec by zero */
3334     if (val == 0) {
3335         return;
3336     }
3337
3338     /* Generate code for the supported types */
3339     flags &= ~CF_CONST;
3340     switch (flags & CF_TYPE) {
3341
3342         case CF_CHAR:
3343             if (flags & CF_FORCECHAR) {
3344                 if (CPU == CPU_65C02 && val <= 2) {
3345                     while (val--) {
3346                         AddCodeLine ("dea");
3347                     }
3348                 } else {
3349                     AddCodeLine ("sec");
3350                     AddCodeLine ("sbc #$%02X", (unsigned char)val);
3351                 }
3352                 break;
3353             }
3354             /* FALLTHROUGH */
3355
3356         case CF_INT:
3357             if (CodeSizeFactor < 200) {
3358                 /* Use subroutines */
3359                 if (val <= 8) {
3360                     AddCodeLine ("jsr decax%d", (int) val);
3361                 } else if (val <= 255) {
3362                     ldyconst (val);
3363                     AddCodeLine ("jsr decaxy");
3364                 } else {
3365                     g_sub (flags | CF_CONST, val);
3366                 }
3367             } else {
3368                 /* Inline the code */
3369                 if (val < 0x300) {
3370                     if ((val & 0xFF) != 0) {
3371                         unsigned L = GetLocalLabel();
3372                         AddCodeLine ("sec");
3373                         AddCodeLine ("sbc #$%02X", (unsigned char) val);
3374                         AddCodeLine ("bcs %s", LocalLabelName (L));
3375                         AddCodeLine ("dex");
3376                         g_defcodelabel (L);
3377                     }
3378                     if (val >= 0x100) {
3379                         AddCodeLine ("dex");
3380                     }
3381                     if (val >= 0x200) {
3382                         AddCodeLine ("dex");
3383                     }
3384                 } else {
3385                     AddCodeLine ("sec");
3386                     if ((val & 0xFF) != 0) {
3387                         AddCodeLine ("sbc #$%02X", (unsigned char) val);
3388                     }
3389                     AddCodeLine ("pha");
3390                     AddCodeLine ("txa");
3391                     AddCodeLine ("sbc #$%02X", (unsigned char) (val >> 8));
3392                     AddCodeLine ("tax");
3393                     AddCodeLine ("pla");
3394                 }
3395             }
3396             break;
3397
3398         case CF_LONG:
3399             if (val <= 255) {
3400                 ldyconst (val);
3401                 AddCodeLine ("jsr deceaxy");
3402             } else {
3403                 g_sub (flags | CF_CONST, val);
3404             }
3405             break;
3406
3407         default:
3408             typeerror (flags);
3409
3410     }
3411 }
3412
3413
3414
3415 /*
3416  * Following are the conditional operators. They compare the TOS against
3417  * the primary and put a literal 1 in the primary if the condition is
3418  * true, otherwise they clear the primary register
3419  */
3420
3421
3422
3423 void g_eq (unsigned flags, unsigned long val)
3424 /* Test for equal */
3425 {
3426     static char* ops [12] = {
3427         "toseq00",      "toseqa0",      "toseqax",
3428         "toseq00",      "toseqa0",      "toseqax",
3429         0,              0,              "toseqeax",
3430         0,              0,              "toseqeax",
3431     };
3432
3433     unsigned L;
3434
3435     /* If the right hand side is const, the lhs is not on stack but still
3436      * in the primary register.
3437      */
3438     if (flags & CF_CONST) {
3439
3440         switch (flags & CF_TYPE) {
3441
3442             case CF_CHAR:
3443                 if (flags & CF_FORCECHAR) {
3444                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3445                     AddCodeLine ("jsr booleq");
3446                     return;
3447                 }
3448                 /* FALLTHROUGH */
3449
3450             case CF_INT:
3451                 L = GetLocalLabel();
3452                 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3453                 AddCodeLine ("bne %s", LocalLabelName (L));
3454                 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3455                 g_defcodelabel (L);
3456                 AddCodeLine ("jsr booleq");
3457                 return;
3458
3459             case CF_LONG:
3460                 break;
3461
3462             default:
3463                 typeerror (flags);
3464         }
3465
3466         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3467          * into the normal, non-optimized stuff. Note: The standard stuff will
3468          * always work with ints.
3469          */
3470         flags &= ~CF_FORCECHAR;
3471         g_push (flags & ~CF_CONST, 0);
3472     }
3473
3474     /* Use long way over the stack */
3475     oper (flags, val, ops);
3476 }
3477
3478
3479
3480 void g_ne (unsigned flags, unsigned long val)
3481 /* Test for not equal */
3482 {
3483     static char* ops [12] = {
3484         "tosne00",      "tosnea0",      "tosneax",
3485         "tosne00",      "tosnea0",      "tosneax",
3486         0,              0,              "tosneeax",
3487         0,              0,              "tosneeax",
3488     };
3489
3490     unsigned L;
3491
3492     /* If the right hand side is const, the lhs is not on stack but still
3493      * in the primary register.
3494      */
3495     if (flags & CF_CONST) {
3496
3497         switch (flags & CF_TYPE) {
3498
3499             case CF_CHAR:
3500                 if (flags & CF_FORCECHAR) {
3501                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3502                     AddCodeLine ("jsr boolne");
3503                     return;
3504                 }
3505                 /* FALLTHROUGH */
3506
3507             case CF_INT:
3508                 L = GetLocalLabel();
3509                 AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3510                 AddCodeLine ("bne %s", LocalLabelName (L));
3511                 AddCodeLine ("cmp #$%02X", (unsigned char)val);
3512                 g_defcodelabel (L);
3513                 AddCodeLine ("jsr boolne");
3514                 return;
3515
3516             case CF_LONG:
3517                 break;
3518
3519             default:
3520                 typeerror (flags);
3521         }
3522
3523         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3524          * into the normal, non-optimized stuff. Note: The standard stuff will
3525          * always work with ints.
3526          */
3527         flags &= ~CF_FORCECHAR;
3528         g_push (flags & ~CF_CONST, 0);
3529     }
3530
3531     /* Use long way over the stack */
3532     oper (flags, val, ops);
3533 }
3534
3535
3536
3537 void g_lt (unsigned flags, unsigned long val)
3538 /* Test for less than */
3539 {
3540     static char* ops [12] = {
3541         "toslt00",      "toslta0",      "tosltax",
3542         "tosult00",     "tosulta0",     "tosultax",
3543         0,              0,              "toslteax",
3544         0,              0,              "tosulteax",
3545     };
3546
3547     /* If the right hand side is const, the lhs is not on stack but still
3548      * in the primary register.
3549      */
3550     if (flags & CF_CONST) {
3551
3552         /* Because the handling of the overflow flag is too complex for
3553          * inlining, we can handle only unsigned compares, and signed
3554          * compares against zero here.
3555          */
3556         if (flags & CF_UNSIGNED) {
3557
3558             /* Give a warning in some special cases */
3559             if (val == 0) {
3560                 Warning ("Condition is never true");
3561                 AddCodeLine ("jsr return0");
3562                 return;
3563             }
3564
3565             /* Look at the type */
3566             switch (flags & CF_TYPE) {
3567
3568                 case CF_CHAR:
3569                     if (flags & CF_FORCECHAR) {
3570                         AddCodeLine ("cmp #$%02X", (unsigned char)val);
3571                         AddCodeLine ("jsr boolult");
3572                         return;
3573                     }
3574                     /* FALLTHROUGH */
3575
3576                 case CF_INT:
3577                     /* If the low byte is zero, we must only test the high byte */
3578                     AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3579                     if ((val & 0xFF) != 0) {
3580                         unsigned L = GetLocalLabel();
3581                         AddCodeLine ("bne %s", LocalLabelName (L));
3582                         AddCodeLine ("cmp #$%02X", (unsigned char)val);
3583                         g_defcodelabel (L);
3584                     }
3585                     AddCodeLine ("jsr boolult");
3586                     return;
3587
3588                 case CF_LONG:
3589                     /* Do a subtraction */
3590                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3591                     AddCodeLine ("txa");
3592                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3593                     AddCodeLine ("lda sreg");
3594                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3595                     AddCodeLine ("lda sreg+1");
3596                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3597                     AddCodeLine ("jsr boolult");
3598                     return;
3599
3600                 default:
3601                     typeerror (flags);
3602             }
3603
3604         } else if (val == 0) {
3605
3606             /* Look at the type */
3607             switch (flags & CF_TYPE) {
3608
3609                 case CF_CHAR:
3610                     if (flags & CF_FORCECHAR) {
3611                         AddCodeLine ("tax");
3612                         AddCodeLine ("jsr boollt");
3613                         return;
3614                     }
3615                     /* FALLTHROUGH */
3616
3617                 case CF_INT:
3618                     /* Just check the high byte */
3619                     AddCodeLine ("txa");
3620                     AddCodeLine ("jsr boollt");
3621                     return;
3622
3623                 case CF_LONG:
3624                     /* Just check the high byte */
3625                     AddCodeLine ("lda sreg+1");
3626                     AddCodeLine ("jsr boollt");
3627                     return;
3628
3629                 default:
3630                     typeerror (flags);
3631             }
3632         }
3633
3634         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3635          * into the normal, non-optimized stuff. Note: The standard stuff will
3636          * always work with ints.
3637          */
3638         flags &= ~CF_FORCECHAR;
3639         g_push (flags & ~CF_CONST, 0);
3640     }
3641
3642     /* Use long way over the stack */
3643     oper (flags, val, ops);
3644 }
3645
3646
3647
3648 void g_le (unsigned flags, unsigned long val)
3649 /* Test for less than or equal to */
3650 {
3651     static char* ops [12] = {
3652         "tosle00",      "toslea0",      "tosleax",
3653         "tosule00",     "tosulea0",     "tosuleax",
3654         0,              0,              "tosleeax",
3655         0,              0,              "tosuleeax",
3656     };
3657
3658
3659     /* If the right hand side is const, the lhs is not on stack but still
3660      * in the primary register.
3661      */
3662     if (flags & CF_CONST) {
3663
3664         /* Look at the type */
3665         switch (flags & CF_TYPE) {
3666
3667             case CF_CHAR:
3668                 if (flags & CF_FORCECHAR) {
3669                     if (flags & CF_UNSIGNED) {
3670                         /* Unsigned compare */
3671                         if (val < 0xFF) {
3672                             /* Use < instead of <= because the former gives
3673                              * better code on the 6502 than the latter.
3674                              */
3675                             g_lt (flags, val+1);
3676                         } else {
3677                             /* Always true */
3678                             Warning ("Condition is always true");
3679                             AddCodeLine ("jsr return1");
3680                         }
3681                     } else {
3682                         /* Signed compare */
3683                         if ((long) val < 0x7F) {
3684                             /* Use < instead of <= because the former gives
3685                              * better code on the 6502 than the latter.
3686                              */
3687                             g_lt (flags, val+1);
3688                         } else {
3689                             /* Always true */
3690                             Warning ("Condition is always true");
3691                             AddCodeLine ("jsr return1");
3692                         }
3693                     }
3694                     return;
3695                 }
3696                 /* FALLTHROUGH */
3697
3698             case CF_INT:
3699                 if (flags & CF_UNSIGNED) {
3700                     /* Unsigned compare */
3701                     if (val < 0xFFFF) {
3702                         /* Use < instead of <= because the former gives
3703                          * better code on the 6502 than the latter.
3704                          */
3705                         g_lt (flags, val+1);
3706                     } else {
3707                         /* Always true */
3708                         Warning ("Condition is always true");
3709                         AddCodeLine ("jsr return1");
3710                     }
3711                 } else {
3712                     /* Signed compare */
3713                     if ((long) val < 0x7FFF) {
3714                         g_lt (flags, val+1);
3715                     } else {
3716                         /* Always true */
3717                         Warning ("Condition is always true");
3718                         AddCodeLine ("jsr return1");
3719                     }
3720                 }
3721                 return;
3722
3723             case CF_LONG:
3724                 if (flags & CF_UNSIGNED) {
3725                     /* Unsigned compare */
3726                     if (val < 0xFFFFFFFF) {
3727                         /* Use < instead of <= because the former gives
3728                          * better code on the 6502 than the latter.
3729                          */
3730                         g_lt (flags, val+1);
3731                     } else {
3732                         /* Always true */
3733                         Warning ("Condition is always true");
3734                         AddCodeLine ("jsr return1");
3735                     }
3736                 } else {
3737                     /* Signed compare */
3738                     if ((long) val < 0x7FFFFFFF) {
3739                         g_lt (flags, val+1);
3740                     } else {
3741                         /* Always true */
3742                         Warning ("Condition is always true");
3743                         AddCodeLine ("jsr return1");
3744                     }
3745                 }
3746                 return;
3747
3748             default:
3749                 typeerror (flags);
3750         }
3751
3752         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3753          * into the normal, non-optimized stuff. Note: The standard stuff will
3754          * always work with ints.
3755          */
3756         flags &= ~CF_FORCECHAR;
3757         g_push (flags & ~CF_CONST, 0);
3758     }
3759
3760     /* Use long way over the stack */
3761     oper (flags, val, ops);
3762 }
3763
3764
3765
3766 void g_gt (unsigned flags, unsigned long val)
3767 /* Test for greater than */
3768 {
3769     static char* ops [12] = {
3770         "tosgt00",      "tosgta0",      "tosgtax",
3771         "tosugt00",     "tosugta0",     "tosugtax",
3772         0,              0,              "tosgteax",
3773         0,              0,              "tosugteax",
3774     };
3775
3776
3777     /* If the right hand side is const, the lhs is not on stack but still
3778      * in the primary register.
3779      */
3780     if (flags & CF_CONST) {
3781
3782         /* Look at the type */
3783         switch (flags & CF_TYPE) {
3784
3785             case CF_CHAR:
3786                 if (flags & CF_FORCECHAR) {
3787                     if (flags & CF_UNSIGNED) {
3788                         if (val == 0) {
3789                             /* If we have a compare > 0, we will replace it by
3790                              * != 0 here, since both are identical but the
3791                              * latter is easier to optimize.
3792                              */
3793                             g_ne (flags, val);
3794                         } else if (val < 0xFF) {
3795                             /* Use >= instead of > because the former gives
3796                              * better code on the 6502 than the latter.
3797                              */
3798                             g_ge (flags, val+1);
3799                         } else {
3800                             /* Never true */
3801                             Warning ("Condition is never true");
3802                             AddCodeLine ("jsr return0");
3803                         }
3804                     } else {
3805                         if ((long) val < 0x7F) {
3806                             /* Use >= instead of > because the former gives
3807                              * better code on the 6502 than the latter.
3808                              */
3809                             g_ge (flags, val+1);
3810                         } else {
3811                             /* Never true */
3812                             Warning ("Condition is never true");
3813                             AddCodeLine ("jsr return0");
3814                         }
3815                     }
3816                     return;
3817                 }
3818                 /* FALLTHROUGH */
3819
3820             case CF_INT:
3821                 if (flags & CF_UNSIGNED) {
3822                     /* Unsigned compare */
3823                     if (val == 0) {
3824                         /* If we have a compare > 0, we will replace it by
3825                          * != 0 here, since both are identical but the latter
3826                          * is easier to optimize.
3827                          */
3828                         g_ne (flags, val);
3829                     } else if (val < 0xFFFF) {
3830                         /* Use >= instead of > because the former gives better
3831                          * code on the 6502 than the latter.
3832                          */
3833                         g_ge (flags, val+1);
3834                     } else {
3835                         /* Never true */
3836                         Warning ("Condition is never true");
3837                         AddCodeLine ("jsr return0");
3838                     }
3839                 } else {
3840                     /* Signed compare */
3841                     if ((long) val < 0x7FFF) {
3842                         g_ge (flags, val+1);
3843                     } else {
3844                         /* Never true */
3845                         Warning ("Condition is never true");
3846                         AddCodeLine ("jsr return0");
3847                     }
3848                 }
3849                 return;
3850
3851             case CF_LONG:
3852                 if (flags & CF_UNSIGNED) {
3853                     /* Unsigned compare */
3854                     if (val == 0) {
3855                         /* If we have a compare > 0, we will replace it by
3856                          * != 0 here, since both are identical but the latter
3857                          * is easier to optimize.
3858                          */
3859                         g_ne (flags, val);
3860                     } else if (val < 0xFFFFFFFF) {
3861                         /* Use >= instead of > because the former gives better
3862                          * code on the 6502 than the latter.
3863                          */
3864                         g_ge (flags, val+1);
3865                     } else {
3866                         /* Never true */
3867                         Warning ("Condition is never true");
3868                         AddCodeLine ("jsr return0");
3869                     }
3870                 } else {
3871                     /* Signed compare */
3872                     if ((long) val < 0x7FFFFFFF) {
3873                         g_ge (flags, val+1);
3874                     } else {
3875                         /* Never true */
3876                         Warning ("Condition is never true");
3877                         AddCodeLine ("jsr return0");
3878                     }
3879                 }
3880                 return;
3881
3882             default:
3883                 typeerror (flags);
3884         }
3885
3886         /* If we go here, we didn't emit code. Push the lhs on stack and fall
3887          * into the normal, non-optimized stuff. Note: The standard stuff will
3888          * always work with ints.
3889          */
3890         flags &= ~CF_FORCECHAR;
3891         g_push (flags & ~CF_CONST, 0);
3892     }
3893
3894     /* Use long way over the stack */
3895     oper (flags, val, ops);
3896 }
3897
3898
3899
3900 void g_ge (unsigned flags, unsigned long val)
3901 /* Test for greater than or equal to */
3902 {
3903     static char* ops [12] = {
3904         "tosge00",      "tosgea0",      "tosgeax",
3905         "tosuge00",     "tosugea0",     "tosugeax",
3906         0,              0,              "tosgeeax",
3907         0,              0,              "tosugeeax",
3908     };
3909
3910
3911     /* If the right hand side is const, the lhs is not on stack but still
3912      * in the primary register.
3913      */
3914     if (flags & CF_CONST) {
3915
3916         /* Because the handling of the overflow flag is too complex for
3917          * inlining, we can handle only unsigned compares, and signed
3918          * compares against zero here.
3919          */
3920         if (flags & CF_UNSIGNED) {
3921
3922             /* Give a warning in some special cases */
3923             if (val == 0) {
3924                 Warning ("Condition is always true");
3925                 AddCodeLine ("jsr return1");
3926                 return;
3927             }
3928
3929             /* Look at the type */
3930             switch (flags & CF_TYPE) {
3931
3932                 case CF_CHAR:
3933                     if (flags & CF_FORCECHAR) {
3934                         AddCodeLine ("cmp #$%02X", (unsigned char)val);
3935                         AddCodeLine ("jsr booluge");
3936                         return;
3937                     }
3938                     /* FALLTHROUGH */
3939
3940                 case CF_INT:
3941                     /* If the low byte is zero, we must only test the high byte */
3942                     AddCodeLine ("cpx #$%02X", (unsigned char)(val >> 8));
3943                     if ((val & 0xFF) != 0) {
3944                         unsigned L = GetLocalLabel();
3945                         AddCodeLine ("bne %s", LocalLabelName (L));
3946                         AddCodeLine ("cmp #$%02X", (unsigned char)val);
3947                         g_defcodelabel (L);
3948                     }
3949                     AddCodeLine ("jsr booluge");
3950                     return;
3951
3952                 case CF_LONG:
3953                     /* Do a subtraction */
3954                     AddCodeLine ("cmp #$%02X", (unsigned char)val);
3955                     AddCodeLine ("txa");
3956                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 8));
3957                     AddCodeLine ("lda sreg");
3958                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 16));
3959                     AddCodeLine ("lda sreg+1");
3960                     AddCodeLine ("sbc #$%02X", (unsigned char)(val >> 24));
3961                     AddCodeLine ("jsr booluge");
3962                     return;
3963
3964                 default:
3965                     typeerror (flags);
3966             }
3967
3968         } else if (val == 0) {
3969
3970             /* Look at the type */
3971             switch (flags & CF_TYPE) {
3972
3973                 case CF_CHAR:
3974                     if (flags & CF_FORCECHAR) {
3975                         AddCodeLine ("tax");
3976                         AddCodeLine ("jsr boolge");
3977                         return;
3978                     }
3979                     /* FALLTHROUGH */
3980
3981                 case CF_INT:
3982                     /* Just test the high byte */
3983                     AddCodeLine ("txa");
3984                     AddCodeLine ("jsr boolge");
3985                     return;
3986
3987                 case CF_LONG:
3988                     /* Just test the high byte */
3989                     AddCodeLine ("lda sreg+1");
3990                     AddCodeLine ("jsr boolge");
3991                     return;
3992
3993                 default:
3994                     typeerror (flags);
3995             }
3996         }
3997
3998
3999         /* If we go here, we didn't emit code. Push the lhs on stack and fall
4000          * into the normal, non-optimized stuff. Note: The standard stuff will
4001          * always work with ints.
4002          */
4003         flags &= ~CF_FORCECHAR;
4004         g_push (flags & ~CF_CONST, 0);
4005     }
4006
4007     /* Use long way over the stack */
4008     oper (flags, val, ops);
4009 }
4010
4011
4012
4013 /*****************************************************************************/
4014 /*                         Allocating static storage                         */
4015 /*****************************************************************************/
4016
4017
4018
4019 void g_res (unsigned n)
4020 /* Reserve static storage, n bytes */
4021 {
4022     AddDataLine ("\t.res\t%u,$00", n);
4023 }
4024
4025
4026
4027 void g_defdata (unsigned flags, unsigned long val, long offs)
4028 /* Define data with the size given in flags */
4029 {
4030     if (flags & CF_CONST) {
4031
4032         /* Numeric constant */
4033         switch (flags & CF_TYPE) {
4034
4035             case CF_CHAR:
4036                 AddDataLine ("\t.byte\t$%02lX", val & 0xFF);
4037                 break;
4038
4039             case CF_INT:
4040                 AddDataLine ("\t.word\t$%04lX", val & 0xFFFF);
4041                 break;
4042
4043             case CF_LONG:
4044                 AddDataLine ("\t.dword\t$%08lX", val & 0xFFFFFFFF);
4045                 break;
4046
4047             default:
4048                 typeerror (flags);
4049                 break;
4050
4051         }
4052
4053     } else {
4054
4055         /* Create the correct label name */
4056         const char* Label = GetLabelName (flags, val, offs);
4057
4058         /* Labels are always 16 bit */
4059         AddDataLine ("\t.addr\t%s", Label);
4060
4061     }
4062 }
4063
4064
4065
4066 void g_defbytes (const void* Bytes, unsigned Count)
4067 /* Output a row of bytes as a constant */
4068 {
4069     unsigned Chunk;
4070     char Buf [128];
4071     char* B;
4072
4073     /* Cast the buffer pointer */
4074     const unsigned char* Data = (const unsigned char*) Bytes;
4075
4076     /* Output the stuff */
4077     while (Count) {
4078
4079         /* How many go into this line? */
4080         if ((Chunk = Count) > 16) {
4081             Chunk = 16;
4082         }
4083         Count -= Chunk;
4084
4085         /* Output one line */
4086         strcpy (Buf, "\t.byte\t");
4087         B = Buf + 7;
4088         do {
4089             B += sprintf (B, "$%02X", *Data++);
4090             if (--Chunk) {
4091                 *B++ = ',';
4092             }
4093         } while (Chunk);
4094
4095         /* Output the line */
4096         AddDataLine (Buf);
4097     }
4098 }
4099
4100
4101
4102 void g_zerobytes (unsigned Count)
4103 /* Output Count bytes of data initialized with zero */
4104 {
4105     if (Count > 0) {
4106         AddDataLine ("\t.res\t%u,$00", Count);
4107     }
4108 }
4109
4110
4111
4112 void g_initregister (unsigned Label, unsigned Reg, unsigned Size)
4113 /* Initialize a register variable from static initialization data */
4114 {
4115     /* Register variables do always have less than 128 bytes */
4116     unsigned CodeLabel = GetLocalLabel ();
4117     ldxconst (Size-1);
4118     g_defcodelabel (CodeLabel);
4119     AddCodeLine ("lda %s,x", GetLabelName (CF_STATIC, Label, 0));
4120     AddCodeLine ("sta %s,x", GetLabelName (CF_REGVAR, Reg, 0));
4121     AddCodeLine ("dex");
4122     AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4123 }
4124
4125
4126
4127 void g_initauto (unsigned Label, unsigned Size)
4128 /* Initialize a local variable at stack offset zero from static data */
4129 {
4130     unsigned CodeLabel = GetLocalLabel ();
4131
4132     CheckLocalOffs (Size);
4133     if (Size <= 128) {
4134         ldyconst (Size-1);
4135         g_defcodelabel (CodeLabel);
4136         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4137         AddCodeLine ("sta (sp),y");
4138         AddCodeLine ("dey");
4139         AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4140     } else if (Size <= 256) {
4141         ldyconst (0);
4142         g_defcodelabel (CodeLabel);
4143         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, Label, 0));
4144         AddCodeLine ("sta (sp),y");
4145         AddCodeLine ("iny");
4146         AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4147         AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4148     }
4149 }
4150
4151
4152
4153 void g_initstatic (unsigned InitLabel, unsigned VarLabel, unsigned Size)
4154 /* Initialize a static local variable from static initialization data */
4155 {
4156     if (Size <= 128) {
4157         unsigned CodeLabel = GetLocalLabel ();
4158         ldyconst (Size-1);
4159         g_defcodelabel (CodeLabel);
4160         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4161         AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4162         AddCodeLine ("dey");
4163         AddCodeLine ("bpl %s", LocalLabelName (CodeLabel));
4164     } else if (Size <= 256) {
4165         unsigned CodeLabel = GetLocalLabel ();
4166         ldyconst (0);
4167         g_defcodelabel (CodeLabel);
4168         AddCodeLine ("lda %s,y", GetLabelName (CF_STATIC, InitLabel, 0));
4169         AddCodeLine ("sta %s,y", GetLabelName (CF_STATIC, VarLabel, 0));
4170         AddCodeLine ("iny");
4171         AddCodeLine ("cpy #$%02X", (unsigned char) Size);
4172         AddCodeLine ("bne %s", LocalLabelName (CodeLabel));
4173     } else {
4174         /* Use the easy way here: memcpy */
4175         g_getimmed (CF_STATIC, VarLabel, 0);
4176         AddCodeLine ("jsr pushax");
4177         g_getimmed (CF_STATIC, InitLabel, 0);
4178         AddCodeLine ("jsr pushax");
4179         g_getimmed (CF_INT | CF_UNSIGNED | CF_CONST, Size, 0);
4180         AddCodeLine ("jsr %s", GetLabelName (CF_EXTERNAL, (unsigned long) "memcpy", 0));
4181     }
4182 }
4183
4184
4185
4186 /*****************************************************************************/
4187 /*                             Switch statement                              */
4188 /*****************************************************************************/
4189
4190
4191
4192 void g_switch (Collection* Nodes, unsigned DefaultLabel, unsigned Depth)
4193 /* Generate code for a switch statement */
4194 {
4195     unsigned NextLabel = 0;
4196     unsigned I;
4197
4198     /* Setup registers and determine which compare insn to use */
4199     const char* Compare;
4200     switch (Depth) {
4201         case 1:
4202             Compare = "cmp #$%02X";
4203             break;
4204         case 2:
4205             Compare = "cpx #$%02X";
4206             break;
4207         case 3:
4208             AddCodeLine ("ldy sreg");
4209             Compare = "cpy #$%02X";
4210             break;
4211         case 4:
4212             AddCodeLine ("ldy sreg+1");
4213             Compare = "cpy #$%02X";
4214             break;
4215         default:
4216             Internal ("Invalid depth in g_switch: %u", Depth);
4217     }
4218
4219     /* Walk over all nodes */
4220     for (I = 0; I < CollCount (Nodes); ++I) {
4221
4222         /* Get the next case node */
4223         CaseNode* N = CollAtUnchecked (Nodes, I);
4224
4225         /* If we have a next label, define it */
4226         if (NextLabel) {
4227             g_defcodelabel (NextLabel);
4228             NextLabel = 0;
4229         }
4230
4231         /* Do the compare */
4232         AddCodeLine (Compare, CN_GetValue (N));
4233
4234         /* If this is the last level, jump directly to the case code if found */
4235         if (Depth == 1) {
4236
4237             /* Branch if equal */
4238             g_falsejump (0, CN_GetLabel (N));
4239
4240         } else {
4241
4242             /* Determine the next label */
4243             if (I == CollCount (Nodes) - 1) {
4244                 /* Last node means not found */
4245                 g_truejump (0, DefaultLabel);
4246             } else {
4247                 /* Jump to the next check */
4248                 NextLabel = GetLocalLabel ();
4249                 g_truejump (0, NextLabel);
4250             }
4251
4252             /* Check the next level */
4253             g_switch (N->Nodes, DefaultLabel, Depth-1);
4254
4255         }
4256     }
4257
4258     /* If we go here, we haven't found the label */
4259     g_jump (DefaultLabel);
4260 }
4261
4262
4263
4264 /*****************************************************************************/
4265 /*                       User supplied assembler code                        */
4266 /*****************************************************************************/
4267
4268
4269
4270 void g_asmcode (struct StrBuf* B)
4271 /* Output one line of assembler code. */
4272 {
4273     AddCodeLine ("%.*s", SB_GetLen (B), SB_GetConstBuf (B));
4274 }
4275
4276
4277
4278 /*****************************************************************************/
4279 /*                          Inlined known functions                          */
4280 /*****************************************************************************/
4281
4282
4283
4284 void g_strlen (unsigned flags, unsigned long val, long offs)
4285 /* Inline the strlen() function */
4286 {
4287     /* We need a label in both cases */
4288     unsigned label = GetLocalLabel ();
4289
4290     /* Two different encodings */
4291     if (flags & CF_CONST) {
4292
4293         /* The address of the string is constant. Create the correct label name */
4294         const char* lbuf = GetLabelName (flags, val, offs);
4295
4296         /* Generate the strlen code */
4297         AddCodeLine ("ldy #$FF");
4298         g_defcodelabel (label);
4299         AddCodeLine ("iny");
4300         AddCodeLine ("lda %s,y", lbuf);
4301         AddCodeLine ("bne %s", LocalLabelName (label));
4302         AddCodeLine ("tax");
4303         AddCodeLine ("tya");
4304
4305     } else {
4306
4307         /* Address not constant but in primary */
4308         if (CodeSizeFactor < 400) {
4309             /* This is too much code, so call strlen instead of inlining */
4310             AddCodeLine ("jsr _strlen");
4311         } else {
4312             /* Inline the function */
4313             AddCodeLine ("sta ptr1");
4314             AddCodeLine ("stx ptr1+1");
4315             AddCodeLine ("ldy #$FF");
4316             g_defcodelabel (label);
4317             AddCodeLine ("iny");
4318             AddCodeLine ("lda (ptr1),y");
4319             AddCodeLine ("bne %s", LocalLabelName (label));
4320             AddCodeLine ("tax");
4321             AddCodeLine ("tya");
4322         }
4323     }
4324 }
4325
4326
4327