extern char *pic16_aopGet (struct asmop *aop, int offset, bool bit16, bool dname);
#if defined(__BORLANDC__) || defined(_MSC_VER)
-#define STRCASECMP stricmp
#define inline
-#else
-#define STRCASECMP strcasecmp
#endif
#define DUMP_DF_GRAPHS 0
extern void pic16_RegsUnMapLiveRanges(void);
extern void pic16_BuildFlowTree(pBlock *pb);
extern void pic16_pCodeRegOptimizeRegUsage(int level);
-extern int pic16_picIsInitialized(void);
extern void SAFE_snprintf(char **str, size_t *size, const char *format, ...);
-extern int mnem2key(char const *mnem);
+extern int mnem2key(unsigned char const *mnem);
/****************************************************************/
/* Forward declarations */
#endif
extern set *externs;
-extern void pic16_initStack(int base_address, int size);
extern regs *pic16_allocProcessorRegister(int rIdx, char * name, short po_type, int alias);
extern regs *pic16_allocInternalRegister(int rIdx, char * name, short po_type, int alias);
-extern void pic16_init_pic(char *);
void pic16_pCodeInitRegisters(void)
{
initialized = 1;
-// pic16_initStack(0xfff, 8);
- pic16_init_pic(port->processor);
-
pic16_pc_status.r = pic16_allocProcessorRegister(IDX_STATUS,"STATUS", PO_STATUS, 0x80);
pic16_pc_pcl.r = pic16_allocProcessorRegister(IDX_PCL,"PCL", PO_PCL, 0x80);
pic16_pc_pclath.r = pic16_allocProcessorRegister(IDX_PCLATH,"PCLATH", PO_PCLATH, 0x80);
/* */
/*-----------------------------------------------------------------*/
-int mnem2key(char const *mnem)
+int mnem2key(unsigned char const *mnem)
{
int key = 0;
for(i=0; i<MAX_PIC16MNEMONICS; i++)
if(pic16Mnemonics[i])
- hTabAddItem(&pic16MnemonicsHash, mnem2key(pic16Mnemonics[i]->mnemonic), pic16Mnemonics[i]);
+ hTabAddItem(&pic16MnemonicsHash, mnem2key((const unsigned char *)pic16Mnemonics[i]->mnemonic), pic16Mnemonics[i]);
pci = hTabFirstItem(pic16MnemonicsHash, &key);
while(pci) {
{
pCodeInstruction *pci;
- int key = mnem2key(mnem);
+ int key = mnem2key((unsigned char *)mnem);
if(!mnemonics_initialized)
pic16initMnemonics();
i = 0;
do {
hTabAddItem(&pic16pCodePeepCommandsHash,
- mnem2key(peepCommands[i].cmd), &peepCommands[i]);
+ mnem2key((const unsigned char *)peepCommands[i].cmd), &peepCommands[i]);
i++;
} while (peepCommands[i].cmd);
{
peepCommand *pcmd;
- int key = mnem2key(cmd);
+ int key = mnem2key((unsigned char *)cmd);
pcmd = hTabFirstItemWK(pic16pCodePeepCommandsHash, key);
return 0;
}
+
/*-----------------------------------------------------------------*/
/* pic16_newpCode - create and return a newly initialized pCode */
/* */
/* pic16_newpCodeCSource - create a new pCode Source Symbol */
/*-----------------------------------------------------------------*/
-pCode *pic16_newpCodeCSource(int ln, char *f, char *l)
+pCode *pic16_newpCodeCSource(int ln, const char *f, const char *l)
{
pCodeCSource *pccs;
if(asdir && *asdir) {
- while(isspace(*asdir))asdir++; // strip any white space from the beginning
+ while(isspace((unsigned char)*asdir))asdir++; // strip any white space from the beginning
pcad->directive = Safe_strdup( asdir );
}
va_end(ap);
- while(isspace(*lbp))lbp++;
+ while(isspace((unsigned char)*lbp))lbp++;
if(lbp && *lbp)
pcad->arg = Safe_strdup( lbp );
return pcop;
}
+/* Allow for 12 bit literals, required for LFSR */
+pCodeOp *pic16_newpCodeOpLit12(int lit)
+{
+ char *s = buffer;
+ pCodeOp *pcop;
+
+
+ pcop = Safe_calloc(1,sizeof(pCodeOpLit) );
+ pcop->type = PO_LITERAL;
+
+ pcop->name = NULL;
+ //if(lit>=0)
+ sprintf(s,"0x%03x", ((unsigned int)lit) & 0x0fff);
+ //else
+ // sprintf(s, "%i", lit);
+
+ if(s)
+ pcop->name = Safe_strdup(s);
+
+ ((pCodeOpLit *)pcop)->lit = lit;
+
+ return pcop;
+}
+
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
pCodeOp *pic16_newpCodeOpLit2(int lit, pCodeOp *arg2)
// fprintf(stderr,"%s %s %d\n",__FUNCTION__,name,offset);
} else {
pcop->name = NULL;
+ PCOI(pcop)->rIdx = -1;
}
PCOI(pcop)->index = index;
pCodeOp *pic16_newpCodeOpReg(int rIdx)
{
pCodeOp *pcop;
+ regs *r;
pcop = Safe_calloc(1,sizeof(pCodeOpReg) );
pcop->name = NULL;
if(rIdx >= 0) {
- PCOR(pcop)->rIdx = rIdx;
- PCOR(pcop)->r = pic16_regWithIdx(rIdx);
+ r = pic16_regWithIdx(rIdx);
+ if(!r)
+ r = pic16_allocWithIdx(rIdx);
} else {
- PCOR(pcop)->r = pic16_findFreeReg(REG_GPR);
+ r = pic16_findFreeReg(REG_GPR);
- if(PCOR(pcop)->r)
- PCOR(pcop)->rIdx = PCOR(pcop)->r->rIdx;
- else {
+ if(!r) {
fprintf(stderr, "%s:%d Could not find a free GPR register\n",
__FUNCTION__, __LINE__);
- exit(-1);
+ exit(EXIT_FAILURE);
}
}
+ PCOR(pcop)->rIdx = rIdx;
+ PCOR(pcop)->r = r;
pcop->type = PCOR(pcop)->r->pc_type;
return pcop;
else
pcop = pic16_newpCodeOpReg(-1);
break;
+
+ case PO_TWO_OPS:
+ assert( !"Cannot create PO_TWO_OPS from string!" );
+ pcop = NULL;
+ break;
default:
pcop = Safe_calloc(1,sizeof(pCodeOp) );
return pcop;
}
-#define DB_ITEMS_PER_LINE 8
+pCodeOp *pic16_newpCodeOp2(pCodeOp *src, pCodeOp *dst)
+{
+ pCodeOp2 *pcop2 = Safe_calloc(1, sizeof(pCodeOp2));
+ pcop2->pcop.type = PO_TWO_OPS;
+ pcop2->pcopL = src;
+ pcop2->pcopR = dst;
+ return PCOP(pcop2);
+}
+
+/* This is a multiple of two as gpasm pads DB directives to even length,
+ * thus the data would be interleaved with \0 bytes...
+ * This is a multiple of three in order to have arrays of 3-byte pointers
+ * continuously in memory (without 0-padding at the lines' end).
+ * This is rather 12 than 6 in order not to split up 4-byte data types
+ * in arrays right in the middle of a 4-byte word. */
+#define DB_ITEMS_PER_LINE 12
typedef struct DBdata
{
int count;
- char buffer[256];
+ char buffer[512];
} DBdata;
struct DBdata DBd;
/*-----------------------------------------------------------------*/
/* Add "DB" directives to a pBlock */
/*-----------------------------------------------------------------*/
-void pic16_emitDB(char c, char ptype, void *p)
+void pic16_emitDB(int c, char ptype, void *p)
{
int l;
// fprintf(stderr, "%s:%d DBbuffer: '%s'\n", __FILE__, __LINE__, DBd.buffer);
DBd.count++; //=strlen(s);
- if (DBd.count>=16)
+ if (DBd.count>=DB_ITEMS_PER_LINE)
pic16_flushDB(ptype, p);
}
void pic16_pCodeConstString(char *name, char *value)
{
pBlock *pb;
-
- // fprintf(stderr, " %s %s %s\n",__FUNCTION__,name,value);
+ char *item;
+ static set *emittedSymbols = NULL;
if(!name || !value)
return;
+ /* keep track of emitted symbols to avoid multiple definition of str_<nr> */
+ if (emittedSymbols) {
+ /* scan set for name */
+ for (item = setFirstItem (emittedSymbols); item; item = setNextItem (emittedSymbols))
+ {
+ if (!strcmp (item,name)) {
+ //fprintf (stderr, "%s already emitted\n", name);
+ return;
+ } // if
+ } // for
+ } // if
+ addSet (&emittedSymbols, Safe_strdup (name));
+
+ //fprintf(stderr, " %s %s %s\n",__FUNCTION__,name,value);
+
pb = pic16_newpCodeChain(NULL, 'P',pic16_newpCodeCharP("; Starting pCode block"));
pic16_addpBlock(pb);
}
if(pcop) {
+
switch(pcop->type) {
case PO_W:
case PO_WREG:
case PO_FSR0:
if(use_buffer) {
SAFE_snprintf(&buffer,&size,"%s",PCOR(pcop)->r->name);
- return buffer;
+ return (buffer);
}
- return PCOR(pcop)->r->name;
+ return (PCOR(pcop)->r->name);
break;
case PO_GPR_TEMP:
r = pic16_regWithIdx(PCOR(pcop)->r->rIdx);
if(use_buffer) {
SAFE_snprintf(&buffer,&size,"%s",r->name);
- return buffer;
+ return (buffer);
}
- return r->name;
+ return (r->name);
case PO_IMMEDIATE:
s = buffer;
pcop->name);
}
}
- return buffer;
+ return (buffer);
case PO_GPR_REGISTER:
case PO_DIR:
} else {
SAFE_snprintf(&s,&size,"%s",pcop->name);
}
- return buffer;
+ return (buffer);
case PO_GPR_BIT:
s = buffer;
if(PCORB(pcop)->subtype == PO_GPR_TEMP) {
}
return (buffer);
+ case PO_TWO_OPS:
+ return (pic16_get_op( PCOP2(pcop)->pcopL, use_buffer ? buffer : NULL, size ));
+
default:
if(pcop->name) {
if(use_buffer) {
SAFE_snprintf(&buffer,&size,"%s",pcop->name);
- return buffer;
+ return (buffer);
}
- return pcop->name;
+ return (pcop->name);
}
}
+ return ("unhandled type for op1");
}
- return "NO operand1";
+ return ("NO operand1");
}
/*-----------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
char *pic16_get_op2(pCodeOp *pcop,char *buffer, size_t size)
{
- regs *r;
- static char b[128];
- char *s;
- int use_buffer = 1; // copy the string to the passed buffer pointer
-
- if(!buffer) {
- buffer = b;
- size = sizeof(b);
- use_buffer = 0; // Don't bother copying the string to the buffer.
- }
-#if 0
- fprintf(stderr, "%s:%d second operand %s is %d\tPO_DIR(%d) PO_GPR_TEMP(%d) PO_IMMEDIATE(%d) PO_INDF0(%d) PO_FSR0(%d)\n",
- __FUNCTION__, __LINE__, PCOR(PCOR2(pcop)->pcop2)->r->name, PCOR2(pcop)->pcop2->type,
- PO_DIR, PO_GPR_TEMP, PO_IMMEDIATE, PO_INDF0, PO_FSR0);
-#endif
-
- if(pcop) {
- switch(PCOR2(pcop)->pcop2->type) {
- case PO_W:
- case PO_WREG:
- case PO_PRODL:
- case PO_PRODH:
- case PO_INDF0:
- case PO_FSR0:
- if(use_buffer) {
- SAFE_snprintf(&buffer,&size,"%s",PCOR(PCOR2(pcop)->pcop2)->r->name);
- return buffer;
- }
- return PCOR(PCOR2(pcop)->pcop2)->r->name;
- break;
- case PO_GPR_TEMP:
- r = pic16_regWithIdx(PCOR(PCOR2(pcop)->pcop2)->r->rIdx);
-
- if(use_buffer) {
- SAFE_snprintf(&buffer,&size,"%s",r->name);
- return buffer;
- }
- return r->name;
-
- case PO_IMMEDIATE:
- assert( 0 );
- break;
-#if 0
- s = buffer;
-
- if(PCOI(pcop)->_const) {
- if( PCOI(pcop)->offset && PCOI(pcop)->offset<4) {
- SAFE_snprintf(&s,&size,"(((%s+%d) >> %d)&0xff)",
- pcop->name,
- PCOI(pcop)->index,
- 8 * PCOI(pcop)->offset );
- } else
- SAFE_snprintf(&s,&size,"LOW(%s+%d)",pcop->name,PCOI(pcop)->index);
- } else {
- if( PCOI(pcop)->index) {
- SAFE_snprintf(&s,&size,"(%s + %d)",
- pcop->name,
- PCOI(pcop)->index );
- } else {
- if(PCOI(pcop)->offset)
- SAFE_snprintf(&s,&size,"(%s >> %d)&0xff",pcop->name, 8*PCOI(pcop)->offset);
- else
- SAFE_snprintf(&s,&size,"%s",pcop->name);
- }
- }
- return buffer;
-#endif
- case PO_DIR:
- s = buffer;
- if( PCOR(PCOR2(pcop)->pcop2)->instance) {
- SAFE_snprintf(&s,&size,"(%s + %d)",
- PCOR(PCOR2(pcop)->pcop2)->r->name,
- PCOR(PCOR2(pcop)->pcop2)->instance );
- } else {
- SAFE_snprintf(&s,&size,"%s",PCOR(PCOR2(pcop)->pcop2)->r->name);
- }
- return buffer;
-
- default:
- if(PCOR(PCOR2(pcop)->pcop2)->r->name) {
- if(use_buffer) {
- SAFE_snprintf(&buffer,&size,"%s",PCOR(PCOR2(pcop)->pcop2)->r->name);
- return buffer;
- }
- return PCOR(PCOR2(pcop)->pcop2)->r->name;
- }
- }
- }
+ if(pcop && pcop->type == PO_TWO_OPS) {
+ return pic16_get_op( PCOP2(pcop)->pcopR, buffer, size );
+ }
return "NO operand2";
}
if(isPCI(pc) && (PCI(pc)->pci_magic != PCI_MAGIC)) {
fprintf(stderr, "%s:%d: pCodeInstruction initialization error in instruction %s, magic is %x (defaut: %x)\n",
__FILE__, __LINE__, PCI(pc)->mnemonic, PCI(pc)->pci_magic, PCI_MAGIC);
-// exit(-1);
+// exit(EXIT_FAILURE);
}
#endif
if( (PCI(pc)->num_ops >= 1) && (PCI(pc)->pcop)) {
- if(PCI(pc)->is2MemOp) {
- SAFE_snprintf(&s,&size, "%s, %s",
- pic16_get_op(PCOP(PCI(pc)->pcop), NULL, 0),
- pic16_get_op2(PCOP(PCI(pc)->pcop), NULL, 0));
+ //if(PCI(pc)->is2MemOp)
+ if (PCI(pc)->pcop->type == PO_TWO_OPS)
+ {
+ /* split into two phases due to static buffer in pic16_get_op() */
+ SAFE_snprintf(&s,&size, "%s",
+ pic16_get_op((PCI(pc)->pcop), NULL, 0));
+ SAFE_snprintf(&s, &size, ", %s",
+ pic16_get_op2((PCI(pc)->pcop), NULL, 0));
break;
}
r = pic16_getRegFromInstruction(pc);
// fprintf(stderr, "%s:%d reg = %p\tname= %s, accessBank= %d\n",
-// __FUNCTION__, __LINE__, r, (r)?r->name:"<null>", (r)?r->accessBank:-1);
+// __FUNCTION__, __LINE__, r, (r)?r->name:"<null>", (r)?isACCESS_BANK(r):-1);
if(PCI(pc)->isAccess) {
static char *bank_spec[2][2] = {
{ ", B", ", BANKED" }/* MPASM (should) use BANKED by default */
};
- SAFE_snprintf(&s,&size,"%s", bank_spec[(r && !r->accessBank) ? 1 : 0][pic16_mplab_comp ? 1 : 0]);
+ SAFE_snprintf(&s,&size,"%s", bank_spec[(r && !isACCESS_BANK(r)) ? 1 : 0][pic16_mplab_comp ? 1 : 0]);
}
}
//
case PO_WILD:
break;
+
+ case PO_TWO_OPS:
+ return pic16_getRegFrompCodeOp( PCOP2(pcop)->pcopL );
+ break;
default:
- fprintf(stderr, "pic16_getRegFromInstruction - unknown reg type %d (%s)\n",pcop->type, dumpPicOptype (pcop->type));
+ fprintf(stderr, "pic16_getRegFrompCodeOp - unknown reg type %d (%s)\n",pcop->type, dumpPicOptype (pcop->type));
// assert( 0 );
break;
}
/*-----------------------------------------------------------------*/
regs * pic16_getRegFromInstruction(pCode *pc)
{
-
if(!pc ||
!isPCI(pc) ||
!PCI(pc)->pcop ||
dumpPicOptype( PCI(pc)->pcop->type), PCI(pc)->pcop->type);
#endif
- return pic16_getRegFrompCodeOp (PCI(pc)->pcop);
+ return( pic16_getRegFrompCodeOp (PCI(pc)->pcop) );
}
/*-------------------------------------------------------------------------------*/
(PCI(pc)->num_ops == 1)) // accept only 2 operand commands
return NULL;
+ if (PCI(pc)->pcop->type != PO_TWO_OPS)
+ return NULL;
#if 0
fprintf(stderr, "pic16_getRegFromInstruction2 - reg type %s (%d)\n",
dumpPicOptype( PCI(pc)->pcop->type), PCI(pc)->pcop->type);
#endif
- return pic16_getRegFrompCodeOp (PCOR2(PCI(pc)->pcop)->pcop2);
+ return pic16_getRegFrompCodeOp (PCOP2(PCI(pc)->pcop)->pcopR);
}
/*-----------------------------------------------------------------*/
}
if(PCI(pc)->pcop->type == PO_GPR_REGISTER) {
if(PCOR(PCI(pc)->pcop)->r) {
- pic16_allocWithIdx (PCOR(PCI(pc)->pcop)->r->rIdx);
+ pic16_allocWithIdx(PCOR(PCI(pc)->pcop)->r->rIdx); /* FIXME! - VR */
DFPRINTF((stderr,"found register in pblock: reg 0x%x\n",PCOR(PCI(pc)->pcop)->r->rIdx));
} else {
if(PCI(pc)->pcop->name)
{
pCode *pc; pCodeLabel *pcl;
- if(!pb)
+ if(!pb || !pb->pcHead)
return;
for(pc = pb->pcHead; (pc=pic16_findNextInstruction(pc->next)) != NULL; ) {
"PO_BIT", // bit operand.
"PO_STR", // (8051 legacy)
"PO_LABEL",
- "PO_WILD" // Wild card operand in peep optimizer
+ "PO_WILD", // Wild card operand in peep optimizer
+ "PO_TWO_OPS" // combine two operands
};
char *dumpPicOptype(PIC_OPTYPE type)
{
+ assert( type >= 0 && type < sizeof(pic_optype_names)/sizeof( char *) );
return (pic_optype_names[ type ]);
}
/* getSymbolFromOperand - return a pointer to the symbol in */
/* the given operand and its length */
/*----------------------------------------------------------*/
-char *getSymbolFromOperand (char *op, unsigned int *len)
+char *getSymbolFromOperand (char *op, int *len)
{
char *sym, *curr;
*len = 0;
pseudoBankNr getBankFromBanksel (pCode *pc)
{
char *sym;
- int data = (int)NULL;
+ int data = 0;
if (!pc) return INVALID_BANK;
#define PTR_TO_INT(x) (((char *)(x)) - ((char *) 0))
#endif
+static int pic16_regIsLocal (regs *r);
+static int pic16_safepCodeRemove (pCode *pc, char *comment);
+
/* statistics */
static unsigned int pic16_df_removed_pcodes = 0;
static unsigned int pic16_df_saved_bytes = 0;
pcprev = pic16_findPrevInstruction (pc->prev);
pcnext = pic16_findNextInstruction (pc->next);
- /* if previous instruction is a skip -- do not remove */
- if (pcprev && isPCI_SKIP(pcprev)) return 0;
-
/* move labels to next instruction (if possible) */
if (PCI(pc)->label && !pcnext) return 0;
+ /* if this is a SKIP with side-effects -- do not remove */
+ /* XXX: might try to replace this one with the side-effect only version */
+ if (isPCI_SKIP(pc)
+ && ((PCI(pc)->outCond & (PCC_REGISTER | PCC_W)) != 0))
+ {
+ pCode *newpc;
+ switch (PCI(pc)->op)
+ {
+ case POC_INCFSZ:
+ case POC_INFSNZ:
+ newpc = pic16_newpCode(POC_INCF, pic16_pCodeOpCopy( PCI(pc)->pcop ) );
+ pic16_pCodeReplace( pc, newpc );
+ return 1;
+ break;
+ case POC_INCFSZW:
+ newpc = pic16_newpCode(POC_INCFW, pic16_pCodeOpCopy( PCI(pc)->pcop ) );
+ pic16_pCodeReplace( pc, newpc );
+ return 1;
+ break;
+ case POC_DECFSZ:
+ case POC_DCFSNZ:
+ newpc = pic16_newpCode(POC_INCF, pic16_pCodeOpCopy( PCI(pc)->pcop ) );
+ pic16_pCodeReplace( pc, newpc );
+ return 1;
+ break;
+ case POC_DECFSZW:
+ newpc = pic16_newpCode(POC_INCF, pic16_pCodeOpCopy( PCI(pc)->pcop ) );
+ pic16_pCodeReplace( pc, newpc );
+ return 1;
+ break;
+ default:
+ return 0;
+ }
+ return 0;
+ }
+
+ /* if previous instruction is a skip -- do not remove */
+ if (pcprev && isPCI_SKIP(pcprev)) {
+ if (!pic16_safepCodeUnlink (pcprev, "=DF= removed now unused SKIP")) {
+ /* preceeding SKIP could not be removed -- keep this instruction! */
+ return 0;
+ }
+ }
+
if (PCI(pc)->label) {
//fprintf (stderr, "%s: moving label(s)\n", __FUNCTION__);
//pc->print (stderr, pc);
defmap_t *map;
pCodeFlow *pcfl;
+ assert(pb);
+
pcfl = PCI(pic16_findNextInstruction (pb->pcHead))->pcflow;
/* find initial value (assigning pc == NULL) */
static int pic16_regIsLocal (regs *r) {
symbol_t sym;
if (r) {
+ if (r->type == REG_TMP) return 1;
+
sym = symFromStr (r->name);
switch (sym) {
case SPO_WREG:
while (map && map->pc != pc) map = map->next;
/* no entries found? something is fishy with DF analysis... -- play safe */
- if (!map) { fprintf (stderr, "%s: defmap not found\n", __FUNCTION__); return 1; }
+ if (!map) {
+ if (pic16_pcode_verbose) {
+ fprintf (stderr, "%s: defmap not found\n", __FUNCTION__);
+ }
+ return 1;
+ }
/* remember first item assigned to pc for later use */
lastpc = map;
set *todo;
set *blacklist;
+ if (!pb) return;
+
/* initialize out_vals to unique'fied defmaps per pCodeFlow */
for (pc = pic16_findNextInstruction (pb->pcHead); pc; pc = pic16_findNextInstruction (pc->next)) {
if (isPCFL(pc)) {
if (val && oldval && (val->in_val != 0) && (val->in_val == oldval->in_val)) {
//fprintf (stderr, "MOVFW: W already set up correctly (%x) at %p\n", oldval->in_val, pc);
if (!pic16_isAlive (SPO_STATUS, pc)) pic16_safepCodeRemove (pc, "=DF= redundant MOVFW removed");
+ } else {
+ defmap_t *pred, *predpred;
+ /* Optimize MOVLW immd; MOVWF reg1; [...]; MOVFW reg1
+ * into MOVLW immd; MOVWF reg1; [...]; MOVLW immd
+ * This might allow removal of the first two assignments. */
+ pred = defmapFindDef (list, sym1, pc);
+ predpred = pred ? defmapFindDef (list, SPO_WREG, pred->pc) : NULL;
+ if (pred && predpred && (PCI(pred->pc)->op == POC_MOVWF) && (PCI(predpred->pc)->op == POC_MOVLW)
+ && !pic16_isAlive (SPO_STATUS, pc))
+ {
+ newpc = pic16_newpCode (POC_MOVLW, pic16_pCodeOpCopy (PCI(predpred->pc)->pcop));
+
+ if (pic16_debug_verbose || pic16_pcode_verbose) {
+ pic16_InsertCommentAfter (pc->prev, "=DF= MOVFW: replaced last of MOVLW;MOVWF;MOVFW by MOVLW");
+ } // if
+ pic16_pCodeReplace (pc, newpc);
+ defmapReplaceSymRef (pc, sym1, 0, 1);
+ pic16_fixDefmap (pc, newpc);
+ pc = newpc;
+
+ /* This breaks the defmap chain's references to pCodes... fix it! */
+ if (!val->prev) PCI(pc)->pcflow->defmap = val->next;
+ if (!val->acc.access.isWrite) {
+ deleteDefmap (val); // delete reference to reg1 as in value
+ val = NULL;
+ } else {
+ val->acc.access.isRead = 0; // delete reference to reg1 as in value
+ }
+ oldval = PCI(pc)->pcflow->defmap;
+ while (oldval) {
+ if (oldval->pc == pc) oldval->pc = newpc;
+ oldval = oldval->next;
+ } // while
+ } // if
}
if (val) defmapUpdate (list, SPO_WREG, pc, val->in_val);
}
if (!isSpecial1 && pic16_regIsLocal (reg1) && val && oldval && !pic16_isAlive (SPO_STATUS, pc)) {
//pc->print (stderr, pc); fprintf (stderr, "lit: %d (%x, %x)\n", lit, lit, val->in_val);
if (lit == 0) {
- newpc = pic16_newpCode (POC_CLRF, PCOR2(pci->pcop)->pcop2);
+ newpc = pic16_newpCode (POC_CLRF, PCOP2(pci->pcop)->pcopR);
} else if (lit == 0x00ff) {
- newpc = pic16_newpCode (POC_SETF, PCOR2(pci->pcop)->pcop2);
+ newpc = pic16_newpCode (POC_SETF, PCOP2(pci->pcop)->pcopR);
} else {
newpc = NULL;
}
pc = newpc;
break; // do not process instruction as MOVFF...
}
- } else if (!isSpecial1 && !isSpecial2 && pic16_regIsLocal (reg1) && pic16_regIsLocal (reg2)) {
- if (val && oldval && (val->in_val != 0) && (val->in_val == oldval->in_val)) {
+ } else if (!isSpecial1 && !isSpecial2
+ && pic16_regIsLocal (reg1) && pic16_regIsLocal (reg2)
+ && val && oldval && (val->in_val != 0)) {
+ if (val->in_val == oldval->in_val) {
//fprintf (stderr, "MOVFF: F2 (%s) already set up correctly (%x) at %p\n", strFromSym (sym2), oldval->in_val, pc);
pic16_safepCodeRemove (pc, "=DF= redundant MOVFF removed");
} else {
if (copy->val && copy->val == val->in_val) {
//fprintf (stderr, "found replacement symbol for %s (val %x) <-- %s (assigned %x @ %p)\n", strFromSym(sym1), val->in_val, strFromSym(copy->sym), copy->val, copy->pc);
if (copy->sym == SPO_WREG) {
- newpc = pic16_newpCode (POC_MOVWF, pic16_pCodeOpCopy (PCOR2(pci->pcop)->pcop2));
+ newpc = pic16_newpCode (POC_MOVWF, pic16_pCodeOpCopy (PCOP2(pci->pcop)->pcopR));
} else {
+ pCodeOp *pcop = NULL;
+ /* the code below fails if we try to replace
+ * MOVFF PRODL, r0x03
+ * MOVFF r0x03, PCLATU
+ * with
+ * MOVFF PRODL, PCLATU
+ * as copy(PRODL) contains has pc==NULL, by name fails...
+ */
+ if (!copy->pc || !PCI(copy->pc)->pcop) break;
+
+ if (copy->pc && PCI(copy->pc)->pcop)
+ pcop = PCI(copy->pc)->pcop;
+#if 0
+ /* This code is broken--see above. */
+ else
+ {
+ const char *symname = strFromSym(copy->sym);
+
+ assert( symname );
+ pic16_InsertCommentAfter (pc->prev, "BUG-ME");
+ pic16_InsertCommentAfter (pc->prev, "=DF= MOVFF: newpCodeOpregFromStr(%s)", (char *)symname);
+ //pcop = pic16_newpCodeOpRegFromStr((char *)symname);
+ }
+#endif
+ assert( pcop );
newpc = pic16_newpCode(POC_MOVFF, pic16_popGet2p(
-// /*TODO: change to copy->pc's out symbol*/pic16_pCodeOpCopy (pci->pcop),
- pic16_pCodeOpCopy (PCI(copy->pc)->pcop),
- pic16_pCodeOpCopy (PCOR2(pci->pcop)->pcop2)));
+ pcop,
+ pic16_pCodeOpCopy (PCOP2(pci->pcop)->pcopR)));
}
pic16_InsertCommentAfter (pc->prev, "=DF= MOVFF: SRC op %s replaced by %s", strFromSym(sym1), strFromSym(copy->sym));
pic16_df_saved_bytes += PCI(pc)->isize - PCI(newpc)->isize;
static void pic16_destructDF (pBlock *pb) {
pCode *pc, *next;
+ if (!pb) return;
+
/* remove old defmaps */
pc = pic16_findNextInstruction (pb->pcHead);
while (pc) {
static int pic16_pBlockHasAsmdirs (pBlock *pb) {
pCode *pc;
+ if (!pb) return 0;
+
pc = pic16_findNextInstruction (pb->pcHead);
while (pc) {
if (isPCAD(pc)) return 1;
pCode *pc, *next;
int change=0;
+ if (!pb) return;
+
//fprintf (stderr, "creating DF for pb %p (%s)\n", pb, pic16_pBlockGetFunctionName (pb));
pic16_destructDF (pb);
#endif
}
-
-/* ======================================================================= */
-/* === DEPRECATED CONTROL FLOW CREATION ROUTINES ========================= */
-/* ======================================================================= */
-
-#if 0
-
-/* connect pCode f anf t via their to/from pBranches */
-static void pic16_pCodeLink (pCode *f, pCode *t) {
- pBranch *br;
- pCodeInstruction *_f, *_t;
-
- if (!f || !t) return;
-
-#if 0
- fprintf (stderr, "linking:\n");
- f->print(stderr, f);
- f->print(stderr, t);
-#endif
-
- assert (isPCI(f) || isPCAD(f));
- assert (isPCI(t) || isPCAD(t));
- _f = PCI(f);
- _t = PCI(t);
-
- /* define t to be CF successor of f */
- br = Safe_malloc (sizeof (pBranch));
- br->pc = t;
- br->next = NULL;
- _f->to = pic16_pBranchAppend (_f->to, br);
-
- /* define f to be CF predecessor of t */
- br = Safe_malloc (sizeof (pBranch));
- br->pc = f;
- br->next = NULL;
- _t->from = pic16_pBranchAppend (_t->from, br);
-
- /* also update pcflow information */
- if (_f->pcflow && _t->pcflow && _f->pcflow != _t->pcflow) {
- //fprintf (stderr, "creating flow %p --> %p\n", _f->pcflow, _t->pcflow);
- LinkFlow_pCode (_f, _t);
- } // if
-}
-
-static void pic16_destructCF (pBlock *pb) {
- pCode *pc;
- pBranch *br;
-
- /* remove old CF information */
- pc = pb->pcHead;
- while (pc) {
- if (isPCI(pc)) {
- while (PCI(pc)->to) {
- br = PCI(pc)->to->next;
- Safe_free (PCI(pc)->to);
- PCI(pc)->to = br;
- } // while
- while (PCI(pc)->from) {
- br = PCI(pc)->from->next;
- Safe_free (PCI(pc)->from);
- PCI(pc)->from = br;
- }
- } else if (isPCFL(pc)) {
- deleteSet (&PCFL(pc)->to);
- deleteSet (&PCFL(pc)->from);
- }
- pc = pc->next;
- }
-
- releaseStack ();
-}
-
-/* Set up pCodeInstruction's to and from pBranches. */
-static void pic16_createCF (pBlock *pb) {
- pCode *pc;
- pCode *next, *dest;
- char *label;
-
- //fprintf (stderr, "creating CF for %p\n", pb);
-
- pic16_destructCF (pb);
-
- /* check pBlock: do not analyze pBlocks with ASMDIRs (for now...) */
- if (pic16_pBlockHasAsmdirs (pb)) {
- //fprintf (stderr, "%s: pBlock contains ASMDIRs -- data flow analysis not performed!\n", __FUNCTION__);
- return;
- }
-
- pc = pic16_findNextInstruction(pb->pcHead);
- while (pc) {
- next = pic16_findNextInstruction(pc->next);
- if (isPCI_SKIP(pc)) {
- pic16_pCodeLink(pc, next);
- pic16_pCodeLink(pc, pic16_findNextInstruction(next->next));
- } else if (isPCI_BRANCH(pc)) {
- // Bcc, BRA, CALL, GOTO
- if (PCI(pc)->pcop) {
- switch (PCI(pc)->pcop->type) {
- case PO_LABEL:
- label = PCOLAB(PCI(pc)->pcop)->pcop.name;
- dest = findLabelinpBlock (pc->pb, PCOLAB(PCI(pc)->pcop));
- break;
-
- case PO_STR:
- /* needed for GOTO ___irq_handler */
- label = PCI(pc)->pcop->name;
- dest = NULL;
- break;
-
- default:
- assert (0 && "invalid label format");
- break;
- } // switch
- } else {
- label = "NO PCOP";
- dest = NULL;
- }
-
- switch (PCI(pc)->op) {
- case POC_BRA:
- case POC_GOTO:
- if (dest != NULL) {
- pic16_pCodeLink(pc, dest);
- } else {
- //fprintf (stderr, "jump target \"%s\" not found!\n", label);
- }
- break;
- case POC_CALL:
- case POC_RETURN:
- case POC_RETFIE:
- pic16_pCodeLink(pc, next);
- break;
- case POC_BC:
- case POC_BNC:
- case POC_BZ:
- case POC_BNZ:
- case POC_BN:
- case POC_BNN:
- case POC_BOV:
- case POC_BNOV:
- if (dest != NULL) {
- pic16_pCodeLink(pc, dest);
- } else {
- //fprintf (stderr, "jump target \"%s\"not found!\n", label);
- }
- pic16_pCodeLink(pc, next);
- break;
- default:
- fprintf (stderr, "BRANCH instruction: %s\n", PCI(pc)->mnemonic);
- assert (0 && "unhandled branch instruction");
- break;
- } // switch
- } else {
- pic16_pCodeLink (pc, next);
- }
- pc = next;
- } // while
-}
-#endif
-
/* ======================================================================== */
/* === VCG DUMPER ROUTINES ================================================ */
/* ======================================================================== */
static void pic16_vcg_dump (FILE *of, pBlock *pb) {
pCode *pc;
+ if (!pb) return;
+
/* check pBlock: do not analyze pBlocks with ASMDIRs (for now...) */
if (pic16_pBlockHasAsmdirs (pb)) {
//fprintf (stderr, "%s: pBlock contains ASMDIRs -- data flow analysis not performed!\n", __FUNCTION__);
char buf[BUF_SIZE];
pCode *pc;
+ if (!pb) return;
+
/* get function name */
pc = pb->pcHead;
while (pc && !isPCF(pc)) pc = pc->next;