1 /*-------------------------------------------------------------------------
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
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
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.
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.
21 -------------------------------------------------------------------------*/
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:
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:
52 movf t1,w ; Can't remove this movf
58 movf t1,w ; This movf can be removed
59 xorwf t2,w ; since xorwf will over write Z
69 /***********************************************************************
72 * The DFPRINTF macro will call fprintf if PCODE_DEBUG is defined.
73 * The macro is used like:
75 * DPRINTF(("%s #%d\n","test", 1));
77 * The double parenthesis (()) are necessary
79 ***********************************************************************/
83 #define DFPRINTF(args) (fprintf args)
85 #define DFPRINTF(args) ;
89 /***********************************************************************
90 * PIC status bits - this will move into device dependent headers
91 ***********************************************************************/
97 #define PIC_IRP_BIT 7 /* Indirect register page select */
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 */
111 /***********************************************************************
112 * PIC bank definitions
113 ***********************************************************************/
114 #define PIC_BANK_FIRST 0
115 #define PIC_BANK_LAST 0xf
118 /***********************************************************************
120 ***********************************************************************/
126 /***********************************************************************
128 * PIC_OPTYPE - Operand types that are specific to the PIC architecture
130 * If a PIC assembly instruction has an operand then here is where we
131 * associate a type to it. For example,
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.
139 ***********************************************************************/
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
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)
168 PO_WILD // Wild card operand in peep optimizer
172 /***********************************************************************
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.
179 ***********************************************************************/
183 POC_WILD=-1, /* Wild card - used in the pCode peep hole optimizer
184 * to represent ANY pic opcode */
267 POC_TRIS, // To be removed
275 /***********************************************************************
276 * PC_TYPE - pCode Types
277 ***********************************************************************/
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_BAD /* Mark the pCode object as being bad */
293 /************************************************/
294 /*************** Structures ********************/
295 /************************************************/
296 /* These are here as forward references - the
297 * full definition of these are below */
299 struct pCodeWildBlock;
300 struct pCodeRegLives;
302 /*************************************************
305 The first step in optimizing pCode is determining
306 the program flow. This information is stored in
307 single-linked lists in the for of 'from' and 'to'
308 objects with in a pcode. For example, most instructions
309 don't involve any branching. So their from branch
310 points to the pCode immediately preceding them and
311 their 'to' branch points to the pcode immediately
312 following them. A skip instruction is an example of
313 a pcode that has multiple (in this case two) elements
314 in the 'to' branch. A 'label' pcode is an where there
315 may be multiple 'from' branches.
316 *************************************************/
318 typedef struct pBranch
320 struct pCode *pc; // Next pCode in a branch
321 struct pBranch *next; /* If more than one branch
322 * the next one is here */
326 /*************************************************
329 pCode Operand structure.
330 For those assembly instructions that have arguments,
331 the pCode will have a pCodeOp in which the argument
332 can be stored. For example
336 'some_register' will be stored/referenced in a pCodeOp
338 *************************************************/
340 typedef struct pCodeOp
347 typedef struct pCodeOpBit
351 unsigned int inBitSpace: 1; /* True if in bit space, else
352 just a bit of a register */
355 typedef struct pCodeOpLit
361 typedef struct pCodeOpImmd
364 int offset; /* low,med, or high byte of immediat value */
365 int index; /* add this to the immediate value */
366 unsigned _const:1; /* is in code space */
368 int rIdx; /* If this immd points to a register */
369 struct regs *r; /* then this is the reg. */
373 typedef struct pCodeOpLabel
379 typedef struct pCodeOpReg
381 pCodeOp pcop; // Can be either GPR or SFR
382 int rIdx; // Index into the register table
384 int instance; // byte # of Multi-byte registers
388 typedef struct pCodeOpRegBit
390 pCodeOpReg pcor; // The Register containing this bit
391 int bit; // 0-7 bit number.
392 PIC_OPTYPE subtype; // The type of this register.
393 unsigned int inBitSpace: 1; /* True if in bit space, else
394 just a bit of a register */
398 typedef struct pCodeOpWild
402 struct pCodeWildBlock *pcwb;
404 int id; /* index into an array of char *'s that will match
405 * the wild card. The array is in *pcp. */
406 pCodeOp *subtype; /* Pointer to the Operand type into which this wild
407 * card will be expanded */
408 pCodeOp *matched; /* When a wild matches, we'll store a pointer to the
409 * opcode we matched */
414 /*************************************************
417 Here is the basic build block of a PIC instruction.
418 Each pic instruction will get allocated a pCode.
419 A linked list of pCodes makes a program.
421 **************************************************/
427 struct pCode *prev; // The pCode objects are linked together
428 struct pCode *next; // in doubly linked lists.
430 int seq; // sequence number
432 struct pBlock *pb; // The pBlock that contains this pCode.
434 /* "virtual functions"
435 * The pCode structure is like a base class
436 * in C++. The subsequent structures that "inherit"
437 * the pCode structure will initialize these function
438 * pointers to something useful */
439 // void (*analyze) (struct pCode *_this);
440 void (*destruct)(struct pCode *_this);
441 void (*print) (FILE *of,struct pCode *_this);
446 /*************************************************
448 **************************************************/
450 typedef struct pCodeComment
459 /*************************************************
461 **************************************************/
463 typedef struct pCodeCSource
475 /*************************************************
478 The Flow object is used as marker to separate
479 the assembly code into contiguous chunks. In other
480 words, everytime an instruction cause or potentially
481 causes a branch, a Flow object will be inserted into
482 the pCode chain to mark the beginning of the next
485 **************************************************/
487 typedef struct pCodeFlow
492 pCode *end; /* Last pCode in this flow. Note that
493 the first pCode is pc.next */
495 /* set **uses; * map the pCode instruction inCond and outCond conditions
496 * in this array of set's. The reason we allocate an
497 * array of pointers instead of declaring each type of
498 * usage is because there are port dependent usage definitions */
499 //int nuses; /* number of uses sets */
501 set *from; /* flow blocks that can send control to this flow block */
502 set *to; /* flow blocks to which this one can send control */
503 struct pCodeFlow *ancestor; /* The most immediate "single" pCodeFlow object that
504 * executes prior to this one. In many cases, this
505 * will be just the previous */
507 int inCond; /* Input conditions - stuff assumed defined at entry */
508 int outCond; /* Output conditions - stuff modified by flow block */
510 int firstBank; /* The first and last bank flags are the first and last */
511 int lastBank; /* register banks used within one flow object */
516 set *registers;/* Registers used in this flow */
520 /*************************************************
523 The Flow Link object is used to record information
524 about how consecutive excutive Flow objects are related.
525 The pCodeFlow objects demarcate the pCodeInstructions
526 into contiguous chunks. The FlowLink records conflicts
527 in the discontinuities. For example, if one Flow object
528 references a register in bank 0 and the next Flow object
529 references a register in bank 1, then there is a discontinuity
530 in the banking registers.
533 typedef struct pCodeFlowLink
535 pCodeFlow *pcflow; /* pointer to linked pCodeFlow object */
537 int bank_conflict; /* records bank conflicts */
541 /*************************************************
544 Here we describe all the facets of a PIC instruction
545 (expansion for the 18cxxx is also provided).
547 **************************************************/
549 typedef struct pCodeInstruction
554 PIC_OPCODE op; // The opcode of the instruction.
556 char const * const mnemonic; // Pointer to mnemonic string
558 pBranch *from; // pCodes that execute before this one
559 pBranch *to; // pCodes that execute after
560 pBranch *label; // pCode instructions that have labels
562 pCodeOp *pcop; /* Operand, if this instruction has one */
563 pCodeFlow *pcflow; /* flow block to which this instruction belongs */
564 pCodeCSource *cline; /* C Source from which this instruction was derived */
566 unsigned int num_ops; /* Number of operands (0,1,2 for mid range pics) */
567 unsigned int isModReg: 1; /* If destination is W or F, then 1==F */
568 unsigned int isBitInst: 1; /* e.g. BCF */
569 unsigned int isBranch: 1; /* True if this is a branching instruction */
570 unsigned int isSkip: 1; /* True if this is a skip instruction */
571 unsigned int isLit: 1; /* True if this instruction has an literal operand */
572 unsigned int isAccess: 1; /* True if this instruction has an access RAM operand */
573 unsigned int isFastCall: 1; /* True if this instruction has a fast call/return mode select operand */
575 PIC_OPCODE inverted_op; /* Opcode of instruction that's the opposite of this one */
576 unsigned int inCond; // Input conditions for this instruction
577 unsigned int outCond; // Output conditions for this instruction
582 /*************************************************
584 **************************************************/
586 typedef struct pCodeLabel
596 /*************************************************
598 **************************************************/
600 typedef struct pCodeFunction
606 char *fname; /* If NULL, then this is the end of
607 a function. Otherwise, it's the
608 start and the name is contained
611 pBranch *from; // pCodes that execute before this one
612 pBranch *to; // pCodes that execute after
613 pBranch *label; // pCode instructions that have labels
615 int ncalled; /* Number of times function is called */
620 /*************************************************
622 **************************************************/
624 typedef struct pCodeWild
627 pCodeInstruction pci;
629 int id; /* Index into the wild card array of a peepBlock
630 * - this wild card will get expanded into that pCode
631 * that is stored at this index */
633 /* Conditions on wild pcode instruction */
634 int mustBeBitSkipInst:1;
635 int mustNotBeBitSkipInst:1;
636 int invertBitSkipInst:1;
638 pCodeOp *operand; // Optional operand
639 pCodeOp *label; // Optional label
643 /*************************************************
646 Here are PIC program snippets. There's a strong
647 correlation between the eBBlocks and pBlocks.
648 SDCC subdivides a C program into managable chunks.
649 Each chunk becomes a eBBlock and ultimately in the
652 **************************************************/
654 typedef struct pBlock
656 memmap *cmemmap; /* The snippet is from this memmap */
657 char dbName; /* if cmemmap is NULL, then dbName will identify the block */
658 pCode *pcHead; /* A pointer to the first pCode in a link list of pCodes */
659 pCode *pcTail; /* A pointer to the last pCode in a link list of pCodes */
661 struct pBlock *next; /* The pBlocks will form a doubly linked list */
664 set *function_entries; /* dll of functions in this pblock */
670 unsigned visited:1; /* set true if traversed in call tree */
672 unsigned seq; /* sequence number of this pBlock */
676 /*************************************************
679 The collection of pBlock program snippets are
680 placed into a linked list that is implemented
681 in the pFile structure.
683 The pcode optimizer will parse the pFile.
685 **************************************************/
689 pBlock *pbHead; /* A pointer to the first pBlock */
690 pBlock *pbTail; /* A pointer to the last pBlock */
692 pBranch *functions; /* A SLL of functions in this pFile */
698 /*************************************************
701 The pCodeWildBlock object keeps track of the wild
702 variables, operands, and opcodes that exist in
704 **************************************************/
705 typedef struct pCodeWildBlock {
707 struct pCodePeep *pcp; // pointer back to ... I don't like this...
709 int nvars; // Number of wildcard registers in target.
710 char **vars; // array of pointers to them
712 int nops; // Number of wildcard operands in target.
713 pCodeOp **wildpCodeOps; // array of pointers to the pCodeOp's.
715 int nwildpCodes; // Number of wildcard pCodes in target/replace
716 pCode **wildpCodes; // array of pointers to the pCode's.
720 /*************************************************
723 The pCodePeep object mimics the peep hole optimizer
724 in the main SDCC src (e.g. SDCCpeeph.c). Essentially
725 there is a target pCode chain and a replacement
726 pCode chain. The target chain is compared to the
727 pCode that is generated by gen.c. If a match is
728 found then the pCode is replaced by the replacement
730 **************************************************/
731 typedef struct pCodePeep {
732 pCodeWildBlock target; // code we'd like to optimize
733 pCodeWildBlock replace; // and this is what we'll optimize it with.
736 //pBlock replace; // and this is what we'll optimize it with.
740 /* (Note: a wildcard register is a place holder. Any register
741 * can be replaced by the wildcard when the pcode is being
742 * compared to the target. */
744 /* Post Conditions. A post condition is a condition that
745 * must be either true or false before the peep rule is
746 * accepted. For example, a certain rule may be accepted
747 * if and only if the Z-bit is not used as an input to
748 * the subsequent instructions in a pCode chain.
750 unsigned int postFalseCond;
751 unsigned int postTrueCond;
755 /*************************************************
757 pCode peep command definitions
759 Here are some special commands that control the
760 way the peep hole optimizer behaves
762 **************************************************/
764 enum peepCommandTypes{
771 /*************************************************
772 peepCommand structure stores the peep commands.
774 **************************************************/
776 typedef struct peepCommand {
781 /*************************************************
784 **************************************************/
785 #define PCODE(x) ((pCode *)(x))
786 #define PCI(x) ((pCodeInstruction *)(x))
787 #define PCL(x) ((pCodeLabel *)(x))
788 #define PCF(x) ((pCodeFunction *)(x))
789 #define PCFL(x) ((pCodeFlow *)(x))
790 #define PCFLINK(x)((pCodeFlowLink *)(x))
791 #define PCW(x) ((pCodeWild *)(x))
792 #define PCCS(x) ((pCodeCSource *)(x))
794 #define PCOP(x) ((pCodeOp *)(x))
795 //#define PCOB(x) ((pCodeOpBit *)(x))
796 #define PCOL(x) ((pCodeOpLit *)(x))
797 #define PCOI(x) ((pCodeOpImmd *)(x))
798 #define PCOLAB(x) ((pCodeOpLabel *)(x))
799 #define PCOR(x) ((pCodeOpReg *)(x))
800 #define PCORB(x) ((pCodeOpRegBit *)(x))
801 #define PCOW(x) ((pCodeOpWild *)(x))
803 #define PBR(x) ((pBranch *)(x))
805 #define PCWB(x) ((pCodeWildBlock *)(x))
809 macros for checking pCode types
811 #define isPCI(x) ((PCODE(x)->type == PC_OPCODE))
812 #define isPCI_BRANCH(x) ((PCODE(x)->type == PC_OPCODE) && PCI(x)->isBranch)
813 #define isPCI_SKIP(x) ((PCODE(x)->type == PC_OPCODE) && PCI(x)->isSkip)
814 #define isPCI_LIT(x) ((PCODE(x)->type == PC_OPCODE) && PCI(x)->isLit)
815 #define isPCI_BITSKIP(x)((PCODE(x)->type == PC_OPCODE) && PCI(x)->isSkip && PCI(x)->isBitInst)
816 #define isPCFL(x) ((PCODE(x)->type == PC_FLOW))
817 #define isPCF(x) ((PCODE(x)->type == PC_FUNCTION))
818 #define isPCL(x) ((PCODE(x)->type == PC_LABEL))
819 #define isPCW(x) ((PCODE(x)->type == PC_WILD))
820 #define isPCCS(x) ((PCODE(x)->type == PC_CSOURCE))
822 #define isCALL(x) ((isPCI(x)) && (PCI(x)->op == POC_CALL))
823 #define isSTATUS_REG(r) ((r)->pc_type == PO_STATUS)
824 #define isBSR_REG(r) ((r)->pc_type == PO_BSR)
826 #define isACCESS_LOW(r) ((pic16_finalMapping[REG_ADDR(r)].bank == \
827 PIC_BANK_FIRST) && (REG_ADDR(r) < 0x80))
828 #define isACCESS_HI(r) (pic16_finalMapping[REG_ADDR(r)].bank == PIC_BANK_LAST)
829 #define isACCESS_BANK(r)(isACCESS_LOW(r) || isACCESS_HI(r))
831 #define isPCOLAB(x) ((PCOP(x)->type) == PO_LABEL)
833 /*-----------------------------------------------------------------*
835 *-----------------------------------------------------------------*/
837 pCode *pic16_newpCode (PIC_OPCODE op, pCodeOp *pcop); // Create a new pCode given an operand
838 pCode *pic16_newpCodeCharP(char *cP); // Create a new pCode given a char *
839 pCode *pic16_newpCodeInlineP(char *cP); // Create a new pCode given a char *
840 pCode *pic16_newpCodeFunction(char *g, char *f); // Create a new function
841 pCode *pic16_newpCodeLabel(char *name,int key); // Create a new label given a key
842 pCode *pic16_newpCodeCSource(int ln, char *f, char *l); // Create a new symbol line
843 pBlock *pic16_newpCodeChain(memmap *cm,char c, pCode *pc); // Create a new pBlock
844 void pic16_printpBlock(FILE *of, pBlock *pb); // Write a pBlock to a file
845 void pic16_addpCode2pBlock(pBlock *pb, pCode *pc); // Add a pCode to a pBlock
846 void pic16_addpBlock(pBlock *pb); // Add a pBlock to a pFile
847 void pic16_copypCode(FILE *of, char dbName); // Write all pBlocks with dbName to *of
848 void pic16_movepBlock2Head(char dbName); // move pBlocks around
849 void pic16_AnalyzepCode(char dbName);
850 void pic16_printCallTree(FILE *of);
851 void pCodePeepInit(void);
852 void pic16_pBlockConvert2ISR(pBlock *pb);
854 pCodeOp *pic16_newpCodeOpLabel(char *name, int key);
855 pCodeOp *pic16_newpCodeOpImmd(char *name, int offset, int index, int code_space);
856 pCodeOp *pic16_newpCodeOpLit(int lit);
857 pCodeOp *pic16_newpCodeOpBit(char *name, int bit,int inBitSpace);
858 pCodeOp *pic16_newpCodeOpRegFromStr(char *name);
859 pCodeOp *pic16_newpCodeOp(char *name, PIC_OPTYPE p);
860 pCodeOp *pic16_pCodeOpCopy(pCodeOp *pcop);
862 pCode * pic16_findNextInstruction(pCode *pci);
863 pCode * pic16_findNextpCode(pCode *pc, PC_TYPE pct);
864 int pic16_isPCinFlow(pCode *pc, pCode *pcflow);
865 struct regs * pic16_getRegFromInstruction(pCode *pc);
867 extern void pic16_pcode_test(void);
869 /*-----------------------------------------------------------------*
871 *-----------------------------------------------------------------*/
873 extern pCodeOpReg pic16_pc_status;
874 extern pCodeOpReg pic16_pc_intcon;
875 extern pCodeOpReg pic16_pc_indf0;
876 extern pCodeOpReg pic16_pc_fsr0;
877 extern pCodeOpReg pic16_pc_pcl;
878 extern pCodeOpReg pic16_pc_pclath;
879 extern pCodeOpReg pic16_pc_wreg;
880 extern pCodeOpReg pic16_pc_kzero;
881 extern pCodeOpReg pic16_pc_wsave; /* wsave and ssave are used to save W and the Status */
882 extern pCodeOpReg pic16_pc_ssave; /* registers during an interrupt */
885 #endif // __PCODE_H__