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