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