]> git.sur5r.net Git - cc65/blob - src/cc65/codeent.c
Collect more info on zp registers
[cc65] / src / cc65 / codeent.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 codeent.c                                 */
4 /*                                                                           */
5 /*                            Code segment entry                             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2001     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 <stdlib.h>
37 #include <string.h>
38
39 /* common */
40 #include "chartype.h"
41 #include "check.h"
42 #include "xmalloc.h"
43 #include "xsprintf.h"
44
45 /* cc65 */
46 #include "codeinfo.h"
47 #include "error.h"
48 #include "global.h"
49 #include "codelab.h"
50 #include "opcodes.h"
51 #include "codeent.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Data                                    */
57 /*****************************************************************************/
58
59
60
61 /* Empty argument */
62 static char EmptyArg[] = "";
63
64
65
66 /*****************************************************************************/
67 /*                             Helper functions                              */
68 /*****************************************************************************/
69
70
71
72 static void FreeArg (char* Arg)
73 /* Free a code entry argument */
74 {
75     if (Arg != EmptyArg) {
76         xfree (Arg);
77     }
78 }
79
80
81
82 static char* GetArgCopy (const char* Arg)
83 /* Create an argument copy for assignment */
84 {
85     if (Arg && Arg[0] != '\0') {
86         /* Create a copy */
87         return xstrdup (Arg);
88     } else {
89         /* Use the empty argument string */
90         return EmptyArg;
91     }
92 }
93
94
95
96 static int NumArg (const char* Arg, unsigned long* Num)
97 /* If the given argument is numerical, convert it and return true. Otherwise
98  * set Num to zero and return false.
99  */
100 {
101     char* End;
102     unsigned long Val;
103
104     /* Determine the base */
105     int Base = 10;
106     if (*Arg == '$') {
107         ++Arg;
108         Base = 16;
109     } else if (*Arg == '%') {
110         ++Arg;
111         Base = 2;
112     }
113
114     /* Convert the value. strtol is not exactly what we want here, but it's
115      * cheap and may be replaced by something fancier later.
116      */
117     Val = strtoul (Arg, &End, Base);
118
119     /* Check if the conversion was successful */
120     if (*End != '\0') {
121
122         /* Could not convert */
123         *Num = 0;
124         return 0;
125
126     } else {
127
128         /* Conversion ok */
129         *Num = Val;
130         return 1;
131
132     }
133 }
134
135
136
137 static void SetUseChgInfo (CodeEntry* E, const OPCDesc* D)
138 /* Set the Use and Chg in E */
139 {
140     unsigned short Use;
141
142     /* If this is a subroutine call, or a jump to an external function,
143      * lookup the information about this function and use it. The jump itself
144      * does not change any registers, so we don't need to use the data from D.
145      */
146     if ((E->Info & (OF_BRA | OF_CALL)) != 0 && E->JumpTo == 0) {
147         /* A subroutine call or jump to external symbol (function exit) */
148         GetFuncInfo (E->Arg, &E->Use, &E->Chg);
149     } else {
150         /* Some other instruction. Use the values from the opcode description
151          * plus addressing mode info.
152          */
153         E->Use = D->Use | GetAMUseInfo (E->AM);
154         E->Chg = D->Chg;
155
156         /* Check for special zero page registers used */
157         switch (E->AM) {
158
159             case AM65_ZP:
160             case AM65_ABS:
161             /* Be conservative: */
162             case AM65_ZPX:
163             case AM65_ABSX:
164             case AM65_ABSY:
165                 if (IsZPName (E->Arg, &Use) && Use != REG_NONE) {
166                     if (E->OPC == OP65_ASL || E->OPC == OP65_DEC ||
167                         E->OPC == OP65_INC || E->OPC == OP65_LSR ||
168                         E->OPC == OP65_ROL || E->OPC == OP65_ROR ||
169                         E->OPC == OP65_TRB || E->OPC == OP65_TSB) {
170                         /* The zp loc is both, input and output */
171                         E->Chg |= Use;
172                         E->Use |= Use;
173                     } else if ((E->Info & OF_STORE) != 0) {
174                         /* Just output */
175                         E->Chg |= Use;
176                     } else {
177                         /* Input only */
178                         E->Use |= Use;
179                     }
180                 }
181                 break;
182
183             case AM65_ZPX_IND:
184             case AM65_ZP_INDY:
185             case AM65_ZP_IND:
186                 if (IsZPName (E->Arg, &Use) && Use != REG_NONE) {
187                     /* These addressing modes will never change the zp loc */
188                     E->Use |= Use;
189                 }
190                 break;
191
192             default:
193                 /* Keep gcc silent */
194                 break;
195         }
196     }
197 }
198
199
200
201 /*****************************************************************************/
202 /*                                   Code                                    */
203 /*****************************************************************************/
204
205
206
207 CodeEntry* NewCodeEntry (opc_t OPC, am_t AM, const char* Arg,
208                          CodeLabel* JumpTo, LineInfo* LI)
209 /* Create a new code entry, initialize and return it */
210 {
211     /* Get the opcode description */
212     const OPCDesc* D = GetOPCDesc (OPC);
213
214     /* Allocate memory */
215     CodeEntry* E = xmalloc (sizeof (CodeEntry));
216
217     /* Initialize the fields */
218     E->OPC    = D->OPC;
219     E->AM     = AM;
220     E->Arg    = GetArgCopy (Arg);
221     E->Flags  = NumArg (E->Arg, &E->Num)? CEF_NUMARG : 0;
222     E->Info   = D->Info;
223     E->Size   = GetInsnSize (E->OPC, E->AM);
224     E->JumpTo = JumpTo;
225     E->LI     = UseLineInfo (LI);
226     E->RI     = 0;
227     SetUseChgInfo (E, D);
228     InitCollection (&E->Labels);
229
230     /* If we have a label given, add this entry to the label */
231     if (JumpTo) {
232         CollAppend (&JumpTo->JumpFrom, E);
233     }
234
235     /* Return the initialized struct */
236     return E;
237 }
238
239
240
241 void FreeCodeEntry (CodeEntry* E)
242 /* Free the given code entry */
243 {
244     /* Free the string argument if we have one */
245     FreeArg (E->Arg);
246
247     /* Cleanup the collection */
248     DoneCollection (&E->Labels);
249
250     /* Release the line info */
251     ReleaseLineInfo (E->LI);
252
253     /* Delete the register info */
254     CE_FreeRegInfo (E);
255
256     /* Free the entry */
257     xfree (E);
258 }
259
260
261
262 void CE_ReplaceOPC (CodeEntry* E, opc_t OPC)
263 /* Replace the opcode of the instruction. This will also replace related info,
264  * Size, Use and Chg, but it will NOT update any arguments or labels.
265  */
266 {
267     /* Get the opcode descriptor */
268     const OPCDesc* D = GetOPCDesc (OPC);
269
270     /* Replace the opcode */
271     E->OPC  = OPC;
272     E->Info = D->Info;
273     E->Size = GetInsnSize (E->OPC, E->AM);
274     SetUseChgInfo (E, D);
275 }
276
277
278
279 int CodeEntriesAreEqual (const CodeEntry* E1, const CodeEntry* E2)
280 /* Check if both code entries are equal */
281 {
282     return E1->OPC == E2->OPC && E1->AM == E2->AM && strcmp (E1->Arg, E2->Arg) == 0;
283 }
284
285
286
287 void CE_AttachLabel (CodeEntry* E, CodeLabel* L)
288 /* Attach the label to the entry */
289 {
290     /* Add it to the entries label list */
291     CollAppend (&E->Labels, L);
292
293     /* Tell the label about it's owner */
294     L->Owner = E;
295 }
296
297
298
299 void CE_MoveLabel (CodeLabel* L, CodeEntry* E)
300 /* Move the code label L from it's former owner to the code entry E. */
301 {
302     /* Delete the label from the owner */
303     CollDeleteItem (&L->Owner->Labels, L);
304
305     /* Set the new owner */
306     CollAppend (&E->Labels, L);
307     L->Owner = E;
308 }
309
310
311
312 void CE_SetNumArg (CodeEntry* E, long Num)
313 /* Set a new numeric argument for the given code entry that must already
314  * have a numeric argument.
315  */
316 {
317     char Buf[16];
318
319     /* Check that the entry has a numerical argument */
320     CHECK (E->Flags & CEF_NUMARG);
321
322     /* Make the new argument string */
323     if (E->Size == 2) {
324         Num &= 0xFF;
325         xsprintf (Buf, sizeof (Buf), "$%02X", (unsigned) Num);
326     } else if (E->Size == 3) {
327         Num &= 0xFFFF;
328         xsprintf (Buf, sizeof (Buf), "$%04X", (unsigned) Num);
329     } else {
330         Internal ("Invalid instruction size in CE_SetNumArg");
331     }
332
333     /* Free the old argument */
334     FreeArg (E->Arg);
335
336     /* Assign the new one */
337     E->Arg = GetArgCopy (Buf);
338
339     /* Use the new numerical value */
340     E->Num = Num;
341 }
342
343
344
345 int CE_KnownImm (const CodeEntry* E)
346 /* Return true if the argument of E is a known immediate value */
347 {
348     return (E->AM == AM65_IMM && (E->Flags & CEF_NUMARG) != 0);
349 }
350
351
352
353 void CE_FreeRegInfo (CodeEntry* E)
354 /* Free an existing register info struct */
355 {
356     if (E->RI) {
357         FreeRegInfo (E->RI);
358         E->RI = 0;
359     }
360 }
361
362
363
364 void CE_GenRegInfo (CodeEntry* E, RegContents* InputRegs)
365 /* Generate register info for this instruction. If an old info exists, it is
366  * overwritten.
367  */
368 {
369     /* Pointers to the register contents */
370     RegContents* In;
371     RegContents* Out;
372
373     /* Function register usage */
374     unsigned short Use, Chg;
375
376     /* If we don't have a register info struct, allocate one. */
377     if (E->RI == 0) {
378         E->RI = NewRegInfo (InputRegs);
379     } else {
380         if (InputRegs) {
381             E->RI->In  = *InputRegs;
382         } else {
383             RC_Invalidate (&E->RI->In);
384         }
385         E->RI->Out2 = E->RI->Out = E->RI->In;
386     }
387
388     /* Get pointers to the register contents */
389     In  = &E->RI->In;
390     Out = &E->RI->Out;
391
392     /* Handle the different instructions */
393     switch (E->OPC) {
394
395         case OP65_ADC:
396             /* We don't know the value of the carry, so the result is
397              * always unknown.
398              */
399             Out->RegA = -1;
400             break;
401
402         case OP65_AND:
403             if (In->RegA >= 0) {
404                 if (CE_KnownImm (E)) {
405                     Out->RegA = In->RegA & (short) E->Num;
406                 } else {
407                     Out->RegA = -1;
408                 }
409             }
410             break;
411
412         case OP65_ASL:
413             if (E->AM == AM65_ACC && In->RegA >= 0) {
414                 Out->RegA = (In->RegA << 1) & 0xFF;
415             }
416             break;
417
418         case OP65_BCC:
419             break;
420
421         case OP65_BCS:
422             break;
423
424         case OP65_BEQ:
425             break;
426
427         case OP65_BIT:
428             break;
429
430         case OP65_BMI:
431             break;
432
433         case OP65_BNE:
434             break;
435
436         case OP65_BPL:
437             break;
438
439         case OP65_BRA:
440             break;
441
442         case OP65_BRK:
443             break;
444
445         case OP65_BVC:
446             break;
447
448         case OP65_BVS:
449             break;
450
451         case OP65_CLC:
452             break;
453
454         case OP65_CLD:
455             break;
456
457         case OP65_CLI:
458             break;
459
460         case OP65_CLV:
461             break;
462
463         case OP65_CMP:
464             break;
465
466         case OP65_CPX:
467             break;
468
469         case OP65_CPY:
470             break;
471
472         case OP65_DEA:
473             if (In->RegA >= 0) {
474                 Out->RegA = In->RegA - 1;
475             }
476             break;
477
478         case OP65_DEC:
479             if (E->AM == AM65_ACC && In->RegA >= 0) {
480                 Out->RegA = In->RegA - 1;
481             }
482             break;
483
484         case OP65_DEX:
485             if (In->RegX >= 0) {
486                 Out->RegX = In->RegX - 1;
487             }
488             break;
489
490         case OP65_DEY:
491             if (In->RegY >= 0) {
492                 Out->RegY = In->RegY - 1;
493             }
494             break;
495
496         case OP65_EOR:
497             if (In->RegA >= 0) {
498                 if (CE_KnownImm (E)) {
499                     Out->RegA = In->RegA ^ (short) E->Num;
500                 } else {
501                     Out->RegA = -1;
502                 }
503             }
504             break;
505
506         case OP65_INA:
507             if (In->RegA >= 0) {
508                 Out->RegA = In->RegA + 1;
509             }
510             break;
511
512         case OP65_INC:
513             if (E->AM == AM65_ACC && In->RegA >= 0) {
514                 Out->RegA = In->RegA + 1;
515             }
516             break;
517
518         case OP65_INX:
519             if (In->RegX >= 0) {
520                 Out->RegX = In->RegX + 1;
521             }
522             break;
523
524         case OP65_INY:
525             if (In->RegY >= 0) {
526                 Out->RegY = In->RegY + 1;
527             }
528             break;
529
530         case OP65_JCC:
531             break;
532
533         case OP65_JCS:
534             break;
535
536         case OP65_JEQ:
537             break;
538
539         case OP65_JMI:
540             break;
541
542         case OP65_JMP:
543             break;
544
545         case OP65_JNE:
546             break;
547
548         case OP65_JPL:
549             break;
550
551         case OP65_JSR:
552             /* Get the code info for the function */
553             GetFuncInfo (E->Arg, &Use, &Chg);
554             if (Chg & REG_A) {
555                 Out->RegA = -1;
556             }
557             if (Chg & REG_X) {
558                 Out->RegX = -1;
559             }
560             if (Chg & REG_Y) {
561                 Out->RegY = -1;
562             }
563             break;
564
565         case OP65_JVC:
566             break;
567
568         case OP65_JVS:
569             break;
570
571         case OP65_LDA:
572             if (CE_KnownImm (E)) {
573                 Out->RegA = (unsigned char) E->Num;
574             } else {
575                 /* A is now unknown */
576                 Out->RegA = -1;
577             }
578             break;
579
580         case OP65_LDX:
581             if (CE_KnownImm (E)) {
582                 Out->RegX = (unsigned char) E->Num;
583             } else {
584                 /* X is now unknown */
585                 Out->RegX = -1;
586             }
587             break;
588
589         case OP65_LDY:
590             if (CE_KnownImm (E)) {
591                 Out->RegY = (unsigned char) E->Num;
592             } else {
593                 /* Y is now unknown */
594                 Out->RegY = -1;
595             }
596             break;
597
598         case OP65_LSR:
599             if (E->AM == AM65_ACC && In->RegA >= 0) {
600                 Out->RegA = (In->RegA >> 1) & 0xFF;
601             }
602             break;
603
604         case OP65_NOP:
605             break;
606
607         case OP65_ORA:
608             if (In->RegA >= 0) {
609                 if (CE_KnownImm (E)) {
610                     Out->RegA = In->RegA | (short) E->Num;
611                 } else {
612                     /* A is now unknown */
613                     Out->RegA = -1;
614                 }
615             }
616             break;
617
618         case OP65_PHA:
619             break;
620
621         case OP65_PHP:
622             break;
623
624         case OP65_PHX:
625             break;
626
627         case OP65_PHY:
628             break;
629
630         case OP65_PLA:
631             Out->RegA = -1;
632             break;
633
634         case OP65_PLP:
635             break;
636
637         case OP65_PLX:
638             Out->RegX = -1;
639             break;
640
641         case OP65_PLY:
642             Out->RegY = -1;
643             break;
644
645         case OP65_ROL:
646             Out->RegA = -1;
647             break;
648
649         case OP65_ROR:
650             Out->RegA = -1;
651             break;
652
653         case OP65_RTI:
654             break;
655
656         case OP65_RTS:
657             break;
658
659         case OP65_SBC:
660             /* We don't know the value of the carry bit */
661             Out->RegA = -1;
662             break;
663
664         case OP65_SEC:
665             break;
666
667         case OP65_SED:
668             break;
669
670         case OP65_SEI:
671             break;
672
673         case OP65_STA:
674             break;
675
676         case OP65_STX:
677             break;
678
679         case OP65_STY:
680             break;
681
682         case OP65_TAX:
683             Out->RegX = In->RegA;
684             break;
685
686         case OP65_TAY:
687             Out->RegY = In->RegA;
688             break;
689
690         case OP65_TRB:
691             /* For now... */
692             Out->RegA = -1;
693             break;
694
695         case OP65_TSB:
696             /* For now... */
697             Out->RegA = -1;
698             break;
699
700         case OP65_TSX:
701             Out->RegX = -1;
702             break;
703
704         case OP65_TXA:
705             Out->RegA = In->RegX;
706             break;
707
708         case OP65_TXS:
709             break;
710
711         case OP65_TYA:
712             Out->RegA = In->RegY;
713             break;
714
715         default:
716             break;
717
718     }
719 }
720
721
722
723 static char* RegInfoDesc (unsigned U, char* Buf)
724 /* Return a string containing register info */
725 {
726     Buf[0] = '\0';
727
728     strcat (Buf, U & REG_SREG? "E" : "_");
729     strcat (Buf, U & REG_A?    "A" : "_");
730     strcat (Buf, U & REG_X?    "X" : "_");
731     strcat (Buf, U & REG_Y?    "Y" : "_");
732     strcat (Buf, U & REG_SP?   "S" : "_");
733     strcat (Buf, U & REG_TMP1? "T1" : "__");
734     strcat (Buf, U & REG_TMP2? "T2" : "__");
735     strcat (Buf, U & REG_TMP3? "T3" : "__");
736     strcat (Buf, U & REG_TMP4? "T4" : "__");
737     strcat (Buf, U & REG_PTR1? "1" : "_");
738     strcat (Buf, U & REG_PTR2? "2" : "_");
739     strcat (Buf, U & REG_PTR3? "3" : "_");
740     strcat (Buf, U & REG_PTR4? "4" : "_");
741     strcat (Buf, U & REG_SAVE? "V"  : "_");
742     strcat (Buf, U & REG_BANK? "B" : "_");
743
744     return Buf;
745 }
746
747
748
749 void CE_Output (const CodeEntry* E, FILE* F)
750 /* Output the code entry to a file */
751 {
752     const OPCDesc* D;
753     unsigned Chars;
754     const char* Target;
755
756     /* If we have a label, print that */
757     unsigned LabelCount = CollCount (&E->Labels);
758     unsigned I;
759     for (I = 0; I < LabelCount; ++I) {
760         CL_Output (CollConstAt (&E->Labels, I), F);
761     }
762
763     /* Get the opcode description */
764     D = GetOPCDesc (E->OPC);
765
766     /* Print the mnemonic */
767     Chars = fprintf (F, "\t%s", D->Mnemo);
768
769     /* Print the operand */
770     switch (E->AM) {
771
772         case AM_IMP:
773         case AM65_IMP:
774             /* implicit */
775             break;
776
777         case AM65_ACC:
778             /* accumulator */
779             Chars += fprintf (F, "%*sa", 9-Chars, "");
780             break;
781
782         case AM_IMM:
783         case AM65_IMM:
784             /* immidiate */
785             Chars += fprintf (F, "%*s#%s", 9-Chars, "", E->Arg);
786             break;
787
788         case AM_ABS:
789         case AM65_ZP:
790         case AM65_ABS:
791             /* zeropage and absolute */
792             Chars += fprintf (F, "%*s%s", 9-Chars, "", E->Arg);
793             break;
794
795         case AM65_ZPX:
796         case AM65_ABSX:
797             /* zeropage,X and absolute,X */
798             Chars += fprintf (F, "%*s%s,x", 9-Chars, "", E->Arg);
799             break;
800
801         case AM65_ABSY:
802             /* absolute,Y */
803             Chars += fprintf (F, "%*s%s,y", 9-Chars, "", E->Arg);
804             break;
805
806         case AM65_ZPX_IND:
807             /* (zeropage,x) */
808             Chars += fprintf (F, "%*s(%s,x)", 9-Chars, "", E->Arg);
809             break;
810
811         case AM65_ZP_INDY:
812             /* (zeropage),y */
813             Chars += fprintf (F, "%*s(%s),y", 9-Chars, "", E->Arg);
814             break;
815
816         case AM65_ZP_IND:
817             /* (zeropage) */
818             Chars += fprintf (F, "%*s(%s)", 9-Chars, "", E->Arg);
819             break;
820
821         case AM65_BRA:
822             /* branch */
823             Target = E->JumpTo? E->JumpTo->Name : E->Arg;
824             Chars += fprintf (F, "%*s%s", 9-Chars, "", Target);
825             break;
826
827         default:
828             Internal ("Invalid addressing mode");
829
830     }
831
832     /* Print usage info if requested by the debugging flag */
833     if (Debug) {
834         char Use [128];
835         char Chg [128];
836         fprintf (F,
837                  "%*s; USE: %-19s CHG: %-19s SIZE: %u\n",
838                  30-Chars, "",
839                  RegInfoDesc (E->Use, Use),
840                  RegInfoDesc (E->Chg, Chg),
841                  E->Size);
842     } else {
843         /* Terminate the line */
844         fprintf (F, "\n");
845     }
846 }
847
848
849
850
851
852