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