2004-11-04 Vangelis Rokas <vrokas AT otenet.gr>
[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_INFSNZW,
232   POC_IORWF,
233   POC_IORFW,
234   POC_IORLW,
235   POC_LFSR,
236   POC_MOVF,
237   POC_MOVFW,
238   POC_MOVFF,
239   POC_MOVLB,
240   POC_MOVLW,
241   POC_MOVWF,
242   POC_MULLW,
243   POC_MULWF,
244   POC_NEGF,
245   POC_NOP,
246   POC_POP,
247   POC_PUSH,
248   POC_RCALL,
249   POC_RETFIE,
250   POC_RETLW,
251   POC_RETURN,
252   POC_RLCF,
253   POC_RLCFW,
254   POC_RLNCF,
255   POC_RLNCFW,
256   POC_RRCF,
257   POC_RRCFW,
258   POC_RRNCF,
259   POC_RRNCFW,
260   POC_SETF,
261   POC_SUBLW,
262   POC_SUBFWB,
263   POC_SUBWF,
264   POC_SUBFW,
265   POC_SUBWFB_D0,
266   POC_SUBWFB_D1,
267   POC_SUBFWB_D0,
268   POC_SUBFWB_D1,
269   POC_SWAPF,
270   POC_SWAPFW,
271   POC_TBLRD,
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,
279   POC_TSTFSZ,
280   POC_XORLW,
281   POC_XORWF,
282   POC_XORFW,
283
284   POC_BANKSEL
285 } PIC_OPCODE;
286
287
288 /***********************************************************************
289  *  PC_TYPE  - pCode Types
290  ***********************************************************************/
291
292 typedef enum
293 {
294   PC_COMMENT=0,   /* pCode is a comment     */
295   PC_INLINE,      /* user's inline code     */
296   PC_OPCODE,      /* PORT dependent opcode  */
297   PC_LABEL,       /* assembly label         */
298   PC_FLOW,        /* flow analysis          */
299   PC_FUNCTION,    /* Function start or end  */
300   PC_WILD,        /* wildcard - an opcode place holder used 
301                    * in the pCode peep hole optimizer */
302   PC_CSOURCE,     /* C-Source Line  */
303   PC_ASMDIR,      /* Assembler directive */
304   PC_BAD,         /* Mark the pCode object as being bad */
305   PC_INFO         /* pCode informatio node, used primarily in optimizing */
306 } PC_TYPE;
307
308
309 /***********************************************************************
310  *  INFO_TYPE  - information node types
311  ***********************************************************************/
312
313 typedef enum
314 {
315   INF_OPTIMIZATION,      /* structure contains optimization information */
316   INF_LOCALREGS          /* structure contains local register information */
317 } INFO_TYPE;
318
319
320
321 /***********************************************************************
322  *  OPT_TYPE  - optimization node types
323  ***********************************************************************/
324
325 typedef enum
326 {
327   OPT_BEGIN,             /* mark beginning of optimization block */
328   OPT_END,               /* mark ending of optimization block */
329 } OPT_TYPE;
330
331 /***********************************************************************
332  *  LR_TYPE  - optimization node types
333  ***********************************************************************/
334
335 typedef enum
336 {
337   LR_ENTRY_BEGIN,             /* mark beginning of optimization block */
338   LR_ENTRY_END,               /* mark ending of optimization block */
339   LR_EXIT_BEGIN,
340   LR_EXIT_END
341 } LR_TYPE;
342
343
344 /************************************************/
345 /***************  Structures ********************/
346 /************************************************/
347 /* These are here as forward references - the 
348  * full definition of these are below           */
349 struct pCode;
350 struct pCodeWildBlock;
351 struct pCodeRegLives;
352
353 /*************************************************
354   pBranch
355
356   The first step in optimizing pCode is determining
357  the program flow. This information is stored in
358  single-linked lists in the for of 'from' and 'to'
359  objects with in a pcode. For example, most instructions
360  don't involve any branching. So their from branch
361  points to the pCode immediately preceding them and
362  their 'to' branch points to the pcode immediately
363  following them. A skip instruction is an example of
364  a pcode that has multiple (in this case two) elements
365  in the 'to' branch. A 'label' pcode is an where there
366  may be multiple 'from' branches.
367  *************************************************/
368
369 typedef struct pBranch
370 {
371   struct pCode   *pc;    // Next pCode in a branch
372   struct pBranch *next;  /* If more than one branch
373                           * the next one is here */
374
375 } pBranch;
376
377 /*************************************************
378   pCodeOp
379
380   pCode Operand structure.
381   For those assembly instructions that have arguments, 
382   the pCode will have a pCodeOp in which the argument
383   can be stored. For example
384
385     movf   some_register,w
386
387   'some_register' will be stored/referenced in a pCodeOp
388
389  *************************************************/
390
391 typedef struct pCodeOp
392 {
393   PIC_OPTYPE type;
394   char *name;
395   
396 } pCodeOp;
397
398 #if 0
399 typedef struct pCodeOpBit
400 {
401   pCodeOp pcop;
402   int bit;
403   unsigned int inBitSpace: 1; /* True if in bit space, else
404                                  just a bit of a register */
405 } pCodeOpBit;
406 #endif
407 typedef struct pCodeOpLit
408 {
409   pCodeOp pcop;
410   int lit;
411 } pCodeOpLit;
412
413 typedef struct pCodeOpLit2
414 {
415   pCodeOp pcop;
416   int lit;
417   pCodeOp *arg2;
418 } pCodeOpLit2;
419
420
421 typedef struct pCodeOpImmd
422 {
423   pCodeOp pcop;
424   int offset;           /* low,high or upper byte of immediate value */
425   int index;            /* add this to the immediate value */
426   unsigned _const:1;    /* is in code space    */
427
428   int rIdx;             /* If this immd points to a register */
429   struct regs *r;       /* then this is the reg. */
430
431 } pCodeOpImmd;
432
433 typedef struct pCodeOpLabel
434 {
435   pCodeOp pcop;
436   int key;
437 } pCodeOpLabel;
438
439 typedef struct pCodeOpReg
440 {
441   pCodeOp pcop;    // Can be either GPR or SFR
442   int rIdx;        // Index into the register table
443   struct regs *r;
444   int instance;    // byte # of Multi-byte registers
445   struct pBlock *pb;
446 } pCodeOpReg;
447
448 typedef struct pCodeOpReg2
449 {
450   pCodeOp pcop;         // used by default to all references
451   int rIdx;
452   struct regs *r;
453   int instance;         // assume same instance for both operands
454   struct pBlock *pb;
455
456   pCodeOp *pcop2;       // second memory operand
457 } pCodeOpReg2;
458
459 typedef struct pCodeOpRegBit
460 {
461   pCodeOpReg  pcor;       // The Register containing this bit
462   int bit;                // 0-7 bit number.
463   PIC_OPTYPE subtype;     // The type of this register.
464   unsigned int inBitSpace: 1; /* True if in bit space, else
465                                  just a bit of a register */
466 } pCodeOpRegBit;
467
468
469 typedef struct pCodeOpWild
470 {
471   pCodeOp pcop;
472
473   struct pCodeWildBlock *pcwb;
474
475   int id;                 /* index into an array of char *'s that will match
476                            * the wild card. The array is in *pcp. */
477   pCodeOp *subtype;       /* Pointer to the Operand type into which this wild
478                            * card will be expanded */
479   pCodeOp *matched;       /* When a wild matches, we'll store a pointer to the
480                            * opcode we matched */
481
482   pCodeOp *pcop2;         /* second operand if exists */
483
484 } pCodeOpWild;
485
486
487 typedef struct pCodeOpOpt
488 {
489   pCodeOp pcop;
490   
491   OPT_TYPE type;          /* optimization node type */
492   
493   char *key;              /* key by which a block is identified */
494 } pCodeOpOpt;
495
496 typedef struct pCodeOpLocalReg
497 {
498   pCodeOp pcop;
499
500   LR_TYPE type;
501 } pCodeOpLocalReg;  
502
503 /*************************************************
504     pCode
505
506     Here is the basic build block of a PIC instruction.
507     Each pic instruction will get allocated a pCode.
508     A linked list of pCodes makes a program.
509
510 **************************************************/
511
512 typedef struct pCode
513 {
514   PC_TYPE    type;
515
516   struct pCode *prev;  // The pCode objects are linked together
517   struct pCode *next;  // in doubly linked lists.
518
519   int seq;             // sequence number
520
521   struct pBlock *pb;   // The pBlock that contains this pCode.
522
523   /* "virtual functions"
524    *  The pCode structure is like a base class
525    * in C++. The subsequent structures that "inherit"
526    * the pCode structure will initialize these function
527    * pointers to something useful */
528   //  void (*analyze) (struct pCode *_this);
529   void (*destruct)(struct pCode *_this);
530   void (*print)  (FILE *of,struct pCode *_this);
531
532 } pCode;
533
534
535 /*************************************************
536     pCodeComment
537 **************************************************/
538
539 typedef struct pCodeComment
540 {
541
542   pCode  pc;
543
544   char *comment;
545
546 } pCodeComment;
547
548
549 /*************************************************
550     pCodeCSource
551 **************************************************/
552
553 typedef struct pCodeCSource
554 {
555
556   pCode  pc;
557
558   int  line_number;
559   char *line;
560   char *file_name;
561
562 } pCodeCSource;
563
564
565 /*************************************************
566     pCodeAsmDir
567 **************************************************/
568
569 /*************************************************
570     pCodeFlow
571
572   The Flow object is used as marker to separate 
573  the assembly code into contiguous chunks. In other
574  words, everytime an instruction cause or potentially
575  causes a branch, a Flow object will be inserted into
576  the pCode chain to mark the beginning of the next
577  contiguous chunk.
578
579 **************************************************/
580
581 typedef struct pCodeFlow
582 {
583
584   pCode  pc;
585
586   pCode *end;   /* Last pCode in this flow. Note that
587                    the first pCode is pc.next */
588
589   /*  set **uses;   * map the pCode instruction inCond and outCond conditions 
590                  * in this array of set's. The reason we allocate an 
591                  * array of pointers instead of declaring each type of 
592                  * usage is because there are port dependent usage definitions */
593   //int nuses;    /* number of uses sets */
594
595   set *from;    /* flow blocks that can send control to this flow block */
596   set *to;      /* flow blocks to which this one can send control */
597   struct pCodeFlow *ancestor; /* The most immediate "single" pCodeFlow object that
598                                * executes prior to this one. In many cases, this 
599                                * will be just the previous */
600
601   int inCond;   /* Input conditions - stuff assumed defined at entry */
602   int outCond;  /* Output conditions - stuff modified by flow block */
603
604   int firstBank; /* The first and last bank flags are the first and last */
605   int lastBank;  /* register banks used within one flow object */
606
607   int FromConflicts;
608   int ToConflicts;
609
610   set *registers;/* Registers used in this flow */
611
612 } pCodeFlow;
613
614 /*************************************************
615   pCodeFlowLink
616
617   The Flow Link object is used to record information
618  about how consecutive excutive Flow objects are related.
619  The pCodeFlow objects demarcate the pCodeInstructions
620  into contiguous chunks. The FlowLink records conflicts
621  in the discontinuities. For example, if one Flow object
622  references a register in bank 0 and the next Flow object
623  references a register in bank 1, then there is a discontinuity
624  in the banking registers.
625
626 */
627 typedef struct pCodeFlowLink
628 {
629   pCodeFlow  *pcflow;   /* pointer to linked pCodeFlow object */
630
631   int bank_conflict;    /* records bank conflicts */
632
633 } pCodeFlowLink;
634
635 /*************************************************
636     pCodeInstruction
637
638     Here we describe all the facets of a PIC instruction
639     (expansion for the 18cxxx is also provided).
640
641 **************************************************/
642
643 typedef struct pCodeInstruction
644 {
645
646   pCode  pc;
647
648   PIC_OPCODE op;        // The opcode of the instruction.
649
650   char const * const mnemonic;       // Pointer to mnemonic string
651
652   char isize;          // pCode instruction size
653
654   pBranch *from;       // pCodes that execute before this one
655   pBranch *to;         // pCodes that execute after
656   pBranch *label;      // pCode instructions that have labels
657
658   pCodeOp *pcop;               /* Operand, if this instruction has one */
659   pCodeFlow *pcflow;           /* flow block to which this instruction belongs */
660   pCodeCSource *cline;         /* C Source from which this instruction was derived */
661
662   unsigned int num_ops;        /* Number of operands (0,1,2 for mid range pics) */
663   unsigned int isModReg:  1;   /* If destination is W or F, then 1==F */
664   unsigned int isBitInst: 1;   /* e.g. BCF */
665   unsigned int isBranch:  1;   /* True if this is a branching instruction */
666   unsigned int isSkip:    1;   /* True if this is a skip instruction */
667   unsigned int isLit:     1;   /* True if this instruction has an literal operand */
668   unsigned int isAccess:   1;   /* True if this instruction has an access RAM operand */
669   unsigned int isFastCall: 1;   /* True if this instruction has a fast call/return mode select operand */
670   unsigned int is2MemOp: 1;     /* True is second operand is a memory operand VR - support for MOVFF */
671   unsigned int is2LitOp: 1;     /* True if instruction takes 2 literal operands VR - support for LFSR */
672
673   PIC_OPCODE inverted_op;      /* Opcode of instruction that's the opposite of this one */
674   unsigned int inCond;   // Input conditions for this instruction
675   unsigned int outCond;  // Output conditions for this instruction
676
677 #define PCI_MAGIC       0x6e12
678   unsigned int pci_magic;       // sanity check for pci initialization
679 } pCodeInstruction;
680
681
682
683 /*************************************************
684     pCodeAsmDir
685 **************************************************/
686
687 typedef struct pCodeAsmDir
688 {
689   pCodeInstruction pci;
690   
691   char *directive;
692   char *arg;
693 } pCodeAsmDir;
694
695
696 /*************************************************
697     pCodeLabel
698 **************************************************/
699
700 typedef struct pCodeLabel
701 {
702
703   pCode  pc;
704
705   char *label;
706   int key;
707   int force;            /* label cannot be optimized out */
708
709 } pCodeLabel;
710
711 /*************************************************
712     pCodeFunction
713 **************************************************/
714
715 typedef struct pCodeFunction
716 {
717
718   pCode  pc;
719
720   char *modname;
721   char *fname;     /* If NULL, then this is the end of
722                       a function. Otherwise, it's the
723                       start and the name is contained
724                       here */
725
726   pBranch *from;       // pCodes that execute before this one
727   pBranch *to;         // pCodes that execute after
728   pBranch *label;      // pCode instructions that have labels
729
730   int  ncalled;    /* Number of times function is called */
731
732   int absblock;    /* hack to emulate a block pCodes in absolute position
733                       but not inside a function */
734   int stackusage;  /* stack positions used in function */
735   
736 } pCodeFunction;
737
738
739 /*************************************************
740     pCodeWild
741 **************************************************/
742
743 typedef struct pCodeWild
744 {
745
746   pCodeInstruction  pci;
747
748   int    id;     /* Index into the wild card array of a peepBlock 
749                   * - this wild card will get expanded into that pCode
750                   *   that is stored at this index */
751
752   /* Conditions on wild pcode instruction */
753   int    mustBeBitSkipInst:1;
754   int    mustNotBeBitSkipInst:1;
755   int    invertBitSkipInst:1;
756
757   pCodeOp *operand;  // Optional operand
758   pCodeOp *label;    // Optional label
759
760 } pCodeWild;
761
762
763 /*************************************************
764     pInfo
765     
766     Here are stored generic informaton
767 *************************************************/
768 typedef struct pCodeInfo
769 {
770   pCodeInstruction pci;
771   
772   INFO_TYPE type;       /* info node type */
773   
774   pCodeOp *oper1;       /* info node arguments */
775 } pCodeInfo;
776   
777
778 /*************************************************
779     pBlock
780
781     Here are PIC program snippets. There's a strong
782     correlation between the eBBlocks and pBlocks.
783     SDCC subdivides a C program into managable chunks.
784     Each chunk becomes a eBBlock and ultimately in the
785     PIC port a pBlock.
786
787 **************************************************/
788
789 typedef struct pBlock
790 {
791   memmap *cmemmap;   /* The snippet is from this memmap */
792   char   dbName;     /* if cmemmap is NULL, then dbName will identify the block */
793   pCode *pcHead;     /* A pointer to the first pCode in a link list of pCodes */
794   pCode *pcTail;     /* A pointer to the last pCode in a link list of pCodes */
795
796   struct pBlock *next;      /* The pBlocks will form a doubly linked list */
797   struct pBlock *prev;
798
799   set *function_entries;    /* dll of functions in this pblock */
800   set *function_exits;
801   set *function_calls;
802   set *tregisters;
803
804   set *FlowTree;
805   unsigned visited:1;       /* set true if traversed in call tree */
806
807   unsigned seq;             /* sequence number of this pBlock */
808
809 } pBlock;
810
811 /*************************************************
812     pFile
813
814     The collection of pBlock program snippets are
815     placed into a linked list that is implemented
816     in the pFile structure.
817
818     The pcode optimizer will parse the pFile.
819
820 **************************************************/
821
822 typedef struct pFile
823 {
824   pBlock *pbHead;     /* A pointer to the first pBlock */
825   pBlock *pbTail;     /* A pointer to the last pBlock */
826
827   pBranch *functions; /* A SLL of functions in this pFile */
828
829 } pFile;
830
831
832
833 /*************************************************
834   pCodeWildBlock
835
836   The pCodeWildBlock object keeps track of the wild
837   variables, operands, and opcodes that exist in
838   a pBlock.
839 **************************************************/
840 typedef struct pCodeWildBlock {
841   pBlock    *pb;
842   struct pCodePeep *pcp;    // pointer back to ... I don't like this...
843
844   int       nvars;          // Number of wildcard registers in target.
845   char    **vars;           // array of pointers to them
846
847   int       nops;           // Number of wildcard operands in target.
848   pCodeOp **wildpCodeOps;   // array of pointers to the pCodeOp's.
849
850   int       nwildpCodes;    // Number of wildcard pCodes in target/replace
851   pCode   **wildpCodes;     // array of pointers to the pCode's.
852
853 } pCodeWildBlock;
854
855 /*************************************************
856   pCodePeep
857
858   The pCodePeep object mimics the peep hole optimizer
859   in the main SDCC src (e.g. SDCCpeeph.c). Essentially
860   there is a target pCode chain and a replacement
861   pCode chain. The target chain is compared to the
862   pCode that is generated by gen.c. If a match is
863   found then the pCode is replaced by the replacement
864   pCode chain.
865 **************************************************/
866 typedef struct pCodePeep {
867   pCodeWildBlock target;     // code we'd like to optimize
868   pCodeWildBlock replace;    // and this is what we'll optimize it with.
869
870   //pBlock *target;
871   //pBlock replace;            // and this is what we'll optimize it with.
872
873
874
875   /* (Note: a wildcard register is a place holder. Any register
876    * can be replaced by the wildcard when the pcode is being 
877    * compared to the target. */
878
879   /* Post Conditions. A post condition is a condition that
880    * must be either true or false before the peep rule is
881    * accepted. For example, a certain rule may be accepted
882    * if and only if the Z-bit is not used as an input to 
883    * the subsequent instructions in a pCode chain.
884    */
885   unsigned int postFalseCond;  
886   unsigned int postTrueCond;
887
888 } pCodePeep;
889
890 /*************************************************
891
892   pCode peep command definitions 
893
894  Here are some special commands that control the
895 way the peep hole optimizer behaves
896
897 **************************************************/
898
899 enum peepCommandTypes{
900   NOTBITSKIP = 0,
901   BITSKIP,
902   INVERTBITSKIP,
903   _LAST_PEEP_COMMAND_
904 };
905
906 /*************************************************
907     peepCommand structure stores the peep commands.
908
909 **************************************************/
910
911 typedef struct peepCommand {
912   int id;
913   char *cmd;
914 } peepCommand;
915
916 /*************************************************
917     pCode Macros
918
919 **************************************************/
920 #define PCODE(x)  ((pCode *)(x))
921 #define PCI(x)    ((pCodeInstruction *)(x))
922 #define PCL(x)    ((pCodeLabel *)(x))
923 #define PCF(x)    ((pCodeFunction *)(x))
924 #define PCFL(x)   ((pCodeFlow *)(x))
925 #define PCFLINK(x)((pCodeFlowLink *)(x))
926 #define PCW(x)    ((pCodeWild *)(x))
927 #define PCCS(x)   ((pCodeCSource *)(x))
928 #define PCAD(x)   ((pCodeAsmDir *)(x))
929 #define PCINF(x)  ((pCodeInfo *)(x))
930
931 #define PCOP(x)   ((pCodeOp *)(x))
932 //#define PCOB(x)   ((pCodeOpBit *)(x))
933 #define PCOL(x)   ((pCodeOpLit *)(x))
934 #define PCOI(x)   ((pCodeOpImmd *)(x))
935 #define PCOLAB(x) ((pCodeOpLabel *)(x))
936 #define PCOR(x)   ((pCodeOpReg *)(x))
937 #define PCOR2(x)  ((pCodeOpReg2 *)(x))
938 #define PCORB(x)  ((pCodeOpRegBit *)(x))
939 #define PCOO(x)   ((pCodeOpOpt *)(x))
940 #define PCOLR(x)  ((pCodeOpLocalReg *)(x))
941 #define PCOW(x)   ((pCodeOpWild *)(x))
942 #define PCOW2(x)  (PCOW(PCOW(x)->pcop2))
943 #define PBR(x)    ((pBranch *)(x))
944
945 #define PCWB(x)   ((pCodeWildBlock *)(x))
946
947
948 /*
949   macros for checking pCode types
950 */
951 #define isPCI(x)        ((PCODE(x)->type == PC_OPCODE))
952 #define isPCI_BRANCH(x) ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isBranch)
953 #define isPCI_SKIP(x)   ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isSkip)
954 #define isPCI_LIT(x)    ((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isLit)
955 #define isPCI_BITSKIP(x)((PCODE(x)->type == PC_OPCODE) &&  PCI(x)->isSkip && PCI(x)->isBitInst)
956 #define isPCFL(x)       ((PCODE(x)->type == PC_FLOW))
957 #define isPCF(x)        ((PCODE(x)->type == PC_FUNCTION))
958 #define isPCL(x)        ((PCODE(x)->type == PC_LABEL))
959 #define isPCW(x)        ((PCODE(x)->type == PC_WILD))
960 #define isPCCS(x)       ((PCODE(x)->type == PC_CSOURCE))
961 #define isASMDIR(x)     ((PCODE(x)->type == PC_ASMDIR))
962
963 #define isCALL(x)       ((isPCI(x)) && (PCI(x)->op == POC_CALL))
964 #define isSTATUS_REG(r) ((r)->pc_type == PO_STATUS)
965 #define isBSR_REG(r)    ((r)->pc_type == PO_BSR)
966 #define isACCESS_BANK(r)        (r->accessBank)
967
968
969
970 #define isPCOLAB(x)     ((PCOP(x)->type) == PO_LABEL)
971
972 /*-----------------------------------------------------------------*
973  * pCode functions.
974  *-----------------------------------------------------------------*/
975
976 pCode *pic16_newpCode (PIC_OPCODE op, pCodeOp *pcop); // Create a new pCode given an operand
977 pCode *pic16_newpCodeCharP(char *cP);              // Create a new pCode given a char *
978 pCode *pic16_newpCodeInlineP(char *cP);            // Create a new pCode given a char *
979 pCode *pic16_newpCodeFunction(char *g, char *f);   // Create a new function
980 pCode *pic16_newpCodeLabel(char *name,int key);    // Create a new label given a key
981 pCode *pic16_newpCodeLabelFORCE(char *name, int key); // Same as newpCodeLabel but label cannot be optimized out
982 pCode *pic16_newpCodeCSource(int ln, char *f, char *l); // Create a new symbol line 
983 pBlock *pic16_newpCodeChain(memmap *cm,char c, pCode *pc); // Create a new pBlock
984 void pic16_printpBlock(FILE *of, pBlock *pb);      // Write a pBlock to a file
985 void pic16_addpCode2pBlock(pBlock *pb, pCode *pc); // Add a pCode to a pBlock
986 void pic16_addpBlock(pBlock *pb);                  // Add a pBlock to a pFile
987 void pic16_copypCode(FILE *of, char dbName);       // Write all pBlocks with dbName to *of
988 void pic16_movepBlock2Head(char dbName);           // move pBlocks around
989 void pic16_AnalyzepCode(char dbName);
990 void pic16_OptimizeLocalRegs(void);
991 void pic16_AssignRegBanks(void);
992 void pic16_printCallTree(FILE *of);
993 void pCodePeepInit(void);
994 void pic16_pBlockConvert2ISR(pBlock *pb);
995 void pic16_pBlockConvert2Absolute(pBlock *pb);
996 void pic16_initDB(void);
997 void pic16_emitDB(char c, char ptype, void *p);           // Add DB directives to a pBlock
998 void pic16_emitDS(char *s, char ptype, void *p);
999 void pic16_flushDB(char ptype, void *p);                          // Add pending DB data to a pBlock
1000
1001 pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...); 
1002
1003 pCodeOp *pic16_newpCodeOpLabel(char *name, int key);
1004 pCodeOp *pic16_newpCodeOpImmd(char *name, int offset, int index, int code_space);
1005 pCodeOp *pic16_newpCodeOpLit(int lit);
1006 pCodeOp *pic16_newpCodeOpLit2(int lit, pCodeOp *arg2);
1007 pCodeOp *pic16_newpCodeOpBit(char *name, int bit,int inBitSpace, PIC_OPTYPE subt);
1008 pCodeOp *pic16_newpCodeOpRegFromStr(char *name);
1009 pCodeOp *pic16_newpCodeOp(char *name, PIC_OPTYPE p);
1010 pCodeOp *pic16_pCodeOpCopy(pCodeOp *pcop);
1011
1012 pCode *pic16_newpCodeInfo(INFO_TYPE type, pCodeOp *pcop);
1013 pCodeOp *pic16_newpCodeOpOpt(OPT_TYPE type, char *key);
1014 pCodeOp *pic16_newpCodeOpLocalRegs(LR_TYPE type);
1015
1016 pCode * pic16_findNextInstruction(pCode *pci);
1017 pCode * pic16_findNextpCode(pCode *pc, PC_TYPE pct);
1018 int pic16_isPCinFlow(pCode *pc, pCode *pcflow);
1019 struct regs * pic16_getRegFromInstruction(pCode *pc);
1020 struct regs * pic16_getRegFromInstruction2(pCode *pc);
1021
1022 extern void pic16_pcode_test(void);
1023 extern int pic16_debug_verbose;
1024 extern int pic16_pcode_verbose;
1025
1026 #ifndef debugf
1027 //#define debugf(frm, rest...)       _debugf(__FILE__, __LINE__, frm, rest)
1028 #define debugf(frm, rest)       _debug(__FILE__, __LINE__, frm, rest)
1029 #endif
1030
1031 extern void _debugf(char *f, int l, char *frm, ...);
1032
1033
1034 /*-----------------------------------------------------------------*
1035  * pCode objects.
1036  *-----------------------------------------------------------------*/
1037
1038 extern pCodeOpReg pic16_pc_status;
1039 extern pCodeOpReg pic16_pc_intcon;
1040 extern pCodeOpReg pic16_pc_pcl;
1041 extern pCodeOpReg pic16_pc_pclath;
1042 extern pCodeOpReg pic16_pc_pclatu; // patch 14
1043 extern pCodeOpReg pic16_pc_wreg;
1044 extern pCodeOpReg pic16_pc_tosl; // patch 14
1045 extern pCodeOpReg pic16_pc_tosh; // patch 14
1046 extern pCodeOpReg pic16_pc_tosu; // patch 14
1047 extern pCodeOpReg pic16_pc_tblptrl; // patch 15
1048 extern pCodeOpReg pic16_pc_tblptrh; //
1049 extern pCodeOpReg pic16_pc_tblptru; //
1050 extern pCodeOpReg pic16_pc_tablat;  // patch 15
1051 extern pCodeOpReg pic16_pc_bsr;
1052 extern pCodeOpReg pic16_pc_fsr0;
1053 extern pCodeOpReg pic16_pc_fsr0l;
1054 extern pCodeOpReg pic16_pc_fsr0h;
1055 extern pCodeOpReg pic16_pc_fsr1l;
1056 extern pCodeOpReg pic16_pc_fsr1h;
1057 extern pCodeOpReg pic16_pc_fsr2l;
1058 extern pCodeOpReg pic16_pc_fsr2h;
1059 extern pCodeOpReg pic16_pc_indf0;
1060 extern pCodeOpReg pic16_pc_postinc0;
1061 extern pCodeOpReg pic16_pc_postdec0;
1062 extern pCodeOpReg pic16_pc_preinc0;
1063 extern pCodeOpReg pic16_pc_plusw0;
1064 extern pCodeOpReg pic16_pc_indf1;
1065 extern pCodeOpReg pic16_pc_postinc1;
1066 extern pCodeOpReg pic16_pc_postdec1;
1067 extern pCodeOpReg pic16_pc_preinc1;
1068 extern pCodeOpReg pic16_pc_plusw1;
1069 extern pCodeOpReg pic16_pc_indf2;
1070 extern pCodeOpReg pic16_pc_postinc2;
1071 extern pCodeOpReg pic16_pc_postdec2;
1072 extern pCodeOpReg pic16_pc_preinc2;
1073 extern pCodeOpReg pic16_pc_plusw2;
1074 extern pCodeOpReg pic16_pc_prodl;
1075 extern pCodeOpReg pic16_pc_prodh;
1076
1077 extern pCodeOpReg pic16_pc_eecon1;
1078 extern pCodeOpReg pic16_pc_eecon2;
1079 extern pCodeOpReg pic16_pc_eedata;
1080 extern pCodeOpReg pic16_pc_eeadr;
1081
1082 extern pCodeOpReg pic16_pc_kzero;
1083 extern pCodeOpReg pic16_pc_wsave;     /* wsave and ssave are used to save W and the Status */
1084 extern pCodeOpReg pic16_pc_ssave;     /* registers during an interrupt */
1085
1086 extern pCodeOpReg pic16_pc_gpsimio;
1087 extern pCodeOpReg pic16_pc_gpsimio2;
1088
1089 #endif // __PCODE_H__