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