* src/pic16/device.h (struct pic16_options_t): added 'int CATregs' flag
[fw/sdcc] / src / pic16 / pcode.h
1 /*-------------------------------------------------------------------------
2
3    pcode.h - post code generation
4    Written By -  Scott Dattalo scott@dattalo.com
5    Ported to PIC16 By -  Martin Dubuc m.dubuc@rogers.com
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20    
21 -------------------------------------------------------------------------*/
22
23 //#include "ralloc.h"
24 struct regs;
25
26 /*
27    Post code generation
28
29    The post code generation is an assembler optimizer. The assembly code
30    produced by all of the previous steps is fully functional. This step
31    will attempt to analyze the flow of the assembly code and agressively 
32    optimize it. The peep hole optimizer attempts to do the same thing.
33    As you may recall, the peep hole optimizer replaces blocks of assembly
34    with more optimal blocks (e.g. removing redundant register loads).
35    However, the peep hole optimizer has to be somewhat conservative since
36    an assembly program has implicit state information that's unavailable 
37    when only a few instructions are examined.
38      Consider this example:
39
40    example1:
41      movwf  t1
42      movf   t1,w
43
44    The movf seems redundant since we know that the W register already
45    contains the same value of t1. So a peep hole optimizer is tempted to
46    remove the "movf". However, this is dangerous since the movf affects
47    the flags in the status register (specifically the Z flag) and subsequent
48    code may depend upon this. Look at these two examples:
49
50    example2:
51      movwf  t1
52      movf   t1,w     ; Can't remove this movf
53      skpz
54       return
55
56    example3:
57      movwf  t1
58      movf   t1,w     ; This  movf can be removed
59      xorwf  t2,w     ; since xorwf will over write Z 
60      skpz
61       return
62
63 */
64
65
66 #ifndef __PCODE_H__
67 #define __PCODE_H__
68
69 /***********************************************************************
70  * debug stuff
71  * 
72  * The DFPRINTF macro will call fprintf if PCODE_DEBUG is defined.
73  * The macro is used like:
74  *
75  * DPRINTF(("%s #%d\n","test", 1));
76  *
77  * The double parenthesis (()) are necessary
78  * 
79  ***********************************************************************/
80 //#define PCODE_DEBUG
81
82 #ifdef PCODE_DEBUG
83 #define DFPRINTF(args) (fprintf args)
84 #else
85 #define DFPRINTF(args) ;
86 #endif
87
88
89 #ifdef WORDS_BIGENDIAN
90   #define _ENDIAN(x)  (3-x)
91 #else
92   #define _ENDIAN(x)  (x)
93 #endif
94
95
96 #define BYTE_IN_LONG(x,b) ((x>>(8*_ENDIAN(b)))&0xff)
97
98
99 /***********************************************************************
100  * Extended Instruction Set/Indexed Literal Offset Mode                *
101  * Set this macro to enable code generation with the extended          *
102  * instruction set and the new Indexed Literal Offset Mode             *
103  ***********************************************************************/
104 #define XINST   1
105
106 /***********************************************************************
107  *  PIC status bits - this will move into device dependent headers
108  ***********************************************************************/
109 #define PIC_C_BIT    0
110 #define PIC_DC_BIT   1
111 #define PIC_Z_BIT    2
112 #define PIC_OV_BIT   3
113 #define PIC_N_BIT    4
114 #define PIC_IRP_BIT  7   /* Indirect register page select */
115
116 /***********************************************************************
117  *  PIC INTCON bits - this will move into device dependent headers
118  ***********************************************************************/
119 #define PIC_RBIF_BIT 0   /* Port B level has changed flag */
120 #define PIC_INTF_BIT 1   /* Port B bit 0 interrupt on edge flag */
121 #define PIC_T0IF_BIT 2   /* TMR0 has overflowed flag */
122 #define PIC_RBIE_BIT 3   /* Port B level has changed - Interrupt Enable */
123 #define PIC_INTE_BIT 4   /* Port B bit 0 interrupt on edge - Int Enable */
124 #define PIC_T0IE_BIT 5   /* TMR0 overflow Interrupt Enable */
125 #define PIC_PIE_BIT  6   /* Peripheral Interrupt Enable */
126 #define PIC_GIE_BIT  7   /* Global Interrupt Enable */
127
128 /***********************************************************************
129  *  PIC bank definitions
130  ***********************************************************************/
131 #define PIC_BANK_FIRST 0
132 #define PIC_BANK_LAST  0xf
133
134
135 /***********************************************************************
136  *  Operand types 
137  ***********************************************************************/
138 #define POT_RESULT  0
139 #define POT_LEFT    1
140 #define POT_RIGHT   2
141
142
143 /***********************************************************************
144  *
145  *  PIC_OPTYPE - Operand types that are specific to the PIC architecture
146  *
147  *  If a PIC assembly instruction has an operand then here is where we
148  *  associate a type to it. For example,
149  *
150  *     movf    reg,W
151  *
152  *  The movf has two operands: 'reg' and the W register. 'reg' is some
153  *  arbitrary general purpose register, hence it has the type PO_GPR_REGISTER.
154  *  The W register, which is the PIC's accumulator, has the type PO_W.
155  *
156  ***********************************************************************/
157
158
159
160 typedef enum 
161 {
162   PO_NONE=0,         // No operand e.g. NOP
163   PO_W,              // The working register (as a destination)
164   PO_WREG,           // The working register (as a file register)
165   PO_STATUS,         // The 'STATUS' register
166   PO_BSR,            // The 'BSR' register
167   PO_FSR0,           // The "file select register" (in PIC18 family it's one 
168                      // of three)
169   PO_INDF0,          // The Indirect register
170   PO_INTCON,         // Interrupt Control register
171   PO_GPR_REGISTER,   // A general purpose register
172   PO_GPR_BIT,        // A bit of a general purpose register
173   PO_GPR_TEMP,       // A general purpose temporary register
174   PO_SFR_REGISTER,   // A special function register (e.g. PORTA)
175   PO_PCL,            // Program counter Low register
176   PO_PCLATH,         // Program counter Latch high register
177   PO_PCLATU,         // Program counter Latch upper register
178   PO_PRODL,          // Product Register Low
179   PO_PRODH,          // Product Register High
180   PO_LITERAL,        // A constant
181   PO_REL_ADDR,       // A relative address
182   PO_IMMEDIATE,      //  (8051 legacy)
183   PO_DIR,            // Direct memory (8051 legacy)
184   PO_CRY,            // bit memory (8051 legacy)
185   PO_BIT,            // bit operand.
186   PO_STR,            //  (8051 legacy)
187   PO_LABEL,
188   PO_WILD            // Wild card operand in peep optimizer
189 } PIC_OPTYPE;
190
191
192 /***********************************************************************
193  *
194  *  PIC_OPCODE
195  *
196  *  This is not a list of the PIC's opcodes per se, but instead
197  *  an enumeration of all of the different types of pic opcodes. 
198  *
199  ***********************************************************************/
200
201 typedef enum
202 {
203   POC_WILD=-1,   /* Wild card - used in the pCode peep hole optimizer
204                   * to represent ANY pic opcode */
205   POC_ADDLW=0,
206   POC_ADDWF,
207   POC_ADDFW,
208   POC_ADDFWC,
209   POC_ADDWFC,
210   POC_ANDLW,
211   POC_ANDWF,
212   POC_ANDFW,
213   POC_BC,
214   POC_BCF,
215   POC_BN,
216   POC_BNC,
217   POC_BNN,
218   POC_BNOV,
219   POC_BNZ,
220   POC_BOV,
221   POC_BRA,
222   POC_BSF,
223   POC_BTFSC,
224   POC_BTFSS,
225   POC_BTG,
226   POC_BZ,
227   POC_CALL,
228   POC_CLRF,
229   POC_CLRWDT,
230   POC_COMF,
231   POC_COMFW,
232   POC_CPFSEQ,
233   POC_CPFSGT,
234   POC_CPFSLT,
235   POC_DAW,
236   POC_DCFSNZ,
237   POC_DCFSNZW,
238   POC_DECF,
239   POC_DECFW,
240   POC_DECFSZ,
241   POC_DECFSZW,
242   POC_GOTO,
243   POC_INCF,
244   POC_INCFW,
245   POC_INCFSZ,
246   POC_INCFSZW,
247   POC_INFSNZ,
248   POC_INFSNZW,
249   POC_IORWF,
250   POC_IORFW,
251   POC_IORLW,
252   POC_LFSR,
253   POC_MOVF,
254   POC_MOVFW,
255   POC_MOVFF,
256   POC_MOVLB,
257   POC_MOVLW,
258   POC_MOVWF,
259   POC_MULLW,
260   POC_MULWF,
261   POC_NEGF,
262   POC_NOP,
263   POC_POP,
264   POC_PUSH,
265   POC_RCALL,
266   POC_RETFIE,
267   POC_RETLW,
268   POC_RETURN,
269   POC_RLCF,
270   POC_RLCFW,
271   POC_RLNCF,
272   POC_RLNCFW,
273   POC_RRCF,
274   POC_RRCFW,
275   POC_RRNCF,
276   POC_RRNCFW,
277   POC_SETF,
278   POC_SUBLW,
279   POC_SUBFWB,
280   POC_SUBWF,
281   POC_SUBFW,
282   POC_SUBWFB_D0,
283   POC_SUBWFB_D1,
284   POC_SUBFWB_D0,
285   POC_SUBFWB_D1,
286   POC_SWAPF,
287   POC_SWAPFW,
288   POC_TBLRD,
289   POC_TBLRD_POSTINC,
290   POC_TBLRD_POSTDEC,
291   POC_TBLRD_PREINC,
292   POC_TBLWT,
293   POC_TBLWT_POSTINC,
294   POC_TBLWT_POSTDEC,
295   POC_TBLWT_PREINC,
296   POC_TSTFSZ,
297   POC_XORLW,
298   POC_XORWF,
299   POC_XORFW,
300
301   POC_BANKSEL
302 } PIC_OPCODE;
303
304
305 /***********************************************************************
306  *  PC_TYPE  - pCode Types
307  ***********************************************************************/
308
309 typedef enum
310 {
311   PC_COMMENT=0,   /* pCode is a comment     */
312   PC_INLINE,      /* user's inline code     */
313   PC_OPCODE,      /* PORT dependent opcode  */
314   PC_LABEL,       /* assembly label         */
315   PC_FLOW,        /* flow analysis          */
316   PC_FUNCTION,    /* Function start or end  */
317   PC_WILD,        /* wildcard - an opcode place holder used 
318                    * in the pCode peep hole optimizer */
319   PC_CSOURCE,     /* C-Source Line  */
320   PC_ASMDIR,      /* Assembler directive */
321   PC_BAD,         /* Mark the pCode object as being bad */
322   PC_INFO         /* pCode information node, used primarily in optimizing */
323 } PC_TYPE;
324
325
326 /***********************************************************************
327  *  INFO_TYPE  - information node types
328  ***********************************************************************/
329
330 typedef enum
331 {
332   INF_OPTIMIZATION,      /* structure contains optimization information */
333   INF_LOCALREGS          /* structure contains local register information */
334 } INFO_TYPE;
335
336
337
338 /***********************************************************************
339  *  OPT_TYPE  - optimization node types
340  ***********************************************************************/
341
342 typedef enum
343 {
344   OPT_BEGIN,             /* mark beginning of optimization block */
345   OPT_END,               /* mark ending of optimization block */
346   OPT_JUMPTABLE_BEGIN,   /* mark beginning of a jumptable */
347   OPT_JUMPTABLE_END      /* mark end of jumptable */
348 } OPT_TYPE;
349
350 /***********************************************************************
351  *  LR_TYPE  - optimization node types
352  ***********************************************************************/
353
354 typedef enum
355 {
356   LR_ENTRY_BEGIN,             /* mark beginning of optimization block */
357   LR_ENTRY_END,               /* mark ending of optimization block */
358   LR_EXIT_BEGIN,
359   LR_EXIT_END
360 } LR_TYPE;
361
362
363 /************************************************/
364 /***************  Structures ********************/
365 /************************************************/
366 /* These are here as forward references - the 
367  * full definition of these are below           */
368 struct pCode;
369 struct pCodeWildBlock;
370 struct pCodeRegLives;
371
372 /*************************************************
373   pBranch
374
375   The first step in optimizing pCode is determining
376  the program flow. This information is stored in
377  single-linked lists in the for of 'from' and 'to'
378  objects with in a pcode. For example, most instructions
379  don't involve any branching. So their from branch
380  points to the pCode immediately preceding them and
381  their 'to' branch points to the pcode immediately
382  following them. A skip instruction is an example of
383  a pcode that has multiple (in this case two) elements
384  in the 'to' branch. A 'label' pcode is an where there
385  may be multiple 'from' branches.
386  *************************************************/
387
388 typedef struct pBranch
389 {
390   struct pCode   *pc;    // Next pCode in a branch
391   struct pBranch *next;  /* If more than one branch
392                           * the next one is here */
393
394 } pBranch;
395
396 /*************************************************
397   pCodeOp
398
399   pCode Operand structure.
400   For those assembly instructions that have arguments, 
401   the pCode will have a pCodeOp in which the argument
402   can be stored. For example
403
404     movf   some_register,w
405
406   'some_register' will be stored/referenced in a pCodeOp
407
408  *************************************************/
409
410 typedef struct pCodeOp
411 {
412   PIC_OPTYPE type;
413   char *name;
414   
415 } pCodeOp;
416
417 #if 0
418 typedef struct pCodeOpBit
419 {
420   pCodeOp pcop;
421   int bit;
422   unsigned int inBitSpace: 1; /* True if in bit space, else
423                                  just a bit of a register */
424 } pCodeOpBit;
425 #endif
426
427 typedef struct pCodeOpLit
428 {
429   pCodeOp pcop;
430   int lit;
431   pCodeOp *arg2;        /* needed as pCodeOpLit and pCodeOpLit2 are not separable via their type (PO_LITERAL) */
432 } pCodeOpLit;
433
434 typedef struct pCodeOpLit2
435 {
436   pCodeOp pcop;
437   int lit;
438   pCodeOp *arg2;
439 } pCodeOpLit2;
440
441
442 typedef struct pCodeOpImmd
443 {
444   pCodeOp pcop;
445   int offset;           /* low,high or upper byte of immediate value */
446   int index;            /* add this to the immediate value */
447   unsigned _const:1;    /* is in code space    */
448
449   int rIdx;             /* If this immd points to a register */
450   struct regs *r;       /* then this is the reg. */
451
452 } pCodeOpImmd;
453
454 typedef struct pCodeOpLabel
455 {
456   pCodeOp pcop;
457   int key;
458 } pCodeOpLabel;
459
460 typedef struct pCodeOpReg
461 {
462   pCodeOp pcop;    // Can be either GPR or SFR
463   int rIdx;        // Index into the register table
464   struct regs *r;
465   int instance;    // byte # of Multi-byte registers
466   struct pBlock *pb;
467
468   pCodeOp *pcop2;       // second memory operand (NEEDED IN gen.c:pic16_popGet2p (pCodeOpReg casted into pCodeOpReg2) 
469 } pCodeOpReg;
470
471 typedef struct pCodeOpReg2
472 {
473   pCodeOp pcop;         // used by default to all references
474   int rIdx;
475   struct regs *r;
476   int instance;         // assume same instance for both operands
477   struct pBlock *pb;
478
479   pCodeOp *pcop2;       // second memory operand
480 } pCodeOpReg2;
481
482 typedef struct pCodeOpRegBit
483 {
484   pCodeOpReg  pcor;       // The Register containing this bit
485   int bit;                // 0-7 bit number.
486   PIC_OPTYPE subtype;     // The type of this register.
487   unsigned int inBitSpace: 1; /* True if in bit space, else
488                                  just a bit of a register */
489 } pCodeOpRegBit;
490
491
492 typedef struct pCodeOpWild
493 {
494   pCodeOp pcop;
495
496   struct pCodeWildBlock *pcwb;
497
498   int id;                 /* index into an array of char *'s that will match
499                            * the wild card. The array is in *pcp. */
500   pCodeOp *subtype;       /* Pointer to the Operand type into which this wild
501                            * card will be expanded */
502   pCodeOp *matched;       /* When a wild matches, we'll store a pointer to the
503                            * opcode we matched */
504
505   pCodeOp *pcop2;         /* second operand if exists */
506
507 } pCodeOpWild;
508
509
510 typedef struct pCodeOpOpt
511 {
512   pCodeOp pcop;
513   
514   OPT_TYPE type;          /* optimization node type */
515   
516   char *key;              /* key by which a block is identified */
517 } pCodeOpOpt;
518
519 typedef struct pCodeOpLocalReg
520 {
521   pCodeOp pcop;
522
523   LR_TYPE type;
524 } pCodeOpLocalReg;  
525
526 /*************************************************
527     pCode
528
529     Here is the basic build block of a PIC instruction.
530     Each pic instruction will get allocated a pCode.
531     A linked list of pCodes makes a program.
532
533 **************************************************/
534
535 typedef struct pCode
536 {
537   PC_TYPE    type;
538
539   struct pCode *prev;  // The pCode objects are linked together
540   struct pCode *next;  // in doubly linked lists.
541
542   int seq;             // sequence number
543
544   struct pBlock *pb;   // The pBlock that contains this pCode.
545
546   /* "virtual functions"
547    *  The pCode structure is like a base class
548    * in C++. The subsequent structures that "inherit"
549    * the pCode structure will initialize these function
550    * pointers to something useful */
551   //  void (*analyze) (struct pCode *_this);
552   void (*destruct)(struct pCode *_this);
553   void (*print)  (FILE *of,struct pCode *_this);
554
555 } pCode;
556
557
558 /*************************************************
559     pCodeComment
560 **************************************************/
561
562 typedef struct pCodeComment
563 {
564
565   pCode  pc;
566
567   char *comment;
568
569 } pCodeComment;
570
571
572 /*************************************************
573     pCodeCSource
574 **************************************************/
575
576 typedef struct pCodeCSource
577 {
578
579   pCode  pc;
580
581   int  line_number;
582   char *line;
583   char *file_name;
584
585 } pCodeCSource;
586
587
588 /*************************************************
589     pCodeAsmDir
590 **************************************************/
591
592 /*************************************************
593     pCodeFlow
594
595   The Flow object is used as marker to separate 
596  the assembly code into contiguous chunks. In other
597  words, everytime an instruction cause or potentially
598  causes a branch, a Flow object will be inserted into
599  the pCode chain to mark the beginning of the next
600  contiguous chunk.
601
602 **************************************************/
603 struct defmap_s; // defined in pcode.c
604
605 typedef struct pCodeFlow
606 {
607
608   pCode  pc;
609
610   pCode *end;   /* Last pCode in this flow. Note that
611                    the first pCode is pc.next */
612
613   /*  set **uses;   * map the pCode instruction inCond and outCond conditions 
614                  * in this array of set's. The reason we allocate an 
615                  * array of pointers instead of declaring each type of 
616                  * usage is because there are port dependent usage definitions */
617   //int nuses;    /* number of uses sets */
618
619   set *from;    /* flow blocks that can send control to this flow block */
620   set *to;      /* flow blocks to which this one can send control */
621   struct pCodeFlow *ancestor; /* The most immediate "single" pCodeFlow object that
622                                * executes prior to this one. In many cases, this 
623                                * will be just the previous */
624
625   int inCond;   /* Input conditions - stuff assumed defined at entry */
626   int outCond;  /* Output conditions - stuff modified by flow block */
627
628   int firstBank; /* The first and last bank flags are the first and last */
629   int lastBank;  /* register banks used within one flow object */
630
631   int FromConflicts;
632   int ToConflicts;
633
634   set *registers;/* Registers used in this flow */
635
636   struct defmap_s *defmap;      /* chronologically ordered list of definitions performed
637                            in this flow (most recent at the front) */
638   struct defmap_s *in_vals;     /* definitions of all symbols reaching this flow
639                                  * symbols with multiple different definitions are stored
640                                  * with an assigned value of 0. */
641   struct defmap_s *out_vals;    /* definitions valid AFTER thie flow */
642
643 } pCodeFlow;
644
645 /*************************************************
646   pCodeFlowLink
647
648   The Flow Link object is used to record information
649  about how consecutive excutive Flow objects are related.
650  The pCodeFlow objects demarcate the pCodeInstructions
651  into contiguous chunks. The FlowLink records conflicts
652  in the discontinuities. For example, if one Flow object
653  references a register in bank 0 and the next Flow object
654  references a register in bank 1, then there is a discontinuity
655  in the banking registers.
656
657 */
658 typedef struct pCodeFlowLink
659 {
660   pCodeFlow  *pcflow;   /* pointer to linked pCodeFlow object */
661
662   int bank_conflict;    /* records bank conflicts */
663
664 } pCodeFlowLink;
665
666 /*************************************************
667     pCodeInstruction
668
669     Here we describe all the facets of a PIC instruction
670     (expansion for the 18cxxx is also provided).
671
672 **************************************************/
673
674 typedef struct pCodeInstruction
675 {
676
677   pCode  pc;
678
679   PIC_OPCODE op;        // The opcode of the instruction.
680
681   char const * const mnemonic;       // Pointer to mnemonic string
682
683   char isize;          // pCode instruction size
684
685   pBranch *from;       // pCodes that execute before this one
686   pBranch *to;         // pCodes that execute after
687   pBranch *label;      // pCode instructions that have labels
688
689   pCodeOp *pcop;               /* Operand, if this instruction has one */
690   pCodeFlow *pcflow;           /* flow block to which this instruction belongs */
691   pCodeCSource *cline;         /* C Source from which this instruction was derived */
692
693   unsigned int num_ops;        /* Number of operands (0,1,2 for mid range pics) */
694   unsigned int isModReg:  1;   /* If destination is W or F, then 1==F */
695   unsigned int isBitInst: 1;   /* e.g. BCF */
696   unsigned int isBranch:  1;   /* True if this is a branching instruction */
697   unsigned int isSkip:    1;   /* True if this is a skip instruction */
698   unsigned int isLit:     1;   /* True if this instruction has an literal operand */
699   unsigned int isAccess:   1;   /* True if this instruction has an access RAM operand */
700   unsigned int isFastCall: 1;   /* True if this instruction has a fast call/return mode select operand */
701   unsigned int is2MemOp: 1;     /* True is second operand is a memory operand VR - support for MOVFF */
702   unsigned int is2LitOp: 1;     /* True if instruction takes 2 literal operands VR - support for LFSR */
703
704   PIC_OPCODE inverted_op;      /* Opcode of instruction that's the opposite of this one */
705   unsigned int inCond;   // Input conditions for this instruction
706   unsigned int outCond;  // Output conditions for this instruction
707
708 #define PCI_MAGIC       0x6e12
709   unsigned int pci_magic;       // sanity check for pci initialization
710 } pCodeInstruction;
711
712
713
714 /*************************************************
715     pCodeAsmDir
716 **************************************************/
717
718 typedef struct pCodeAsmDir
719 {
720   pCodeInstruction pci;
721   
722   char *directive;
723   char *arg;
724 } pCodeAsmDir;
725
726
727 /*************************************************
728     pCodeLabel
729 **************************************************/
730
731 typedef struct pCodeLabel
732 {
733
734   pCode  pc;
735
736   char *label;
737   int key;
738   int force;            /* label cannot be optimized out */
739
740 } pCodeLabel;
741
742 /*************************************************
743     pCodeFunction
744 **************************************************/
745
746 typedef struct pCodeFunction
747 {
748
749   pCode  pc;
750
751   char *modname;
752   char *fname;     /* If NULL, then this is the end of
753                       a function. Otherwise, it's the
754                       start and the name is contained
755                       here */
756
757   pBranch *from;       // pCodes that execute before this one
758   pBranch *to;         // pCodes that execute after
759   pBranch *label;      // pCode instructions that have labels
760
761   int  ncalled;    /* Number of times function is called */
762
763   int absblock;    /* hack to emulate a block pCodes in absolute position
764                       but not inside a function */
765   int stackusage;  /* stack positions used in function */
766   
767 } pCodeFunction;
768
769
770 /*************************************************
771     pCodeWild
772 **************************************************/
773
774 typedef struct pCodeWild
775 {
776
777   pCodeInstruction  pci;
778
779   int    id;     /* Index into the wild card array of a peepBlock 
780                   * - this wild card will get expanded into that pCode
781                   *   that is stored at this index */
782
783   /* Conditions on wild pcode instruction */
784   int    mustBeBitSkipInst:1;
785   int    mustNotBeBitSkipInst:1;
786   int    invertBitSkipInst:1;
787
788   pCodeOp *operand;  // Optional operand
789   pCodeOp *label;    // Optional label
790
791 } pCodeWild;
792
793
794 /*************************************************
795     pInfo
796     
797     Here are stored generic informaton
798 *************************************************/
799 typedef struct pCodeInfo
800 {
801   pCodeInstruction pci;
802   
803   INFO_TYPE type;       /* info node type */
804   
805   pCodeOp *oper1;       /* info node arguments */
806 } pCodeInfo;
807   
808
809 /*************************************************
810     pBlock
811
812     Here are PIC program snippets. There's a strong
813     correlation between the eBBlocks and pBlocks.
814     SDCC subdivides a C program into managable chunks.
815     Each chunk becomes a eBBlock and ultimately in the
816     PIC port a pBlock.
817
818 **************************************************/
819
820 typedef struct pBlock
821 {
822   memmap *cmemmap;   /* The snippet is from this memmap */
823   char   dbName;     /* if cmemmap is NULL, then dbName will identify the block */
824   pCode *pcHead;     /* A pointer to the first pCode in a link list of pCodes */
825   pCode *pcTail;     /* A pointer to the last pCode in a link list of pCodes */
826
827   struct pBlock *next;      /* The pBlocks will form a doubly linked list */
828   struct pBlock *prev;
829
830   set *function_entries;    /* dll of functions in this pblock */
831   set *function_exits;
832   set *function_calls;
833   set *tregisters;
834
835   set *FlowTree;
836   unsigned visited:1;       /* set true if traversed in call tree */
837
838   unsigned seq;             /* sequence number of this pBlock */
839
840 } pBlock;
841
842 /*************************************************
843     pFile
844
845     The collection of pBlock program snippets are
846     placed into a linked list that is implemented
847     in the pFile structure.
848
849     The pcode optimizer will parse the pFile.
850
851 **************************************************/
852
853 typedef struct pFile
854 {
855   pBlock *pbHead;     /* A pointer to the first pBlock */
856   pBlock *pbTail;     /* A pointer to the last pBlock */
857
858   pBranch *functions; /* A SLL of functions in this pFile */
859
860 } pFile;
861
862
863
864 /*************************************************
865   pCodeWildBlock
866
867   The pCodeWildBlock object keeps track of the wild
868   variables, operands, and opcodes that exist in
869   a pBlock.
870 **************************************************/
871 typedef struct pCodeWildBlock {
872   pBlock    *pb;
873   struct pCodePeep *pcp;    // pointer back to ... I don't like this...
874
875   int       nvars;          // Number of wildcard registers in target.
876   char    **vars;           // array of pointers to them
877
878   int       nops;           // Number of wildcard operands in target.
879   pCodeOp **wildpCodeOps;   // array of pointers to the pCodeOp's.
880
881   int       nwildpCodes;    // Number of wildcard pCodes in target/replace
882   pCode   **wildpCodes;     // array of pointers to the pCode's.
883
884 } pCodeWildBlock;
885
886 /*************************************************
887   pCodePeep
888
889   The pCodePeep object mimics the peep hole optimizer
890   in the main SDCC src (e.g. SDCCpeeph.c). Essentially
891   there is a target pCode chain and a replacement
892   pCode chain. The target chain is compared to the
893   pCode that is generated by gen.c. If a match is
894   found then the pCode is replaced by the replacement
895   pCode chain.
896 **************************************************/
897 typedef struct pCodePeep {
898   pCodeWildBlock target;     // code we'd like to optimize
899   pCodeWildBlock replace;    // and this is what we'll optimize it with.
900
901   //pBlock *target;
902   //pBlock replace;            // and this is what we'll optimize it with.
903
904
905
906   /* (Note: a wildcard register is a place holder. Any register
907    * can be replaced by the wildcard when the pcode is being 
908    * compared to the target. */
909
910   /* Post Conditions. A post condition is a condition that
911    * must be either true or false before the peep rule is
912    * accepted. For example, a certain rule may be accepted
913    * if and only if the Z-bit is not used as an input to 
914    * the subsequent instructions in a pCode chain.
915    */
916   unsigned int postFalseCond;  
917   unsigned int postTrueCond;
918
919 } pCodePeep;
920
921 /*************************************************
922
923   pCode peep command definitions 
924
925  Here are some special commands that control the
926 way the peep hole optimizer behaves
927
928 **************************************************/
929
930 enum peepCommandTypes{
931   NOTBITSKIP = 0,
932   BITSKIP,
933   INVERTBITSKIP,
934   _LAST_PEEP_COMMAND_
935 };
936
937 /*************************************************
938     peepCommand structure stores the peep commands.
939
940 **************************************************/
941
942 typedef struct peepCommand {
943   int id;
944   char *cmd;
945 } peepCommand;
946
947 /*************************************************
948     pCode Macros
949
950 **************************************************/
951 #define PCODE(x)  ((pCode *)(x))
952 #define PCI(x)    ((pCodeInstruction *)(x))
953 #define PCL(x)    ((pCodeLabel *)(x))
954 #define PCF(x)    ((pCodeFunction *)(x))
955 #define PCFL(x)   ((pCodeFlow *)(x))
956 #define PCFLINK(x)((pCodeFlowLink *)(x))
957 #define PCW(x)    ((pCodeWild *)(x))
958 #define PCCS(x)   ((pCodeCSource *)(x))
959 #define PCAD(x)   ((pCodeAsmDir *)(x))
960 #define PCINF(x)  ((pCodeInfo *)(x))
961
962 #define PCOP(x)   ((pCodeOp *)(x))
963 //#define PCOB(x)   ((pCodeOpBit *)(x))
964 #define PCOL(x)   ((pCodeOpLit *)(x))
965 #define PCOI(x)   ((pCodeOpImmd *)(x))
966 #define PCOLAB(x) ((pCodeOpLabel *)(x))
967 #define PCOR(x)   ((pCodeOpReg *)(x))
968 #define PCOR2(x)  ((pCodeOpReg2 *)(x))
969 #define PCORB(x)  ((pCodeOpRegBit *)(x))
970 #define PCOO(x)   ((pCodeOpOpt *)(x))
971 #define PCOLR(x)  ((pCodeOpLocalReg *)(x))
972 #define PCOW(x)   ((pCodeOpWild *)(x))
973 #define PCOW2(x)  (PCOW(PCOW(x)->pcop2))
974 #define PBR(x)    ((pBranch *)(x))
975
976 #define PCWB(x)   ((pCodeWildBlock *)(x))
977
978
979 /*
980   macros for checking pCode types
981 */
982 #define isPCI(x)        ((PCODE(x)->type == PC_OPCODE))
983 #define isPCI_BRANCH(x) ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isBranch)
984 #define isPCI_SKIP(x)   ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isSkip)
985 #define isPCI_LIT(x)    ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isLit)
986 #define isPCI_BITSKIP(x)((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isSkip && PCI(x)->isBitInst)
987 #define isPCFL(x)       ((PCODE(x)->type == PC_FLOW))
988 #define isPCF(x)        ((PCODE(x)->type == PC_FUNCTION))
989 #define isPCL(x)        ((PCODE(x)->type == PC_LABEL))
990 #define isPCW(x)        ((PCODE(x)->type == PC_WILD))
991 #define isPCCS(x)       ((PCODE(x)->type == PC_CSOURCE))
992 #define isPCAD(x)       ((PCODE(x)->type == PC_ASMDIR))
993 #define isPCINFO(x)     ((PCODE(x)->type == PC_INFO))
994
995 #define isCALL(x)       ((isPCI(x)) && (PCI(x)->op == POC_CALL))
996 #define isSTATUS_REG(r) ((r)->pc_type == PO_STATUS)
997 #define isBSR_REG(r)    ((r)->pc_type == PO_BSR)
998 #define isACCESS_BANK(r)        (r->accessBank)
999
1000
1001
1002 #define isPCOLAB(x)     ((PCOP(x)->type) == PO_LABEL)
1003
1004 /*-----------------------------------------------------------------*
1005  * pCode functions.
1006  *-----------------------------------------------------------------*/
1007
1008 pCode *pic16_newpCode (PIC_OPCODE op, pCodeOp *pcop); // Create a new pCode given an operand
1009 pCode *pic16_newpCodeCharP(char *cP);              // Create a new pCode given a char *
1010 pCode *pic16_newpCodeInlineP(char *cP);            // Create a new pCode given a char *
1011 pCode *pic16_newpCodeFunction(char *g, char *f);   // Create a new function
1012 pCode *pic16_newpCodeLabel(char *name,int key);    // Create a new label given a key
1013 pCode *pic16_newpCodeLabelFORCE(char *name, int key); // Same as newpCodeLabel but label cannot be optimized out
1014 pCode *pic16_newpCodeCSource(int ln, char *f, char *l); // Create a new symbol line 
1015 pBlock *pic16_newpCodeChain(memmap *cm,char c, pCode *pc); // Create a new pBlock
1016 void pic16_printpBlock(FILE *of, pBlock *pb);      // Write a pBlock to a file
1017 void pic16_addpCode2pBlock(pBlock *pb, pCode *pc); // Add a pCode to a pBlock
1018 void pic16_addpBlock(pBlock *pb);                  // Add a pBlock to a pFile
1019 void pic16_copypCode(FILE *of, char dbName);       // Write all pBlocks with dbName to *of
1020 void pic16_movepBlock2Head(char dbName);           // move pBlocks around
1021 void pic16_AnalyzepCode(char dbName);
1022 void pic16_OptimizeLocalRegs(void);
1023 void pic16_AssignRegBanks(void);
1024 void pic16_printCallTree(FILE *of);
1025 void pCodePeepInit(void);
1026 void pic16_pBlockConvert2ISR(pBlock *pb);
1027 void pic16_pBlockConvert2Absolute(pBlock *pb);
1028 void pic16_initDB(void);
1029 void pic16_emitDB(char c, char ptype, void *p);           // Add DB directives to a pBlock
1030 void pic16_emitDS(char *s, char ptype, void *p);
1031 void pic16_flushDB(char ptype, void *p);                          // Add pending DB data to a pBlock
1032
1033 pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...); 
1034
1035 pCodeOp *pic16_newpCodeOpLabel(char *name, int key);
1036 pCodeOp *pic16_newpCodeOpImmd(char *name, int offset, int index, int code_space);
1037 pCodeOp *pic16_newpCodeOpLit(int lit);
1038 pCodeOp *pic16_newpCodeOpLit2(int lit, pCodeOp *arg2);
1039 pCodeOp *pic16_newpCodeOpBit(char *name, int bit,int inBitSpace, PIC_OPTYPE subt);
1040 pCodeOp *pic16_newpCodeOpBit_simple (struct asmop *op, int offs, int bit);
1041 pCodeOp *pic16_newpCodeOpRegFromStr(char *name);
1042 pCodeOp *pic16_newpCodeOpReg(int rIdx);
1043 pCodeOp *pic16_newpCodeOp(char *name, PIC_OPTYPE p);
1044 pCodeOp *pic16_newpCodeOpRegNotVect(bitVect *bv);
1045 pCodeOp *pic16_pCodeOpCopy(pCodeOp *pcop);
1046
1047 pCode *pic16_newpCodeInfo(INFO_TYPE type, pCodeOp *pcop);
1048 pCodeOp *pic16_newpCodeOpOpt(OPT_TYPE type, char *key);
1049 pCodeOp *pic16_newpCodeOpLocalRegs(LR_TYPE type);
1050 pCodeOp *pic16_newpCodeOpReg(int rIdx);
1051
1052 pCode * pic16_findNextInstruction(pCode *pci);
1053 pCode * pic16_findNextpCode(pCode *pc, PC_TYPE pct);
1054 int pic16_isPCinFlow(pCode *pc, pCode *pcflow);
1055 struct regs * pic16_getRegFromInstruction(pCode *pc);
1056 struct regs * pic16_getRegFromInstruction2(pCode *pc);
1057 char *pic16_get_op(pCodeOp *pcop,char *buffer, size_t size);
1058 char *pic16_get_op2(pCodeOp *pcop,char *buffer, size_t size);
1059 char *dumpPicOptype(PIC_OPTYPE type);
1060
1061 extern void pic16_pcode_test(void);
1062 extern int pic16_debug_verbose;
1063 extern int pic16_pcode_verbose;
1064
1065 extern char *LR_TYPE_STR[];
1066
1067
1068 #ifndef debugf
1069 //#define debugf(frm, rest...)       _debugf(__FILE__, __LINE__, frm, rest)
1070 #define debugf(frm, rest)       _debugf(__FILE__, __LINE__, frm, rest)
1071 #define debugf2(frm, arg1, arg2)        _debugf(__FILE__, __LINE__, frm, arg1, arg2)
1072 #define debugf3(frm, arg1, arg2, arg3)  _debugf(__FILE__, __LINE__, frm, arg1, arg2, arg3)
1073
1074 #endif
1075
1076 extern void _debugf(char *f, int l, char *frm, ...);
1077
1078
1079 /*-----------------------------------------------------------------*
1080  * pCode objects.
1081  *-----------------------------------------------------------------*/
1082
1083 extern pCodeOpReg pic16_pc_status;
1084 extern pCodeOpReg pic16_pc_intcon;
1085 extern pCodeOpReg pic16_pc_pcl;
1086 extern pCodeOpReg pic16_pc_pclath;
1087 extern pCodeOpReg pic16_pc_pclatu;
1088 extern pCodeOpReg pic16_pc_wreg;
1089 extern pCodeOpReg pic16_pc_tosl;
1090 extern pCodeOpReg pic16_pc_tosh;
1091 extern pCodeOpReg pic16_pc_tosu;
1092 extern pCodeOpReg pic16_pc_tblptrl;
1093 extern pCodeOpReg pic16_pc_tblptrh;
1094 extern pCodeOpReg pic16_pc_tblptru;
1095 extern pCodeOpReg pic16_pc_tablat;
1096 extern pCodeOpReg pic16_pc_bsr;
1097 extern pCodeOpReg pic16_pc_fsr0;
1098 extern pCodeOpReg pic16_pc_fsr0l;
1099 extern pCodeOpReg pic16_pc_fsr0h;
1100 extern pCodeOpReg pic16_pc_fsr1l;
1101 extern pCodeOpReg pic16_pc_fsr1h;
1102 extern pCodeOpReg pic16_pc_fsr2l;
1103 extern pCodeOpReg pic16_pc_fsr2h;
1104 extern pCodeOpReg pic16_pc_indf0;
1105 extern pCodeOpReg pic16_pc_postinc0;
1106 extern pCodeOpReg pic16_pc_postdec0;
1107 extern pCodeOpReg pic16_pc_preinc0;
1108 extern pCodeOpReg pic16_pc_plusw0;
1109 extern pCodeOpReg pic16_pc_indf1;
1110 extern pCodeOpReg pic16_pc_postinc1;
1111 extern pCodeOpReg pic16_pc_postdec1;
1112 extern pCodeOpReg pic16_pc_preinc1;
1113 extern pCodeOpReg pic16_pc_plusw1;
1114 extern pCodeOpReg pic16_pc_indf2;
1115 extern pCodeOpReg pic16_pc_postinc2;
1116 extern pCodeOpReg pic16_pc_postdec2;
1117 extern pCodeOpReg pic16_pc_preinc2;
1118 extern pCodeOpReg pic16_pc_plusw2;
1119 extern pCodeOpReg pic16_pc_prodl;
1120 extern pCodeOpReg pic16_pc_prodh;
1121
1122 extern pCodeOpReg pic16_pc_eecon1;
1123 extern pCodeOpReg pic16_pc_eecon2;
1124 extern pCodeOpReg pic16_pc_eedata;
1125 extern pCodeOpReg pic16_pc_eeadr;
1126
1127 extern pCodeOpReg pic16_pc_kzero;
1128 extern pCodeOpReg pic16_pc_wsave;     /* wsave and ssave are used to save W and the Status */
1129 extern pCodeOpReg pic16_pc_ssave;     /* registers during an interrupt */
1130
1131 extern pCodeOpReg *pic16_stackpnt_lo;
1132 extern pCodeOpReg *pic16_stackpnt_hi;
1133 extern pCodeOpReg *pic16_stack_postinc;
1134 extern pCodeOpReg *pic16_stack_postdec;
1135 extern pCodeOpReg *pic16_stack_preinc;
1136 extern pCodeOpReg *pic16_stack_plusw;
1137
1138 extern pCodeOpReg *pic16_framepnt_lo;
1139 extern pCodeOpReg *pic16_framepnt_hi;
1140 extern pCodeOpReg *pic16_frame_postinc;
1141 extern pCodeOpReg *pic16_frame_postdec;
1142 extern pCodeOpReg *pic16_frame_preinc;
1143 extern pCodeOpReg *pic16_frame_plusw;
1144
1145 extern pCodeOpReg pic16_pc_gpsimio;
1146 extern pCodeOpReg pic16_pc_gpsimio2;
1147
1148 #endif // __PCODE_H__