From 52492c31ed73fc5544152dc3a274d55203486157 Mon Sep 17 00:00:00 2001 From: sdattalo Date: Sat, 11 May 2002 13:42:11 +0000 Subject: [PATCH] Fixed several pointer related bugs in the PIC port git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2015 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- src/pic/gen.c | 120 +++++++++++++++++++----------------- src/pic/glue.c | 48 +-------------- src/pic/main.c | 2 +- src/pic/pcode.c | 40 +++++++----- src/pic/pcode.h | 7 ++- src/pic/pcodepeep.c | 6 +- src/regression/configword.c | 53 ++++++++++++++++ src/regression/string1.c | 14 ++++- 8 files changed, 166 insertions(+), 124 deletions(-) create mode 100644 src/regression/configword.c diff --git a/src/pic/gen.c b/src/pic/gen.c index 409435d4..d189a612 100644 --- a/src/pic/gen.c +++ b/src/pic/gen.c @@ -75,7 +75,7 @@ static int optimized_for_speed = 0; static int max_key=0; static int GpsuedoStkPtr=0; -pCodeOp *popGetImmd(char *name, unsigned int offset); +pCodeOp *popGetImmd(char *name, unsigned int offset, int index); unsigned int pic14aopLiteral (value *val, int offset); const char *AopType(short type); static iCode *ifxForOp ( operand *op, iCode *ic ); @@ -546,11 +546,27 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result) /* only remaining is far space */ /* in which case DPTR gets the address */ + sym->aop = aop = newAsmop(AOP_PCODE); + + aop->aopu.pcop = popGetImmd(sym->rname,0,0); + PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space); + PCOI(aop->aopu.pcop)->index = 0; + + DEBUGpic14_emitcode(";"," rname %s, val %d, const = %d", + sym->rname, 0, PCOI(aop->aopu.pcop)->_const); + + allocDirReg (IC_LEFT(ic)); + + aop->size = FPTRSIZE; +/* + DEBUGpic14_emitcode(";","%d size = %d, name =%s",__LINE__,aop->size,sym->rname); sym->aop = aop = newAsmop(AOP_DPTR); pic14_emitcode ("mov","dptr,#%s", sym->rname); aop->size = getSize(sym->type); DEBUGpic14_emitcode(";","%d size = %d",__LINE__,aop->size); +*/ + /* if it is in code space */ if (IN_CODESPACE(space)) aop->code = 1; @@ -561,13 +577,15 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result) /*-----------------------------------------------------------------*/ /* aopForRemat - rematerialzes an object */ /*-----------------------------------------------------------------*/ -static asmop *aopForRemat (symbol *sym) +static asmop *aopForRemat (operand *op) // x symbol *sym) { - iCode *ic = sym->rematiCode; - //X asmop *aop = newAsmop(AOP_IMMD); - asmop *aop = newAsmop(AOP_PCODE); - int val = 0; - int offset = 0; + symbol *sym = OP_SYMBOL(op); + iCode *ic = NULL; + asmop *aop = newAsmop(AOP_PCODE); + int val = 0; + int offset = 0; + + ic = sym->rematiCode; DEBUGpic14_emitcode(";","%s %d",__FUNCTION__,__LINE__); @@ -581,22 +599,17 @@ static asmop *aopForRemat (symbol *sym) ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode; } -/* X - if (val) { - sprintf(buffer,"(%s %c 0x%04x)", - OP_SYMBOL(IC_LEFT(ic))->rname, - val >= 0 ? '+' : '-', - abs(val) & 0xffff); - fprintf(stderr,"hmmm %s\n",buffer); - } else - strcpy(buffer,OP_SYMBOL(IC_LEFT(ic))->rname); -*/ - //ic = sym->rematiCode; + offset = OP_SYMBOL(IC_LEFT(ic))->offset; - aop->aopu.pcop = popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname,val); - DEBUGpic14_emitcode(";"," rname %s, val %d ",OP_SYMBOL(IC_LEFT(ic))->rname,val); - //X aop->aopu.aop_immd = Safe_calloc(1,strlen(buffer)+1); - //X strcpy(aop->aopu.aop_immd,buffer); + aop->aopu.pcop = popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname,0,val); + PCOI(aop->aopu.pcop)->_const = IS_PTR_CONST(operandType(op)); + PCOI(aop->aopu.pcop)->index = val; + + DEBUGpic14_emitcode(";"," rname %s, val %d, const = %d", + OP_SYMBOL(IC_LEFT(ic))->rname, + val, IS_PTR_CONST(operandType(op))); + + // DEBUGpic14_emitcode(";","aop type %s",AopType(AOP_TYPE(IC_LEFT(ic)))); allocDirReg (IC_LEFT(ic)); @@ -790,8 +803,9 @@ void aopOp (operand *op, iCode *ic, bool result) DEBUGpic14_emitcode(";","%d",__LINE__); /* rematerialize it NOW */ if (sym->remat) { + sym->aop = op->aop = aop = - aopForRemat (sym); + aopForRemat (op); aop->size = getSize(sym->type); //DEBUGpic14_emitcode(";"," %d: size %d, %s\n",__LINE__,aop->size,aop->aopu.aop_immd); return; @@ -828,7 +842,7 @@ void aopOp (operand *op, iCode *ic, bool result) sym->rname, sym->usl.spillLoc->offset); // X sym->aop = op->aop = aop = aopForSym(ic,sym->usl.spillLoc,result); sym->aop = op->aop = aop = newAsmop(AOP_PCODE); - aop->aopu.pcop = popGetImmd(sym->usl.spillLoc->rname,sym->usl.spillLoc->offset); + aop->aopu.pcop = popGetImmd(sym->usl.spillLoc->rname,0,sym->usl.spillLoc->offset); //allocDirReg (IC_LEFT(ic)); aop->size = getSize(sym->type); @@ -1187,10 +1201,10 @@ pCodeOp *popGetLit(unsigned int lit) /*-----------------------------------------------------------------*/ /* popGetImmd - asm operator to pcode immediate conversion */ /*-----------------------------------------------------------------*/ -pCodeOp *popGetImmd(char *name, unsigned int offset) +pCodeOp *popGetImmd(char *name, unsigned int offset, int index) { - return newpCodeOpImmd(name, offset); + return newpCodeOpImmd(name, offset,index, 0); } @@ -1274,7 +1288,7 @@ pCodeOp *popGet (asmop *aop, int offset) //, bool bit16, bool dname) case AOP_IMMD: DEBUGpic14_emitcode(";","%d",__LINE__); - return popGetImmd(aop->aopu.aop_immd,offset); + return popGetImmd(aop->aopu.aop_immd,offset,0); case AOP_DIR: pcop = Safe_calloc(1,sizeof(pCodeOpReg) ); @@ -1342,7 +1356,9 @@ pCodeOp *popGet (asmop *aop, int offset) //, bool bit16, bool dname) case AOP_PCODE: DEBUGpic14_emitcode(";","popGet AOP_PCODE%d",__LINE__); - return pCodeOpCopy(aop->aopu.pcop); + pcop = pCodeOpCopy(aop->aopu.pcop); + PCOI(pcop)->offset = offset; + return pcop; } werror(E_INTERNAL_ERROR,__FILE__,__LINE__, @@ -1389,7 +1405,7 @@ void aopPut (asmop *aop, char *s, int offset) emitpcode(POC_CLRF,popGet(aop,offset)); break; } else - emitpcode(POC_MOVLW,popGetImmd(s,offset)); + emitpcode(POC_MOVLW,popGetImmd(s,offset,0)); } emitpcode(POC_MOVWF,popGet(aop,offset)); @@ -2341,7 +2357,7 @@ static void genCall (iCode *ic) //if (strcmp(l,fReturn[offset])) { - if ( ((AOP(IC_LEFT(sic))->type) == AOP_IMMD) || + if ( ((AOP(IC_LEFT(sic))->type) == AOP_PCODE) || ((AOP(IC_LEFT(sic))->type) == AOP_LIT) ) emitpcode(POC_MOVLW,popGet(AOP(IC_LEFT(sic)),offset)); else @@ -7929,7 +7945,7 @@ static void genConstPointerGet (operand *left, emitpcode(POC_GOTO,popGetLabel(blbl->key)); emitpLabel(albl->key); - poc = ( (AOP_TYPE(left) == AOP_IMMD) ? POC_MOVLW : POC_MOVFW); + poc = ( (AOP_TYPE(left) == AOP_PCODE) ? POC_MOVLW : POC_MOVFW); emitpcode(poc,popGet(AOP(left),1)); emitpcode(POC_MOVWF,popCopyReg(&pc_pclath)); @@ -8707,9 +8723,8 @@ static void genIfx (iCode *ic, iCode *popIc) /*-----------------------------------------------------------------*/ static void genAddrOf (iCode *ic) { - //symbol *sym = OP_SYMBOL(IC_LEFT(ic)); operand *right, *result, *left; - //int size, offset ; + int size, offset ; DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); @@ -8722,28 +8737,15 @@ static void genAddrOf (iCode *ic) DEBUGpic14_AopType(__LINE__,left,right,result); - emitpcode(POC_MOVLW, popGet(AOP(left),0)); - emitpcode(POC_MOVWF, popGet(AOP(result),0)); - -#if 0 - /* object not on stack then we need the name */ size = AOP_SIZE(IC_RESULT(ic)); offset = 0; while (size--) { - char s[SDCC_NAME_MAX]; - if (offset) - sprintf(s,"#(%s >> %d)", - sym->rname, - offset*8); - else - sprintf(s,"#%s",sym->rname); - aopPut(AOP(IC_RESULT(ic)),s,offset++); + emitpcode(POC_MOVLW, popGet(AOP(left),offset)); + emitpcode(POC_MOVWF, popGet(AOP(result),offset)); + offset++; } -#endif - - // freeAsmop(IC_RESULT(ic),NULL,ic,TRUE); freeAsmop(left,NULL,ic,FALSE); freeAsmop(result,NULL,ic,TRUE); @@ -9176,15 +9178,22 @@ static void genCast (iCode *ic) if (IS_PTR_CONST(operandType(IC_RESULT(ic)))) DEBUGpic14_emitcode ("; ***","%d - result is const pointer",__LINE__); + if ((AOP_TYPE(right) == AOP_PCODE) && AOP(right)->aopu.pcop->type == PO_IMMEDIATE) { + emitpcode(POC_MOVLW, popGet(AOP(right),0)); + emitpcode(POC_MOVWF, popGet(AOP(result),0)); + emitpcode(POC_MOVLW, popGet(AOP(right),1)); + emitpcode(POC_MOVWF, popGet(AOP(result),1)); + if(AOP_SIZE(result) <2) + fprintf(stderr,"%d -- result is not big enough to hold a ptr\n",__LINE__); + + } else { + /* if they in different places then copy */ size = AOP_SIZE(result); offset = 0 ; while (size--) { - if(AOP_TYPE(right) == AOP_IMMD) - emitpcode(POC_MOVLW, popGetImmd(AOP(right)->aopu.aop_dir,offset)); - else - emitpcode(POC_MOVFW, popGet(AOP(right),offset)); - emitpcode(POC_MOVWF, popGet(AOP(result),offset)); + emitpcode(POC_MOVFW, popGet(AOP(right),offset)); + emitpcode(POC_MOVWF, popGet(AOP(result),offset)); //aopPut(AOP(result), // aopGet(AOP(right),offset,FALSE,FALSE), @@ -9192,7 +9201,8 @@ static void genCast (iCode *ic) offset++; } - goto release; + } + goto release; } diff --git a/src/pic/glue.c b/src/pic/glue.c index d467d816..c49dfcb3 100644 --- a/src/pic/glue.c +++ b/src/pic/glue.c @@ -66,17 +66,6 @@ extern void printPublics (FILE * afile); extern void printChar (FILE * ofile, char *s, int plen); -/*-----------------------------------------------------------------*/ -/* aopLiteral - byte from a literal value */ -/*-----------------------------------------------------------------*/ -static int -_aopLiteral (value * val, int offset) -{ - unsigned long ul = (unsigned long) floatFromVal (val); - - return (ul >> (8*_ENDIAN(offset)) &0xff); -} - /*-----------------------------------------------------------------*/ /* aopLiteral - string from a literal value */ /*-----------------------------------------------------------------*/ @@ -254,54 +243,21 @@ printIvalType (symbol *sym, sym_link * type, initList * ilist, pBlock *pb) switch (getSize (type)) { case 1: - //tfprintf (oFile, "\t!dbs\n",aopLiteral (val, 0)); addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,0)))); - //fprintf(stderr,"0x%02x\n",_aopLiteral(val,0)); - break; case 2: - // if (port->use_dw_for_init) { - //tfprintf (oFile, "\t!dws\n", aopLiteralLong (val, 0, 2)); - // fprintf(stderr,"%s:%d aopLiteralLong\n",__FILE__,__LINE__); - //}else - //fprintf (oFile, "\t.byte %s,%s\n", aopLiteral (val, 0), aopLiteral (val, 1)); - //fprintf(stderr,"0x%02x 0x%02x\n",_aopLiteral(val,0),_aopLiteral(val,1)); addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,0)))); addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,1)))); break; + case 4: - /* - if (!val) { - tfprintf (oFile, "\t!dw !constword\n", 0); - tfprintf (oFile, "\t!dw !constword\n", 0); - } - else { - fprintf (oFile, "\t.byte %s,%s,%s,%s\n", - aopLiteral (val, 0), aopLiteral (val, 1), - aopLiteral (val, 2), aopLiteral (val, 3)); - fprintf(stderr,"0x%02x 0x%02x 0x%02x 0x%02x\n", - _aopLiteral(val,0),_aopLiteral(val,1), - _aopLiteral(val,2),_aopLiteral(val,3)); - } - */ addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,0)))); addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,1)))); addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,2)))); addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(BYTE_IN_LONG(ulval,3)))); break; } -#if 0 - { - int size = getSize(type); - - fprintf(stderr," size=%d, val =",size); - if(val) - fprintf(stderr,"0x%02x\n",_aopLiteral(val,0)); - else - fprintf(stderr,"none\n"); - } -#endif } /*-----------------------------------------------------------------*/ @@ -316,7 +272,7 @@ printIvalChar (sym_link * type, initList * ilist, pBlock *pb, char *s) if(!pb) return 0; - //fprintf(stderr, "%s\n",__FUNCTION__); + fprintf(stderr, "%s\n",__FUNCTION__); if (!s) { diff --git a/src/pic/main.c b/src/pic/main.c index d7771302..df4d2759 100644 --- a/src/pic/main.c +++ b/src/pic/main.c @@ -307,7 +307,7 @@ PORT pic_port = }, { /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */ - 1, 2, 2, 4, 1, 2, 1, 1, 4, 4 + 1, 2, 2, 4, 1, 2, 2, 1, 4, 4 /* TSD - I changed the size of gptr from 3 to 1. However, it should be 2 so that we can accomodate the PIC's with 4 register banks (like the 16f877) diff --git a/src/pic/pcode.c b/src/pic/pcode.c index 78d97b78..c1fe76db 100644 --- a/src/pic/pcode.c +++ b/src/pic/pcode.c @@ -1842,7 +1842,7 @@ pCodeOp *newpCodeOpLit(int lit) /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ -pCodeOp *newpCodeOpImmd(char *name, int offset) +pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space) { pCodeOp *pcop; @@ -1856,7 +1856,9 @@ pCodeOp *newpCodeOpImmd(char *name, int offset) } + PCOI(pcop)->index = index; PCOI(pcop)->offset = offset; + PCOI(pcop)->_const = code_space; return pcop; } @@ -2190,23 +2192,27 @@ static char *get_op( pCodeInstruction *pcc) case PO_IMMEDIATE: s = buffer; size = sizeof(buffer); + fprintf(stderr,"PO_IMMEDIATE name = %s offset = %d\n",pcc->pcop->name, + PCOI(pcc->pcop)->offset); + if(PCOI(pcc->pcop)->_const) { + + if( PCOI(pcc->pcop)->offset && PCOI(pcc->pcop)->offset<4) { + SAFE_snprintf(&s,&size,"(((%s+%d) >> %d)&0xff)", + pcc->pcop->name, + PCOI(pcc->pcop)->index, + 8 * PCOI(pcc->pcop)->offset ); + } else + SAFE_snprintf(&s,&size,"LOW(%s+%d)",pcc->pcop->name,PCOI(pcc->pcop)->index); + } else { + + if( PCOI(pcc->pcop)->index) { // && PCOI(pcc->pcop)->offset<4) { + SAFE_snprintf(&s,&size,"(%s + %d)", + pcc->pcop->name, + PCOI(pcc->pcop)->index ); + } else + SAFE_snprintf(&s,&size,"%s",pcc->pcop->name); + } - -/* - if( PCOI(pcc->pcop)->offset && PCOI(pcc->pcop)->offset<4) { - SAFE_snprintf(&s,&size,"((%s >> %d)&0xff)", - pcc->pcop->name, - 8 * PCOI(pcc->pcop)->offset ); - } else - SAFE_snprintf(&s,&size,"LOW(%s)",pcc->pcop->name); -*/ - - if( PCOI(pcc->pcop)->offset && PCOI(pcc->pcop)->offset<4) { - SAFE_snprintf(&s,&size,"(%s + %d)", - pcc->pcop->name, - PCOI(pcc->pcop)->offset ); - } else - SAFE_snprintf(&s,&size,"%s",pcc->pcop->name); return buffer; case PO_DIR: diff --git a/src/pic/pcode.h b/src/pic/pcode.h index 9e1d0033..007b9a50 100644 --- a/src/pic/pcode.h +++ b/src/pic/pcode.h @@ -304,7 +304,10 @@ typedef struct pCodeOpLit typedef struct pCodeOpImmd { pCodeOp pcop; - int offset; + int offset; /* low,med, or high byte of immediat value */ + int index; /* add this to the immediate value */ + unsigned _const:1; /* is in code space */ + } pCodeOpImmd; typedef struct pCodeOpLabel @@ -707,7 +710,7 @@ void pCodePeepInit(void); void pBlockConvert2ISR(pBlock *pb); pCodeOp *newpCodeOpLabel(char *name, int key); -pCodeOp *newpCodeOpImmd(char *name, int offset); +pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space); pCodeOp *newpCodeOpLit(int lit); pCodeOp *newpCodeOpBit(char *name, int bit,int inBitSpace); pCodeOp *newpCodeOpRegFromStr(char *name); diff --git a/src/pic/pcodepeep.c b/src/pic/pcodepeep.c index 6db8ae9a..ba65ceb5 100644 --- a/src/pic/pcodepeep.c +++ b/src/pic/pcodepeep.c @@ -391,7 +391,7 @@ static void * cvt_altpat_mnem0a(void *pp, pCodeWildBlock *pcwb) //if(p[0].pct[1].tok.n > sMaxWildVar) // sMaxWildMnem = p[0].pct[1].tok.n; - if(p[0].pct[1].tok.n > pcwb->nvars) + if(p[0].pct[1].tok.n > pcwb->nwildpCodes) pcwb->nwildpCodes = p[0].pct[1].tok.n; return newpCodeWild(p[0].pct[1].tok.n,NULL,NULL); @@ -470,7 +470,7 @@ static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb) return NULL; } - if(p[0].pct[1].tok.n > pcwb->nvars) + if(p[0].pct[1].tok.n > pcwb->nwildpCodes) pcwb->nwildpCodes = p[0].pct[1].tok.n; pc = newpCodeWild(p[1].pct[1].tok.n,NULL,NULL); @@ -1689,7 +1689,9 @@ pCodeOp *pCodeOpCopy(pCodeOp *pcop) case PO_IMMEDIATE: pcopnew = Safe_calloc(1,sizeof(pCodeOpImmd) ); + PCOI(pcopnew)->index = PCOI(pcop)->index; PCOI(pcopnew)->offset = PCOI(pcop)->offset; + PCOI(pcopnew)->_const = PCOI(pcop)->_const; break; case PO_LITERAL: diff --git a/src/regression/configword.c b/src/regression/configword.c new file mode 100644 index 00000000..e28c417a --- /dev/null +++ b/src/regression/configword.c @@ -0,0 +1,53 @@ +/* configword.c - illustrates how the configuration word can + * be assigned */ + +unsigned char success=0; +unsigned char failures=0; +unsigned char dummy=0; + +/* copied from 16f877.inc file supplied with gpasm */ + +#define _CP_ALL 0x0FCF +#define _CP_HALF 0x1FDF +#define _CP_UPPER_256 0x2FEF +#define _CP_OFF 0x3FFF +#define _DEBUG_ON 0x37FF +#define _DEBUG_OFF 0x3FFF +#define _WRT_ENABLE_ON 0x3FFF +#define _WRT_ENABLE_OFF 0x3DFF +#define _CPD_ON 0x3EFF +#define _CPD_OFF 0x3FFF +#define _LVP_ON 0x3FFF +#define _LVP_OFF 0x3F7F +#define _BODEN_ON 0x3FFF +#define _BODEN_OFF 0x3FBF +#define _PWRTE_OFF 0x3FFF +#define _PWRTE_ON 0x3FF7 +#define _WDT_ON 0x3FFF +#define _WDT_OFF 0x3FFB +#define _LP_OSC 0x3FFC +#define _XT_OSC 0x3FFD +#define _HS_OSC 0x3FFE +#define _RC_OSC 0x3FFF + +typedef unsigned int word; + +word at 0x2007 CONFIG = _WDT_OFF & _PWRTE_ON; + + +/* to do -- write a test that puts the PIC to sleep, + * and verify that the WDT wakes it up */ + +void done() +{ + + dummy++; + +} +void main(void) +{ + dummy = 0; + + success = failures; + done(); +} diff --git a/src/regression/string1.c b/src/regression/string1.c index 27f9c7ae..f240f2aa 100644 --- a/src/regression/string1.c +++ b/src/regression/string1.c @@ -29,11 +29,23 @@ void isr(void) interrupt 1 using 1 void lcd1(const unsigned char *str) { - if(str && str[2] != 'r') + const char *p = "hellow world!\r\n"; + + if(!str) + failures++; + + if(str && str[2] != 'r') failures++; if(arr[3]!=9) failures++; + + while (1) { + if (0 == *p) break; + PORTB = *p; + p++; + } + } void main(void) -- 2.30.2