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