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