* .version: increased version number to 2.4.2,
[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 /***********************************************************************
90  *  PIC status bits - this will move into device dependent headers
91  ***********************************************************************/
92 #define PIC_C_BIT    0
93 #define PIC_DC_BIT   1
94 #define PIC_Z_BIT    2
95 #define PIC_OV_BIT   3
96 #define PIC_N_BIT    4
97 #define PIC_IRP_BIT  7   /* Indirect register page select */
98
99 /***********************************************************************
100  *  PIC INTCON bits - this will move into device dependent headers
101  ***********************************************************************/
102 #define PIC_RBIF_BIT 0   /* Port B level has changed flag */
103 #define PIC_INTF_BIT 1   /* Port B bit 0 interrupt on edge flag */
104 #define PIC_T0IF_BIT 2   /* TMR0 has overflowed flag */
105 #define PIC_RBIE_BIT 3   /* Port B level has changed - Interrupt Enable */
106 #define PIC_INTE_BIT 4   /* Port B bit 0 interrupt on edge - Int Enable */
107 #define PIC_T0IE_BIT 5   /* TMR0 overflow Interrupt Enable */
108 #define PIC_PIE_BIT  6   /* Peripheral Interrupt Enable */
109 #define PIC_GIE_BIT  7   /* Global Interrupt Enable */
110
111 /***********************************************************************
112  *  PIC bank definitions
113  ***********************************************************************/
114 #define PIC_BANK_FIRST 0
115 #define PIC_BANK_LAST  0xf
116
117
118 /***********************************************************************
119  *  Operand types 
120  ***********************************************************************/
121 #define POT_RESULT  0
122 #define POT_LEFT    1
123 #define POT_RIGHT   2
124
125
126 /***********************************************************************
127  *
128  *  PIC_OPTYPE - Operand types that are specific to the PIC architecture
129  *
130  *  If a PIC assembly instruction has an operand then here is where we
131  *  associate a type to it. For example,
132  *
133  *     movf    reg,W
134  *
135  *  The movf has two operands: 'reg' and the W register. 'reg' is some
136  *  arbitrary general purpose register, hence it has the type PO_GPR_REGISTER.
137  *  The W register, which is the PIC's accumulator, has the type PO_W.
138  *
139  ***********************************************************************/
140
141
142
143 typedef enum 
144 {
145   PO_NONE=0,         // No operand e.g. NOP
146   PO_W,              // The working register (as a destination)
147   PO_WREG,           // The working register (as a file register)
148   PO_STATUS,         // The 'STATUS' register
149   PO_BSR,            // The 'BSR' register
150   PO_FSR0,           // The "file select register" (in PIC18 family it's one 
151                      // of three)
152   PO_INDF0,          // The Indirect register
153   PO_INTCON,         // Interrupt Control register
154   PO_GPR_REGISTER,   // A general purpose register
155   PO_GPR_BIT,        // A bit of a general purpose register
156   PO_GPR_TEMP,       // A general purpose temporary register
157   PO_SFR_REGISTER,   // A special function register (e.g. PORTA)
158   PO_PCL,            // Program counter Low register
159   PO_PCLATH,         // Program counter Latch high register
160   PO_PCLATU,         // Program counter Latch upper register
161   PO_PRODL,          // Product Register Low
162   PO_PRODH,          // Product Register High
163   PO_LITERAL,        // A constant
164   PO_REL_ADDR,       // A relative address
165   PO_IMMEDIATE,      //  (8051 legacy)
166   PO_DIR,            // Direct memory (8051 legacy)
167   PO_CRY,            // bit memory (8051 legacy)
168   PO_BIT,            // bit operand.
169   PO_STR,            //  (8051 legacy)
170   PO_LABEL,
171   PO_WILD            // Wild card operand in peep optimizer
172 } PIC_OPTYPE;
173
174
175 /***********************************************************************
176  *
177  *  PIC_OPCODE
178  *
179  *  This is not a list of the PIC's opcodes per se, but instead
180  *  an enumeration of all of the different types of pic opcodes. 
181  *
182  ***********************************************************************/
183
184 typedef enum
185 {
186   POC_WILD=-1,   /* Wild card - used in the pCode peep hole optimizer
187                   * to represent ANY pic opcode */
188   POC_ADDLW=0,
189   POC_ADDWF,
190   POC_ADDFW,
191   POC_ADDFWC,
192   POC_ADDWFC,
193   POC_ANDLW,
194   POC_ANDWF,
195   POC_ANDFW,
196   POC_BC,
197   POC_BCF,
198   POC_BN,
199   POC_BNC,
200   POC_BNN,
201   POC_BNOV,
202   POC_BNZ,
203   POC_BOV,
204   POC_BRA,
205   POC_BSF,
206   POC_BTFSC,
207   POC_BTFSS,
208   POC_BTG,
209   POC_BZ,
210   POC_CALL,
211   POC_CLRF,
212   POC_CLRWDT,
213   POC_COMF,
214   POC_COMFW,
215   POC_CPFSEQ,
216   POC_CPFSGT,
217   POC_CPFSLT,
218   POC_DAW,
219   POC_DCFSNZ,
220   POC_DCFSNZW,
221   POC_DECF,
222   POC_DECFW,
223   POC_DECFSZ,
224   POC_DECFSZW,
225   POC_GOTO,
226   POC_INCF,
227   POC_INCFW,
228   POC_INCFSZ,
229   POC_INCFSZW,
230   POC_INFSNZ,
231   POC_IORWF,
232   POC_IORFW,
233   POC_IORLW,
234   POC_LFSR,
235   POC_MOVF,
236   POC_MOVFW,
237   POC_MOVFF,
238   POC_MOVLB,
239   POC_MOVLW,
240   POC_MOVWF,
241   POC_MULLW,
242   POC_MULWF,
243   POC_NEGF,
244   POC_NOP,
245   POC_POP,
246   POC_PUSH,
247   POC_RCALL,
248   POC_RETFIE,
249   POC_RETLW,
250   POC_RETURN,
251   POC_RLCF,
252   POC_RLCFW,
253   POC_RLNCF,
254   POC_RLNCFW,
255   POC_RRCF,
256   POC_RRCFW,
257   POC_RRNCF,
258   POC_RRNCFW,
259   POC_SETF,
260   POC_SUBLW,
261   POC_SUBFWB,
262   POC_SUBWF,
263   POC_SUBFW,
264   POC_SUBWFB_D0,
265   POC_SUBWFB_D1,
266   POC_SUBFWB_D0,
267   POC_SUBFWB_D1,
268   POC_SWAPF,
269   POC_SWAPFW,
270 //  POC_TRIS , // To be removed
271   POC_TBLRD,            // patch 15
272   POC_TBLRD_POSTINC,    //
273   POC_TBLRD_POSTDEC,    //
274   POC_TBLRD_PREINC,     //
275   POC_TBLWT,            //
276   POC_TBLWT_POSTINC,    //
277   POC_TBLWT_POSTDEC,    //
278   POC_TBLWT_PREINC,     // patch 15
279   POC_TSTFSZ,
280   POC_XORLW,
281   POC_XORWF,
282   POC_XORFW
283 } PIC_OPCODE;
284
285
286 /***********************************************************************
287  *  PC_TYPE  - pCode Types
288  ***********************************************************************/
289
290 typedef enum
291 {
292   PC_COMMENT=0,   /* pCode is a comment     */
293   PC_INLINE,      /* user's inline code     */
294   PC_OPCODE,      /* PORT dependent opcode  */
295   PC_LABEL,       /* assembly label         */
296   PC_FLOW,        /* flow analysis          */
297   PC_FUNCTION,    /* Function start or end  */
298   PC_WILD,        /* wildcard - an opcode place holder used 
299                    * in the pCode peep hole optimizer */
300   PC_CSOURCE,     /* C-Source Line  */
301   PC_ASMDIR,      /* Assembler directive */
302   PC_BAD          /* Mark the pCode object as being bad */
303 } PC_TYPE;
304
305 /************************************************/
306 /***************  Structures ********************/
307 /************************************************/
308 /* These are here as forward references - the 
309  * full definition of these are below           */
310 struct pCode;
311 struct pCodeWildBlock;
312 struct pCodeRegLives;
313
314 /*************************************************
315   pBranch
316
317   The first step in optimizing pCode is determining
318  the program flow. This information is stored in
319  single-linked lists in the for of 'from' and 'to'
320  objects with in a pcode. For example, most instructions
321  don't involve any branching. So their from branch
322  points to the pCode immediately preceding them and
323  their 'to' branch points to the pcode immediately
324  following them. A skip instruction is an example of
325  a pcode that has multiple (in this case two) elements
326  in the 'to' branch. A 'label' pcode is an where there
327  may be multiple 'from' branches.
328  *************************************************/
329
330 typedef struct pBranch
331 {
332   struct pCode   *pc;    // Next pCode in a branch
333   struct pBranch *next;  /* If more than one branch
334                           * the next one is here */
335
336 } pBranch;
337
338 /*************************************************
339   pCodeOp
340
341   pCode Operand structure.
342   For those assembly instructions that have arguments, 
343   the pCode will have a pCodeOp in which the argument
344   can be stored. For example
345
346     movf   some_register,w
347
348   'some_register' will be stored/referenced in a pCodeOp
349
350  *************************************************/
351
352 typedef struct pCodeOp
353 {
354   PIC_OPTYPE type;
355   char *name;
356   
357 } pCodeOp;
358
359 #if 0
360 typedef struct pCodeOpBit
361 {
362   pCodeOp pcop;
363   int bit;
364   unsigned int inBitSpace: 1; /* True if in bit space, else
365                                  just a bit of a register */
366 } pCodeOpBit;
367 #endif
368 typedef struct pCodeOpLit
369 {
370   pCodeOp pcop;
371   int lit;
372 } pCodeOpLit;
373
374 typedef struct pCodeOpLit2
375 {
376   pCodeOp pcop;
377   int lit;
378   pCodeOp *arg2;
379 } pCodeOpLit2;
380
381
382 typedef struct pCodeOpImmd
383 {
384   pCodeOp pcop;
385   int offset;           /* low,high or upper byte of immediate value */
386   int index;            /* add this to the immediate value */
387   unsigned _const:1;    /* is in code space    */
388
389   int rIdx;             /* If this immd points to a register */
390   struct regs *r;       /* then this is the reg. */
391
392 } pCodeOpImmd;
393
394 typedef struct pCodeOpLabel
395 {
396   pCodeOp pcop;
397   int key;
398 } pCodeOpLabel;
399
400 typedef struct pCodeOpReg
401 {
402   pCodeOp pcop;    // Can be either GPR or SFR
403   int rIdx;        // Index into the register table
404   struct regs *r;
405   int instance;    // byte # of Multi-byte registers
406   struct pBlock *pb;
407 } pCodeOpReg;
408
409 typedef struct pCodeOpReg2
410 {
411   pCodeOp pcop;         // used by default to all references
412   int rIdx;
413   struct regs *r;
414   int instance;         // assume same instance for both operands
415   struct pBlock *pb;
416
417   pCodeOp *pcop2;       // second memory operand
418 } pCodeOpReg2;
419
420 typedef struct pCodeOpRegBit
421 {
422   pCodeOpReg  pcor;       // The Register containing this bit
423   int bit;                // 0-7 bit number.
424   PIC_OPTYPE subtype;     // The type of this register.
425   unsigned int inBitSpace: 1; /* True if in bit space, else
426                                  just a bit of a register */
427 } pCodeOpRegBit;
428
429
430 typedef struct pCodeOpWild
431 {
432   pCodeOp pcop;
433
434   struct pCodeWildBlock *pcwb;
435
436   int id;                 /* index into an array of char *'s that will match
437                            * the wild card. The array is in *pcp. */
438   pCodeOp *subtype;       /* Pointer to the Operand type into which this wild
439                            * card will be expanded */
440   pCodeOp *matched;       /* When a wild matches, we'll store a pointer to the
441                            * opcode we matched */
442
443   pCodeOp *pcop2;         /* second operand if exists */
444
445 } pCodeOpWild;
446
447
448 /*************************************************
449     pCode
450
451     Here is the basic build block of a PIC instruction.
452     Each pic instruction will get allocated a pCode.
453     A linked list of pCodes makes a program.
454
455 **************************************************/
456
457 typedef struct pCode
458 {
459   PC_TYPE    type;
460
461   struct pCode *prev;  // The pCode objects are linked together
462   struct pCode *next;  // in doubly linked lists.
463
464   int seq;             // sequence number
465
466   struct pBlock *pb;   // The pBlock that contains this pCode.
467
468   /* "virtual functions"
469    *  The pCode structure is like a base class
470    * in C++. The subsequent structures that "inherit"
471    * the pCode structure will initialize these function
472    * pointers to something useful */
473   //  void (*analyze) (struct pCode *_this);
474   void (*destruct)(struct pCode *_this);
475   void (*print)  (FILE *of,struct pCode *_this);
476
477 } pCode;
478
479
480 /*************************************************
481     pCodeComment
482 **************************************************/
483
484 typedef struct pCodeComment
485 {
486
487   pCode  pc;
488
489   char *comment;
490
491 } pCodeComment;
492
493
494 /*************************************************
495     pCodeCSource
496 **************************************************/
497
498 typedef struct pCodeCSource
499 {
500
501   pCode  pc;
502
503   int  line_number;
504   char *line;
505   char *file_name;
506
507 } pCodeCSource;
508
509
510 /*************************************************
511     pCodeAsmDir
512 **************************************************/
513
514 /*************************************************
515     pCodeFlow
516
517   The Flow object is used as marker to separate 
518  the assembly code into contiguous chunks. In other
519  words, everytime an instruction cause or potentially
520  causes a branch, a Flow object will be inserted into
521  the pCode chain to mark the beginning of the next
522  contiguous chunk.
523
524 **************************************************/
525
526 typedef struct pCodeFlow
527 {
528
529   pCode  pc;
530
531   pCode *end;   /* Last pCode in this flow. Note that
532                    the first pCode is pc.next */
533
534   /*  set **uses;   * map the pCode instruction inCond and outCond conditions 
535                  * in this array of set's. The reason we allocate an 
536                  * array of pointers instead of declaring each type of 
537                  * usage is because there are port dependent usage definitions */
538   //int nuses;    /* number of uses sets */
539
540   set *from;    /* flow blocks that can send control to this flow block */
541   set *to;      /* flow blocks to which this one can send control */
542   struct pCodeFlow *ancestor; /* The most immediate "single" pCodeFlow object that
543                                * executes prior to this one. In many cases, this 
544                                * will be just the previous */
545
546   int inCond;   /* Input conditions - stuff assumed defined at entry */
547   int outCond;  /* Output conditions - stuff modified by flow block */
548
549   int firstBank; /* The first and last bank flags are the first and last */
550   int lastBank;  /* register banks used within one flow object */
551
552   int FromConflicts;
553   int ToConflicts;
554
555   set *registers;/* Registers used in this flow */
556
557 } pCodeFlow;
558
559 /*************************************************
560   pCodeFlowLink
561
562   The Flow Link object is used to record information
563  about how consecutive excutive Flow objects are related.
564  The pCodeFlow objects demarcate the pCodeInstructions
565  into contiguous chunks. The FlowLink records conflicts
566  in the discontinuities. For example, if one Flow object
567  references a register in bank 0 and the next Flow object
568  references a register in bank 1, then there is a discontinuity
569  in the banking registers.
570
571 */
572 typedef struct pCodeFlowLink
573 {
574   pCodeFlow  *pcflow;   /* pointer to linked pCodeFlow object */
575
576   int bank_conflict;    /* records bank conflicts */
577
578 } pCodeFlowLink;
579
580 /*************************************************
581     pCodeInstruction
582
583     Here we describe all the facets of a PIC instruction
584     (expansion for the 18cxxx is also provided).
585
586 **************************************************/
587
588 typedef struct pCodeInstruction
589 {
590
591   pCode  pc;
592
593   PIC_OPCODE op;        // The opcode of the instruction.
594
595   char const * const mnemonic;       // Pointer to mnemonic string
596
597   pBranch *from;       // pCodes that execute before this one
598   pBranch *to;         // pCodes that execute after
599   pBranch *label;      // pCode instructions that have labels
600
601   pCodeOp *pcop;               /* Operand, if this instruction has one */
602   pCodeFlow *pcflow;           /* flow block to which this instruction belongs */
603   pCodeCSource *cline;         /* C Source from which this instruction was derived */
604
605   unsigned int num_ops;        /* Number of operands (0,1,2 for mid range pics) */
606   unsigned int isModReg:  1;   /* If destination is W or F, then 1==F */
607   unsigned int isBitInst: 1;   /* e.g. BCF */
608   unsigned int isBranch:  1;   /* True if this is a branching instruction */
609   unsigned int isSkip:    1;   /* True if this is a skip instruction */
610   unsigned int isLit:     1;   /* True if this instruction has an literal operand */
611   unsigned int isAccess:   1;   /* True if this instruction has an access RAM operand */
612   unsigned int isFastCall: 1;   /* True if this instruction has a fast call/return mode select operand */
613   unsigned int is2MemOp: 1;     /* True is second operand is a memory operand VR - support for MOVFF */
614   unsigned int is2LitOp: 1;     /* True if instruction takes 2 literal operands VR - support for LFSR */
615
616   PIC_OPCODE inverted_op;      /* Opcode of instruction that's the opposite of this one */
617   unsigned int inCond;   // Input conditions for this instruction
618   unsigned int outCond;  // Output conditions for this instruction
619
620 #define PCI_MAGIC       0x6e12
621   unsigned int pci_magic;       // sanity check for pci initialization
622 } pCodeInstruction;
623
624
625
626 /*************************************************
627     pCodeAsmDir
628 **************************************************/
629
630 typedef struct pCodeAsmDir
631 {
632   pCodeInstruction pci;
633   
634   char *directive;
635   char *arg;
636 } pCodeAsmDir;
637
638
639 /*************************************************
640     pCodeLabel
641 **************************************************/
642
643 typedef struct pCodeLabel
644 {
645
646   pCode  pc;
647
648   char *label;
649   int key;
650   int force;            /* label cannot be optimized out */
651
652 } pCodeLabel;
653
654 /*************************************************
655     pCodeFunction
656 **************************************************/
657
658 typedef struct pCodeFunction
659 {
660
661   pCode  pc;
662
663   char *modname;
664   char *fname;     /* If NULL, then this is the end of
665                       a function. Otherwise, it's the
666                       start and the name is contained
667                       here */
668
669   pBranch *from;       // pCodes that execute before this one
670   pBranch *to;         // pCodes that execute after
671   pBranch *label;      // pCode instructions that have labels
672
673   int  ncalled;    /* Number of times function is called */
674
675   int absblock;    /* hack to emulate a block pCodes in absolute position
676                       but not inside a function */
677 } pCodeFunction;
678
679
680 /*************************************************
681     pCodeWild
682 **************************************************/
683
684 typedef struct pCodeWild
685 {
686
687   pCodeInstruction  pci;
688
689   int    id;     /* Index into the wild card array of a peepBlock 
690                   * - this wild card will get expanded into that pCode
691                   *   that is stored at this index */
692
693   /* Conditions on wild pcode instruction */
694   int    mustBeBitSkipInst:1;
695   int    mustNotBeBitSkipInst:1;
696   int    invertBitSkipInst:1;
697
698   pCodeOp *operand;  // Optional operand
699   pCodeOp *label;    // Optional label
700
701 } pCodeWild;
702
703 /*************************************************
704     pBlock
705
706     Here are PIC program snippets. There's a strong
707     correlation between the eBBlocks and pBlocks.
708     SDCC subdivides a C program into managable chunks.
709     Each chunk becomes a eBBlock and ultimately in the
710     PIC port a pBlock.
711
712 **************************************************/
713
714 typedef struct pBlock
715 {
716   memmap *cmemmap;   /* The snippet is from this memmap */
717   char   dbName;     /* if cmemmap is NULL, then dbName will identify the block */
718   pCode *pcHead;     /* A pointer to the first pCode in a link list of pCodes */
719   pCode *pcTail;     /* A pointer to the last pCode in a link list of pCodes */
720
721   struct pBlock *next;      /* The pBlocks will form a doubly linked list */
722   struct pBlock *prev;
723
724   set *function_entries;    /* dll of functions in this pblock */
725   set *function_exits;
726   set *function_calls;
727   set *tregisters;
728
729   set *FlowTree;
730   unsigned visited:1;       /* set true if traversed in call tree */
731
732   unsigned seq;             /* sequence number of this pBlock */
733
734 } pBlock;
735
736 /*************************************************
737     pFile
738
739     The collection of pBlock program snippets are
740     placed into a linked list that is implemented
741     in the pFile structure.
742
743     The pcode optimizer will parse the pFile.
744
745 **************************************************/
746
747 typedef struct pFile
748 {
749   pBlock *pbHead;     /* A pointer to the first pBlock */
750   pBlock *pbTail;     /* A pointer to the last pBlock */
751
752   pBranch *functions; /* A SLL of functions in this pFile */
753
754 } pFile;
755
756
757
758 /*************************************************
759   pCodeWildBlock
760
761   The pCodeWildBlock object keeps track of the wild
762   variables, operands, and opcodes that exist in
763   a pBlock.
764 **************************************************/
765 typedef struct pCodeWildBlock {
766   pBlock    *pb;
767   struct pCodePeep *pcp;    // pointer back to ... I don't like this...
768
769   int       nvars;          // Number of wildcard registers in target.
770   char    **vars;           // array of pointers to them
771
772   int       nops;           // Number of wildcard operands in target.
773   pCodeOp **wildpCodeOps;   // array of pointers to the pCodeOp's.
774
775   int       nwildpCodes;    // Number of wildcard pCodes in target/replace
776   pCode   **wildpCodes;     // array of pointers to the pCode's.
777
778 } pCodeWildBlock;
779
780 /*************************************************
781   pCodePeep
782
783   The pCodePeep object mimics the peep hole optimizer
784   in the main SDCC src (e.g. SDCCpeeph.c). Essentially
785   there is a target pCode chain and a replacement
786   pCode chain. The target chain is compared to the
787   pCode that is generated by gen.c. If a match is
788   found then the pCode is replaced by the replacement
789   pCode chain.
790 **************************************************/
791 typedef struct pCodePeep {
792   pCodeWildBlock target;     // code we'd like to optimize
793   pCodeWildBlock replace;    // and this is what we'll optimize it with.
794
795   //pBlock *target;
796   //pBlock replace;            // and this is what we'll optimize it with.
797
798
799
800   /* (Note: a wildcard register is a place holder. Any register
801    * can be replaced by the wildcard when the pcode is being 
802    * compared to the target. */
803
804   /* Post Conditions. A post condition is a condition that
805    * must be either true or false before the peep rule is
806    * accepted. For example, a certain rule may be accepted
807    * if and only if the Z-bit is not used as an input to 
808    * the subsequent instructions in a pCode chain.
809    */
810   unsigned int postFalseCond;  
811   unsigned int postTrueCond;
812
813 } pCodePeep;
814
815 /*************************************************
816
817   pCode peep command definitions 
818
819  Here are some special commands that control the
820 way the peep hole optimizer behaves
821
822 **************************************************/
823
824 enum peepCommandTypes{
825   NOTBITSKIP = 0,
826   BITSKIP,
827   INVERTBITSKIP,
828   _LAST_PEEP_COMMAND_
829 };
830
831 /*************************************************
832     peepCommand structure stores the peep commands.
833
834 **************************************************/
835
836 typedef struct peepCommand {
837   int id;
838   char *cmd;
839 } peepCommand;
840
841 /*************************************************
842     pCode Macros
843
844 **************************************************/
845 #define PCODE(x)  ((pCode *)(x))
846 #define PCI(x)    ((pCodeInstruction *)(x))
847 #define PCL(x)    ((pCodeLabel *)(x))
848 #define PCF(x)    ((pCodeFunction *)(x))
849 #define PCFL(x)   ((pCodeFlow *)(x))
850 #define PCFLINK(x)((pCodeFlowLink *)(x))
851 #define PCW(x)    ((pCodeWild *)(x))
852 #define PCCS(x)   ((pCodeCSource *)(x))
853 #define PCAD(x)   ((pCodeAsmDir *)(x))
854
855 #define PCOP(x)   ((pCodeOp *)(x))
856 //#define PCOB(x)   ((pCodeOpBit *)(x))
857 #define PCOL(x)   ((pCodeOpLit *)(x))
858 #define PCOI(x)   ((pCodeOpImmd *)(x))
859 #define PCOLAB(x) ((pCodeOpLabel *)(x))
860 #define PCOR(x)   ((pCodeOpReg *)(x))
861 #define PCOR2(x)  ((pCodeOpReg2 *)(x))
862 #define PCORB(x)  ((pCodeOpRegBit *)(x))
863 #define PCOW(x)   ((pCodeOpWild *)(x))
864 #define PCOW2(x)  (PCOW(PCOW(x)->pcop2))
865 #define PBR(x)    ((pBranch *)(x))
866
867 #define PCWB(x)   ((pCodeWildBlock *)(x))
868
869
870 /*
871   macros for checking pCode types
872 */
873 #define isPCI(x)        ((PCODE(x)->type == PC_OPCODE))
874 #define isPCI_BRANCH(x) ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isBranch)
875 #define isPCI_SKIP(x)   ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isSkip)
876 #define isPCI_LIT(x)    ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isLit)
877 #define isPCI_BITSKIP(x)((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isSkip && PCI(x)->isBitInst)
878 #define isPCFL(x)       ((PCODE(x)->type == PC_FLOW))
879 #define isPCF(x)        ((PCODE(x)->type == PC_FUNCTION))
880 #define isPCL(x)        ((PCODE(x)->type == PC_LABEL))
881 #define isPCW(x)        ((PCODE(x)->type == PC_WILD))
882 #define isPCCS(x)       ((PCODE(x)->type == PC_CSOURCE))
883 #define isASMDIR(x)     ((PCODE(x)->type == PC_ASMDIR))
884
885 #define isCALL(x)       ((isPCI(x)) && (PCI(x)->op == POC_CALL))
886 #define isSTATUS_REG(r) ((r)->pc_type == PO_STATUS)
887 #define isBSR_REG(r)    ((r)->pc_type == PO_BSR)
888 #define isACCESS_BANK(r)        (r->accessBank)
889
890
891
892 #define isPCOLAB(x)     ((PCOP(x)->type) == PO_LABEL)
893
894 /*-----------------------------------------------------------------*
895  * pCode functions.
896  *-----------------------------------------------------------------*/
897
898 pCode *pic16_newpCode (PIC_OPCODE op, pCodeOp *pcop); // Create a new pCode given an operand
899 pCode *pic16_newpCodeCharP(char *cP);              // Create a new pCode given a char *
900 pCode *pic16_newpCodeInlineP(char *cP);            // Create a new pCode given a char *
901 pCode *pic16_newpCodeFunction(char *g, char *f);   // Create a new function
902 pCode *pic16_newpCodeLabel(char *name,int key);    // Create a new label given a key
903 pCode *pic16_newpCodeLabelFORCE(char *name, int key); // Same as newpCodeLabel but label cannot be optimized out
904 pCode *pic16_newpCodeCSource(int ln, char *f, char *l); // Create a new symbol line 
905 pBlock *pic16_newpCodeChain(memmap *cm,char c, pCode *pc); // Create a new pBlock
906 void pic16_printpBlock(FILE *of, pBlock *pb);      // Write a pBlock to a file
907 void pic16_addpCode2pBlock(pBlock *pb, pCode *pc); // Add a pCode to a pBlock
908 void pic16_addpBlock(pBlock *pb);                  // Add a pBlock to a pFile
909 void pic16_copypCode(FILE *of, char dbName);       // Write all pBlocks with dbName to *of
910 void pic16_movepBlock2Head(char dbName);           // move pBlocks around
911 void pic16_AnalyzepCode(char dbName);
912 void pic16_AssignRegBanks(void);
913 void pic16_printCallTree(FILE *of);
914 void pCodePeepInit(void);
915 void pic16_pBlockConvert2ISR(pBlock *pb);
916 void pic16_pBlockConvert2Absolute(pBlock *pb);
917 void pic16_initDB(void);
918 void pic16_emitDB(char c, char ptype, void *p);           // Add DB directives to a pBlock
919 void pic16_emitDS(char *s, char ptype, void *p);
920 void pic16_flushDB(char ptype, void *p);                          // Add pending DB data to a pBlock
921
922 pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...); 
923
924 pCodeOp *pic16_newpCodeOpLabel(char *name, int key);
925 pCodeOp *pic16_newpCodeOpImmd(char *name, int offset, int index, int code_space);
926 pCodeOp *pic16_newpCodeOpLit(int lit);
927 pCodeOp *pic16_newpCodeOpLit2(int lit, pCodeOp *arg2);
928 pCodeOp *pic16_newpCodeOpBit(char *name, int bit,int inBitSpace);
929 pCodeOp *pic16_newpCodeOpRegFromStr(char *name);
930 pCodeOp *pic16_newpCodeOp(char *name, PIC_OPTYPE p);
931 pCodeOp *pic16_pCodeOpCopy(pCodeOp *pcop);
932
933 pCode * pic16_findNextInstruction(pCode *pci);
934 pCode * pic16_findNextpCode(pCode *pc, PC_TYPE pct);
935 int pic16_isPCinFlow(pCode *pc, pCode *pcflow);
936 struct regs * pic16_getRegFromInstruction(pCode *pc);
937 struct regs * pic16_getRegFromInstruction2(pCode *pc);
938
939 extern void pic16_pcode_test(void);
940 extern int pic16_debug_verbose;
941 extern int pic16_pcode_verbose;
942
943 /*-----------------------------------------------------------------*
944  * pCode objects.
945  *-----------------------------------------------------------------*/
946
947 extern pCodeOpReg pic16_pc_status;
948 extern pCodeOpReg pic16_pc_intcon;
949 extern pCodeOpReg pic16_pc_pcl;
950 extern pCodeOpReg pic16_pc_pclath;
951 extern pCodeOpReg pic16_pc_pclatu; // patch 14
952 extern pCodeOpReg pic16_pc_wreg;
953 extern pCodeOpReg pic16_pc_tosl; // patch 14
954 extern pCodeOpReg pic16_pc_tosh; // patch 14
955 extern pCodeOpReg pic16_pc_tosu; // patch 14
956 extern pCodeOpReg pic16_pc_tblptrl; // patch 15
957 extern pCodeOpReg pic16_pc_tblptrh; //
958 extern pCodeOpReg pic16_pc_tblptru; //
959 extern pCodeOpReg pic16_pc_tablat;  // patch 15
960 extern pCodeOpReg pic16_pc_bsr;
961 extern pCodeOpReg pic16_pc_fsr0;
962 extern pCodeOpReg pic16_pc_fsr0l;
963 extern pCodeOpReg pic16_pc_fsr0h;
964 extern pCodeOpReg pic16_pc_fsr1l;
965 extern pCodeOpReg pic16_pc_fsr1h;
966 extern pCodeOpReg pic16_pc_fsr2l;
967 extern pCodeOpReg pic16_pc_fsr2h;
968 extern pCodeOpReg pic16_pc_indf0;
969 extern pCodeOpReg pic16_pc_postinc0;
970 extern pCodeOpReg pic16_pc_postdec0;
971 extern pCodeOpReg pic16_pc_preinc0;
972 extern pCodeOpReg pic16_pc_plusw0;
973 extern pCodeOpReg pic16_pc_indf1;
974 extern pCodeOpReg pic16_pc_postinc1;
975 extern pCodeOpReg pic16_pc_postdec1;
976 extern pCodeOpReg pic16_pc_preinc1;
977 extern pCodeOpReg pic16_pc_plusw1;
978 extern pCodeOpReg pic16_pc_indf2;
979 extern pCodeOpReg pic16_pc_postinc2;
980 extern pCodeOpReg pic16_pc_postdec2;
981 extern pCodeOpReg pic16_pc_preinc2;
982 extern pCodeOpReg pic16_pc_plusw2;
983 extern pCodeOpReg pic16_pc_prodl;
984 extern pCodeOpReg pic16_pc_prodh;
985
986 extern pCodeOpReg pic16_pc_kzero;
987 extern pCodeOpReg pic16_pc_wsave;     /* wsave and ssave are used to save W and the Status */
988 extern pCodeOpReg pic16_pc_ssave;     /* registers during an interrupt */
989
990
991 #endif // __PCODE_H__