You are forbidden to forbid anyone else to use, share and improve
what you give them. Help stamp out software-hoarding!
- Notes:
- 000123 mlh Moved aopLiteral to SDCCglue.c to help the split
- Made everything static
-------------------------------------------------------------------------*/
//#define D(x)
{
short inLine;
short debugLine;
+ short stackExtend;
short nRegsSaved;
+ short parmsPushed;
set *sendSet;
}
_G;
#define MSB24 2
#define MSB32 3
+static char *MOV="mov";
static char *MOVB="mov.b";
static char *MOVW="mov.w";
-//static char *MOVCB="movc.b";
-//static char *MOVCW="movc.w";
+static char *MOVC="movc";
+static char *MOVCB="movc.b";
+static char *MOVCW="movc.w";
+
+static char *R1L="r1l";
+static char *R1="r1";
void bailOut (char *mesg) {
fprintf (stderr, "%s: bailing out\n", mesg);
char *getStackOffset(int stack) {
static char gsoBuf[1024];
- // dit slaat natuurlijk nergens op, maar ja voor nou
- sprintf (gsoBuf, "r7+(%+d+0%+d%+d)", stack,
- FUNC_ISISR(currFunc->type) ?
- port->stack.isr_overhead : port->stack.call_overhead,
- _G.nRegsSaved);
+ sprintf (gsoBuf, "r7+(%d%+d%+d)", stack,
+ currFunc->stack, _G.nRegsSaved);
return gsoBuf;
}
/*-----------------------------------------------------------------*/
/* aopForSym - for a true symbol */
/*-----------------------------------------------------------------*/
-static asmop *aopForSym(symbol *sym, bool result, bool cantUsePointer) {
+static asmop *aopForSym(symbol *sym,
+ bool canUsePointer, bool canUseOffset) {
int size;
asmop *aop;
// if it is on stack
if (sym->onStack) {
- if (cantUsePointer) {
+ if (!canUsePointer || !canUseOffset) {
aop->type=AOP_REG;
switch (size)
{
sprintf (aop->name[1], "r1");
return aop;
}
- bailOut("aopForSym: onStack CantUsePointer unknown size");
}
aop->type=AOP_STK;
sprintf (aop->name[0], "[%s]", getStackOffset(sym->stack));
// if it has a spillLoc
if (sym->usl.spillLoc) {
- return aopForSym (sym->usl.spillLoc, result, cantUsePointer);
+ return aopForSym (sym->usl.spillLoc, canUsePointer, canUseOffset);
}
// if in bit space
// if in code space
if (IN_CODESPACE(SPEC_OCLS(sym->etype))) {
- if (result) {
- bailOut("aopForSym: result can not be in code space");
- }
- aop->type=AOP_CODE;
- emitcode ("mov", "r0,#%s ; aopForSym:code", sym->rname);
- sprintf (aop->name[0], "[r0]");
- if (size>2) {
- sprintf (aop->name[1], "[r0+2]");
+ if (!canUsePointer) {
+ aop->type=AOP_REG;
+ switch (size)
+ {
+ case 1:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOVC, "r0l,[r0]");
+ sprintf (aop->name[0], "r0l");
+ return aop;
+ case 2:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOVC, "r0,[r0]");
+ sprintf (aop->name[0], "r0");
+ return aop;
+ case 3:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOVC, "r1l,[r0+2]");
+ sprintf (aop->name[1], "r1l");
+ emitcode (MOV, "r0,[r0]");
+ sprintf (aop->name[0], "r0");
+ return aop;
+ case 4:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOVC, "r1,[r0+2]");
+ sprintf (aop->name[1], "r1");
+ emitcode (MOVC, "r0,[r0]");
+ sprintf (aop->name[0], "r0");
+ return aop;
+ }
+
+ } else {
+ aop->type=AOP_CODE;
+ emitcode ("mov", "r0,#%s ; aopForSym:code", sym->rname);
+ sprintf (aop->name[0], "[r0]");
+ if (size>2) {
+ sprintf (aop->name[1], "[r0+2]");
+ }
}
return aop;
}
// if in far space
if (IN_FARSPACE(SPEC_OCLS(sym->etype))) {
- aop->type=AOP_FAR;
- emitcode ("mov", "r0,#%s ; aopForSym:far", sym->rname);
- sprintf (aop->name[0], "[r0]");
- if (size>2) {
- sprintf (aop->name[1], "[r0+2]");
+ if (!canUsePointer) {
+ aop->type=AOP_REG;
+ switch (size)
+ {
+ case 1:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOV, "r0l,[r0]");
+ sprintf (aop->name[0], "r0l");
+ return aop;
+ case 2:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOV, "r0,[r0]");
+ sprintf (aop->name[0], "r0");
+ return aop;
+ case 3:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOV, "r1l,[r0+2]");
+ sprintf (aop->name[1], "r1l");
+ emitcode (MOV, "r0,[r0]");
+ sprintf (aop->name[0], "r0");
+ return aop;
+ case 4:
+ emitcode (MOV, "r0,#%s", sym->rname);
+ emitcode (MOV, "r1,[r0+2]");
+ sprintf (aop->name[1], "r1");
+ emitcode (MOV, "r0,[r0]");
+ sprintf (aop->name[0], "r0");
+ return aop;
+ }
+ } else {
+ aop->type=AOP_FAR;
+ emitcode ("mov", "r0,#%s ; aopForSym:far", sym->rname);
+ sprintf (aop->name[0], "[r0]");
+ if (size>2) {
+ sprintf (aop->name[1], "[r0+2]");
+ }
+ return aop;
}
- return aop;
}
-
+
bailOut("aopForSym");
return NULL;
}
break;
case 4:
sprintf (aop->name[0], "#0x%04lx",
+ SPEC_CVAL(operandType(op)).v_ulong & 0xffff);
+ sprintf (aop->name[1], "#0x%04lx",
SPEC_CVAL(operandType(op)).v_ulong >> 16);
- sprintf (aop->name[1], "#0x%04x",
- SPEC_CVAL(operandType(op)).v_ulong && 0xffff);
break;
default:
bailOut("aopForVal");
return NULL;
}
-static void aopOp(operand *op, bool result, bool cantUsePointer) {
+static int aopOp(operand *op,
+ bool canUsePointer, bool canUseOffset) {
if (IS_SYMOP(op)) {
- op->aop=aopForSym (OP_SYMBOL(op), result, cantUsePointer);
- return;
+ op->aop=aopForSym (OP_SYMBOL(op), canUsePointer, canUseOffset);
+ return AOP_SIZE(op);
}
if (IS_VALOP(op)) {
- if (result) {
- bailOut("aopOp: result can not be a value");
- }
op->aop=aopForVal (op);
- return;
+ return AOP_SIZE(op);
}
bailOut("aopOp: unexpected operand");
+ return 0;
+}
+
+bool aopEqual(asmop *aop1, asmop *aop2, int offset) {
+ if (strcmp(aop1->name[offset], aop2->name[offset])) {
+ return FALSE;
+ }
+ return TRUE;
+}
+
+bool aopIsDir(operand *op) {
+ return AOP_TYPE(op)==AOP_DIR;
+}
+
+bool aopIsBit(operand *op) {
+ return AOP_TYPE(op)==AOP_BIT;
}
bool aopIsPtr(operand *op) {
}
}
-char *opRegName(operand *op, int offset, char *opName) {
+char *opRegName(operand *op, int offset, char *opName, bool decorate) {
if (IS_SYMOP(op)) {
if (OP_SYMBOL(op)->onStack) {
}
// fall through
case V_CHAR:
- sprintf (opName, "#0x%02x", SPEC_CVAL(OP_VALUE(op)->type).v_int);
+ sprintf (opName, "#%s0x%02x", decorate?"(char)":"",
+ SPEC_CVAL(OP_VALUE(op)->type).v_int);
break;
case V_INT:
if (SPEC_LONG(OP_VALUE(op)->type)) {
- sprintf (opName, "#0x%02lx", SPEC_CVAL(OP_VALUE(op)->type).v_long);
+ sprintf (opName, "#%s0x%02lx", decorate?"(long)":"",
+ SPEC_CVAL(OP_VALUE(op)->type).v_long);
} else {
- sprintf (opName, "#0x%02x", SPEC_CVAL(OP_VALUE(op)->type).v_int);
+ sprintf (opName, "#%s0x%02x", decorate?"(int)":"",
+ SPEC_CVAL(OP_VALUE(op)->type).v_int);
}
break;
case V_FLOAT:
- sprintf (opName, "#%f", SPEC_CVAL(OP_VALUE(op)->type).v_float);
+ sprintf (opName, "#%s%f", decorate?"(float)":"",
+ SPEC_CVAL(OP_VALUE(op)->type).v_float);
break;
default:
bailOut("opRegName: unexpected noun");
strcat (line, "unknown");
return line;
} else if (IS_VALOP(op)) {
- opRegName(op, 0, line);
+ opRegName(op, 0, line, 1);
} else if (IS_TYPOP(op)) {
- sprintf (line, "[");
+ sprintf (line, "(");
if (isPtr) {
if (DCL_TYPE(optype)==FPOINTER)
strcat (line, "far * ");
if (SPEC_USIGN(operandType(op))) strcat (line, "unsigned ");
if (SPEC_LONG(operandType(op))) strcat (line, "long ");
strcat (line, nounName(operandType(op)));
- strcat (line, "]");
+ strcat (line, ")");
} else {
bailOut("printOp: unexpected operand type");
}
/* toBoolean - return carry for operand!=0 */
/*-----------------------------------------------------------------*/
static char *toBoolean (operand * op) {
+ symbol *tlbl=newiTempLabel(NULL);
+
switch (AOP_SIZE(op))
{
case 1:
case 2:
- emitcode ("cmp", "%s,#0", AOP_NAME(op));
- emitcode ("mov", "c,z");
+ emitcode ("cjne", "%s,#1,%05d$; %s", AOP_NAME(op), tlbl->key+100,
+ "This needs a second thought");
+
+ emitcode ("", "%05d$:", tlbl->key+100);
return "c";
}
}
/*-----------------------------------------------------------------*/
-/* genIpush - genrate code for pushing this gets a little complex */
+/* genIpush - generate code for pushing */
/*-----------------------------------------------------------------*/
static void genIpush (iCode * ic) {
+ operand *left=IC_LEFT(ic);
+
printIc ("genIpush", ic, 0,1,0);
+ aopOp(left,FALSE,FALSE);
+
+
+ if (AOP_TYPE(left)==AOP_LIT) {
+ switch (AOP_SIZE(left))
+ {
+ case 1:
+ emitcode ("mov", "r1l,%s", AOP_NAME(left)[0]);
+ emitcode ("push", "r1l");
+ _G.parmsPushed++;
+ return;
+ case 2:
+ emitcode ("mov", "r1,%s", AOP_NAME(left)[0]);
+ emitcode ("push", "r1");
+ _G.parmsPushed++;
+ return;
+ case 3:
+ emitcode ("mov", "r1l,%s", AOP_NAME(left)[1]);
+ emitcode ("push", "r1l");
+ emitcode ("mov", "r1,%s", AOP_NAME(left)[0]);
+ emitcode ("push", "r1");
+ _G.parmsPushed += 2;
+ return;
+ case 4:
+ emitcode ("mov", "r1,%s", AOP_NAME(left)[1]);
+ emitcode ("push", "r1");
+ emitcode ("mov", "r1,%s", AOP_NAME(left)[0]);
+ emitcode ("push", "r1");
+ _G.parmsPushed += 2;
+ return;
+ }
+ } else {
+ if (AOP_SIZE(left)>2) {
+ emitcode ("push", "%s", AOP_NAME(left)[1]);
+ _G.parmsPushed++;
+ }
+ emitcode ("push", "%s", AOP_NAME(left)[0]);
+ _G.parmsPushed++;
+ }
}
/*-----------------------------------------------------------------*/
/* genCall - generates a call statement */
/*-----------------------------------------------------------------*/
static void genCall (iCode * ic) {
- emitcode (";", "genCall %s result=%s", OP_SYMBOL(IC_LEFT(ic))->name,
+ operand *result=IC_RESULT(ic);
+
+ emitcode (";", "genCall(%d) %s result=%s", ic->lineno,
+ OP_SYMBOL(IC_LEFT(ic))->name,
printOp (IC_RESULT(ic)));
+ emitcode ("call", "%s", OP_SYMBOL(IC_LEFT(ic))->rname);
+
+ /* readjust the stack if we have pushed some parms */
+ if (_G.parmsPushed) {
+ emitcode ("add", "r7,#0x%02x", _G.parmsPushed*2);
+ _G.parmsPushed=0;
+ }
+
+ /* if we need to assign a result value */
+ if (IS_ITEMP (IC_RESULT(ic)) &&
+ OP_SYMBOL (IC_RESULT (ic))->nRegs) {
+ aopOp(result,FALSE,FALSE);
+ switch (AOP_SIZE(result))
+ {
+ case 1:
+ emitcode ("mov", "%s,r0l", AOP_NAME(result)[0]);
+ return;
+ case 2:
+ emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+ return;
+ case 3:
+ // generic pointer
+ emitcode ("mov", "%s,r1l", AOP_NAME(result)[1]);
+ emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+ return;
+ case 4:
+ emitcode ("mov", "%s,r1", AOP_NAME(result)[1]);
+ emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
+ return;
+ }
+ bailOut("genCall");
+ }
}
/*-----------------------------------------------------------------*/
symbol *sym=OP_SYMBOL(IC_LEFT(ic));
sym_link *type=sym->type;
- emitcode (";", "-----------------------------------------");
- emitcode (";", " function %s", sym->name);
- emitcode (";", "-----------------------------------------");
-
+ emitcode (";", "genFunction %s", sym->rname);
+
+ /* print the allocation information */
+ printAllocInfo (currFunc, codeOutFile);
+
emitcode ("", "%s:", sym->rname);
if (IFFUNC_ISNAKED(type))
return;
}
- /* if critical function then turn interrupts off */
- if (IFFUNC_ISCRITICAL (type))
- emitcode ("clr", "ea");
-
+ /* adjust the stack for locals used in this function */
+ if (sym->stack) {
+ emitcode ("sub", "r7,#%d\t; create stack space for locals", sym->stack);
+ }
}
/*-----------------------------------------------------------------*/
{
symbol *sym = OP_SYMBOL (IC_LEFT (ic));
- if (IFFUNC_ISNAKED(sym->type))
- {
+ printIc ("genEndFunction", ic, 0,0,0);
+
+ if (IFFUNC_ISNAKED(sym->type)) {
emitcode(";", "naked function: no epilogue.");
return;
}
- printIc ("genEndFunction", ic, 0,0,0);
+ /* readjust the stock for locals used in this function */
+ if (sym->stack) {
+ emitcode ("add", "r7,#%d\t; release stack space for locals", sym->stack);
+ }
+
+ if (IFFUNC_ISISR(sym->type)) {
+ emitcode ("reti", "");
+ } else {
+ emitcode ("ret", "");
+ }
}
/*-----------------------------------------------------------------*/
printIc ("genRet", ic, 0,1,0);
- aopOp(IC_LEFT(ic),FALSE, FALSE);
+ aopOp(IC_LEFT(ic), TRUE, TRUE);
switch (AOP_SIZE(IC_LEFT(ic)))
{
case 4:
emitcode ("mov", "r1,%s", AOP_NAME(IC_LEFT(ic))[1]);
emitcode ("mov", "r0,%s", AOP_NAME(IC_LEFT(ic))[0]);
- return;
+ break;
case 3:
emitcode ("mov", "r1l,%s", AOP_NAME(IC_LEFT(ic))[1]);
// fall through
case 2:
emitcode ("mov", "r0,%s", AOP_NAME(IC_LEFT(ic))[0]);
- return;
+ break;
case 1:
emitcode ("mov", "r0l,%s", AOP_NAME(IC_LEFT(ic))[0]);
- return;
+ break;
+ default:
+ bailOut("genRet");
}
- bailOut("genRet");
+ emitcode ("jmp", "%05d$", returnLabel->key+100);
}
/*-----------------------------------------------------------------*/
if (IC_LABEL (ic) == entryLabel)
return;
- emitcode (";", "genLabel %s", IC_LABEL(ic)->name);
+ emitcode (";", "genLabel(%d) %s", ic->lineno, IC_LABEL(ic)->name);
emitcode ("", "%05d$:", (IC_LABEL (ic)->key + 100));
}
/* genGoto - generates a jmp */
/*-----------------------------------------------------------------*/
static void genGoto (iCode * ic) {
- emitcode (";", "genGoto %s", IC_LABEL(ic)->name);
+ emitcode (";", "genGoto(%d) %s", ic->lineno, IC_LABEL(ic)->name);
emitcode ("jmp", "%05d$", (IC_LABEL (ic)->key + 100));
}
/* genPlus - generates code for addition */
/*-----------------------------------------------------------------*/
static void genPlus (iCode * ic) {
+ operand *result=IC_RESULT(ic), *left=IC_LEFT(ic), *right=IC_RIGHT(ic);
+ int size;
+ char *instr;
+
printIc ("genPlus", ic, 1,1,1);
+
+ size=aopOp(result, TRUE, TRUE);
+
+ /* if left is a literal, then exchange them */
+ if (IS_LITERAL(operandType(left))) {
+ operand *tmp = right;
+ right = left;
+ left = tmp;
+ }
+
+ if (aopIsBit(result)) {
+ if (IS_LITERAL(operandType(right))) {
+ if (operandLitValue(right)) {
+ emitcode ("setb", AOP_NAME(result)[0]);
+ return;
+ }
+ aopOp(left, TRUE, TRUE);
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[0], toBoolean(left));
+ return;
+ }
+ bailOut("genPlus: unfinished genPlus bit");
+ }
+
+ aopOp(left, !aopIsPtr(result), !aopIsDir(result));
+ aopOp(right, !aopIsPtr(result), !aopIsDir(result));
+
+ if (size>1) {
+ instr="add.w";
+ } else {
+ instr="add.b";
+ }
+ if (!aopEqual(result->aop, left->aop, 0)) {
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+ }
+ emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ if (size>2) {
+ if (!aopEqual(result->aop, left->aop, 1)) {
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[1], AOP_NAME(left)[1]);
+ }
+ if (size==3) {
+ // generic pointer
+ } else {
+ emitcode ("addc.w", "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+ }
+ }
+ return;
}
/*-----------------------------------------------------------------*/
/* genCmpGt :- greater than comparison */
/*-----------------------------------------------------------------*/
static void genCmpLe (iCode * ic) {
- printIc ("genCmpGt", ic, 1,1,1);
+ printIc ("genCmpLe", ic, 1,1,1);
}
/*-----------------------------------------------------------------*/
/* genCmpGt :- greater than comparison */
/*-----------------------------------------------------------------*/
static void genCmpGe (iCode * ic) {
- printIc ("genCmpGt", ic, 1,1,1);
+ printIc ("genCmpGe", ic, 1,1,1);
}
/*-----------------------------------------------------------------*/
/* genCmpGt :- greater than comparison */
/*-----------------------------------------------------------------*/
static void genCmpNe (iCode * ic) {
- printIc ("genCmpGt", ic, 1,1,1);
+ printIc ("genCmpNe", ic, 1,1,1);
}
/*-----------------------------------------------------------------*/
/* genCmpLt - less than comparisons */
while (lic) {
/* if operand of the form op = op + <sizeof *op> */
- if (lic->op == '+' && isOperandEqual(IC_LEFT(lic),op) &&
- isOperandEqual(IC_RESULT(lic),op) &&
- isOperandLiteral(IC_RIGHT(lic)) &&
- operandLitValue(IC_RIGHT(lic)) == isize) {
- return lic;
+ if (lic->op == '+') {
+ if (isOperandEqual(IC_LEFT(lic),op) &&
+ //isOperandEqual(IC_RESULT(lic),op) &&
+ isOperandLiteral(IC_RIGHT(lic)) &&
+ operandLitValue(IC_RIGHT(lic)) == isize) {
+ emitcode (";", "Found hasInc");
+ return lic;
+ }
}
/* if the operand used or deffed */
if (bitVectBitValue(OP_USES(op),lic->key) || (unsigned) lic->defKey == op->key) {
/* genOr - code for or */
/*-----------------------------------------------------------------*/
static void genOr (iCode * ic, iCode * ifx) {
+ operand *result=IC_RESULT(ic), *left=IC_LEFT(ic), *right=IC_RIGHT(ic);
+ int size;
+ char *instr;
+
printIc ("genOr", ic, 1,1,1);
+
+ size=aopOp(result, TRUE, TRUE);
+
+ /* if left is a literal, then exchange them */
+ if (IS_LITERAL(operandType(left))) {
+ operand *tmp = right;
+ right = left;
+ left = tmp;
+ }
+
+ if (aopIsBit(result)) {
+ if (IS_LITERAL(operandType(right))) {
+ if (operandLitValue(right)) {
+ emitcode ("setb", AOP_NAME(result)[0]);
+ return;
+ }
+ aopOp(left, TRUE, TRUE);
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[0], toBoolean(left));
+ return;
+ }
+ }
+
+ aopOp(left, !aopIsPtr(result), !aopIsDir(result));
+ aopOp(right, !aopIsPtr(result), !aopIsDir(result));
+
+ if (size>1) {
+ instr="or.w";
+ } else {
+ instr="or.b";
+ }
+ if (!aopEqual(result->aop, left->aop, 0)) {
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+ }
+ emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ if (size>2) {
+ if (!aopEqual(result->aop, left->aop, 1)) {
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[1], AOP_NAME(left)[1]);
+ }
+ emitcode (instr, "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+ }
+ return;
}
/*-----------------------------------------------------------------*/
/* genInline - write the inline code out */
/*-----------------------------------------------------------------*/
static void genInline (iCode * ic) {
+
printIc ("genInline", ic, 0,0,0);
+
+ emitcode ("", IC_INLINE(ic));
}
/*-----------------------------------------------------------------*/
/* genPointerGet - generate code for pointer get */
/*-----------------------------------------------------------------*/
static void genPointerGet (iCode * ic, iCode *pi) {
- printIc ("genPointerGet", ic, 1,1,0);
-#if 0
char *instr, *scratchReg;
operand *result=IC_RESULT(ic), *left=IC_LEFT(ic);
+ bool codePointer=IS_CODEPTR(operandType(left));
+ int size;
+
+ printIc ("genPointerGet", ic, 1,1,0);
if (!IS_PTR(operandType(left))) {
bailOut ("genPointerGet: pointer required");
}
- aopOp(left,FALSE);
- aopOp(result,TRUE);
+ aopOp(left,FALSE,FALSE);
+ size=aopOp(result,TRUE,aopIsDir(left));
if (IS_GENPTR(operandType(left))) {
- emitcode (";", "INLINE\t_gptrget ; %s %s = [%s %s]",
- AOP_NAME(result)[0], AOP_NAME(result)[1],
- AOP_NAME(left)[0], AOP_NAME(left)[1]);
+ symbol *tlbl1=newiTempLabel(NULL);
+ symbol *tlbl2=newiTempLabel(NULL);
+ emitcode ("cmp", "%s,#0x%02x", AOP_NAME(left)[1], CPOINTER);
+ emitcode ("beq", "%05d$", tlbl1->key+100);
+ // far/near pointer
+ if (pi) {
+ emitcode ("mov", "%s,[%s+]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+ pi->generated=1;
+ } else {
+ emitcode ("mov", "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+ }
+ if (size>2) {
+ if (pi) {
+ emitcode ("mov", "%s,[%s+]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
+ } else {
+ emitcode ("mov", "%s,[%s+2]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
+ }
+ }
+ emitcode ("br", "%05d$", tlbl2->key+100);
+ emitcode ("", "%05d$:", tlbl1->key+100);
+ // code pointer
+ if (pi) {
+ emitcode ("movc", "%s,[%s+]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+ pi->generated=1;
+ } else {
+ emitcode ("mov", "r0,%s", AOP_NAME(left)[0]);
+ emitcode ("movc", "%s,[r0+]", AOP_NAME(result)[0]);
+ }
+ if (size>2) {
+ if (pi) {
+ emitcode ("movc", "%s,[%s+]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
+ } else {
+ emitcode ("movc", "%s,[r0+]", AOP_NAME(result)[1]);
+ }
+ }
+ emitcode ("", "%05d$:", tlbl2->key+100);
return;
}
switch (AOP_TYPE(left))
{
- case AOP_CODE:
- if (AOP_SIZE(result)==1) {
- instr=MOVCB;
- } else {
- instr=MOVCW;
- }
- emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
- if (AOP_SIZE(result) > 2) {
- if (AOP_SIZE(result)==3) {
- instr=MOVCB;
- } else {
+ case AOP_REG:
+ if (size>1) {
+ if (codePointer) {
instr=MOVCW;
- }
- emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
- }
- return;
- case AOP_FAR:
- if (AOP_SIZE(result)==1) {
- instr=MOVB;
- } else {
- instr=MOVW;
- }
- emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
- if (AOP_SIZE(result) > 2) {
- if (AOP_SIZE(result)==3) {
- instr=MOVB;
} else {
instr=MOVW;
}
- emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1], AOP_NAME(left)[0]);
- }
- return;
- case AOP_DIR:
- if (AOP_SIZE(result)==1) {
- instr=MOVB;
+ scratchReg=R1;
} else {
- instr=MOVW;
- }
- emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
- if (AOP_SIZE(result) > 2) {
- if (AOP_SIZE(result)==3) {
- instr=MOVB;
+ if (codePointer) {
+ instr=MOVCB;
} else {
- instr=MOVW;
+ instr=MOVB;
}
- emitcode (instr, "%s,%s+2", AOP_NAME(result)[1], AOP_NAME(left)[0]);
- }
- return;
- case AOP_REG:
- if (AOP_SIZE(result)==1) {
- instr=MOVB;
- scratchReg=R0L;
- } else {
- instr=MOVW;
- scratchReg=R0;
+ scratchReg=R1L;
}
- // if result=onstack
if (AOP_TYPE(result)==AOP_STK) {
- emitcode (instr, "%s,[%s]", scratchReg, AOP_NAME(left)[0]);
- emitcode (instr, "%s,%s", AOP_NAME(result)[0], scratchReg);
+ emitcode (MOV, "%s,[%s]", scratchReg, AOP_NAME(left)[0]);
+ emitcode (MOV, "%s,%s", AOP_NAME(result)[0], scratchReg);
} else {
- emitcode (instr, "%s,[%s]", AOP_NAME(result)[0], AOP_NAME(left)[0]);
- }
- if (AOP_SIZE(result) > 2) {
- // result is generic pointer
- sym_link *optype=operandType(left);
- sym_link *opetype=getSpec(optype);
- if (IS_PTR(optype) && !IS_GENPTR(optype)) {
- emitcode (MOVB, "%s,#0x%02x", AOP_NAME(result)[1],
- PTR_TYPE(SPEC_OCLS(opetype)));
+ if (pi) {
+ emitcode (instr, "%s,[%s+]", AOP_NAME(result)[0],
+ AOP_NAME(left)[0]);
+ pi->generated=1;
} else {
- emitcode (MOVB, "%s,[%s]", AOP_NAME(result)[1], AOP_NAME(left)[1]);
+ if (codePointer) {
+ emitcode (MOV, "r1,%s", AOP_NAME(left)[0]);
+ emitcode (instr, "%s,[r1+]", AOP_NAME(result)[0]);
+ } else {
+ emitcode (instr, "%s,[%s]", AOP_NAME(result)[0],
+ AOP_NAME(left)[0]);
+ }
}
}
- return;
- case AOP_STK:
- // if both on stack
- if (AOP_TYPE(result)==AOP_STK) {
- if (AOP_SIZE(result)==1) {
- emitcode ("mov", "r0l,%s", AOP_NAME(left)[0]);
- emitcode ("mov", "%s,r0l", AOP_NAME(result)[0]);
- } else {
- emitcode ("mov", "r0,%s", AOP_NAME(left)[0]);
- emitcode ("mov", "%s,r0", AOP_NAME(result)[0]);
- }
- } else {
- if (AOP_SIZE(result)==1) {
- emitcode ("mov.b", "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
- } else {
- emitcode ("mov.w", "%s,%s", AOP_NAME(result)[0], AOP_NAME(left)[0]);
+ if (size > 2) {
+ if (size==3) {
+ if (codePointer) {
+ instr=MOVCB;
+ } else {
+ instr=MOVB;
+ }
+ scratchReg=R1L;
}
- }
- if (AOP_SIZE(result) > 2) {
if (AOP_TYPE(result)==AOP_STK) {
- if (AOP_SIZE(result)==3) {
- emitcode ("mov", "r0l,%s", AOP_NAME(left)[1]);
- emitcode ("mov", "%s,r0l", AOP_NAME(result)[1]);
+ emitcode (MOV, "%s,[%s+2]", scratchReg, AOP_NAME(left)[0]);
+ emitcode (MOV, "%s,%s", AOP_NAME(result)[1], scratchReg);
+ } else {
+ if (pi) {
+ emitcode (instr, "%s,[%s+]", AOP_NAME(result)[1],
+ AOP_NAME(left)[0]);
} else {
- emitcode ("mov", "r0,%s", AOP_NAME(left)[1]);
- emitcode ("mov", "%s,r0", AOP_NAME(result)[1]);
+ if (codePointer) {
+ emitcode (instr, "%s,[r1]", AOP_NAME(result)[1]);
+ } else {
+ emitcode (instr, "%s,[%s+2]", AOP_NAME(result)[1],
+ AOP_NAME(left)[0]);
+ }
}
- } else {
- emitcode ("mov", "%s,%s", AOP_NAME(result)[1], AOP_NAME(left)[1]);
}
}
return;
}
- bailOut ("genPointerGet: unknown pointer");
-#endif
+ bailOut ("genPointerGet");
}
/*-----------------------------------------------------------------*/
/* genPointerSet - stores the value into a pointer location */
/*-----------------------------------------------------------------*/
static void genPointerSet (iCode * ic, iCode *pi) {
+ char *instr;
+ operand *result=IC_RESULT(ic), *right=IC_RIGHT(ic);
+ int size;
+
printIc ("genPointerSet", ic, 1,0,1);
+
+ if (!IS_PTR(operandType(result))) {
+ bailOut ("genPointerSet: pointer required");
+ }
+
+ aopOp(result,FALSE,FALSE);
+ size=aopOp(right,FALSE, FALSE);
+
+ if (IS_GENPTR(operandType(result))) {
+ emitcode (";", "INLINE\t_gptrset ; [%s %s] = %s %s",
+ AOP_NAME(result)[0], AOP_NAME(result)[1],
+ AOP_NAME(right)[0], AOP_NAME(right)[1]);
+ return;
+ }
+
+ switch (AOP_TYPE(right))
+ {
+ case AOP_LIT:
+ case AOP_REG:
+ if (size>1) {
+ instr=MOVW;
+ } else {
+ instr=MOVB;
+ }
+ if (pi) {
+ emitcode (instr, "[%s+],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ pi->generated=1;
+ } else {
+ emitcode (instr, "[%s],%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ }
+ if (size > 2) {
+ if (size==3) {
+ instr=MOVB;
+ }
+ if (pi) {
+ emitcode (instr, "[%s+],%s", AOP_NAME(result)[0],
+ AOP_NAME(right)[1]);
+ } else {
+ emitcode (instr, "[%s+2],%s", AOP_NAME(result)[0],
+ AOP_NAME(right)[1]);
+ }
+ }
+ return;
+ }
+ bailOut ("genPointerSet");
}
/*-----------------------------------------------------------------*/
/* genIfx - generate code for Ifx statement */
/*-----------------------------------------------------------------*/
static void genIfx (iCode * ic, iCode * popIc) {
+ int size;
char *instr;
bool trueOrFalse;
symbol *jlbl, *tlbl;
operand *cond=IC_COND(ic);
- emitcode (";", "genIfx cond=%s trueLabel:%s falseLabel:%s",
- printOp(cond),
+ emitcode (";", "genIfx(%d) cond=%s trueLabel:%s falseLabel:%s",
+ ic->lineno, printOp(cond),
IC_TRUE(ic) ? IC_TRUE(ic)->name : "NULL",
IC_FALSE(ic) ? IC_FALSE(ic)->name : "NULL");
- aopOp(cond,FALSE,FALSE);
+ size=aopOp(cond,TRUE,TRUE);
if (IC_TRUE(ic)) {
trueOrFalse=TRUE;
switch (AOP_TYPE(cond) )
{
case AOP_BIT:
- emitcode (trueOrFalse ? "jb" : "jbc", "%s,%05d$",
+ emitcode (trueOrFalse ? "jb" : "jnb", "%s,%05d$",
AOP_NAME(cond)[0], jlbl->key+100);
return;
case AOP_REG:
case AOP_FAR:
case AOP_STK:
tlbl=newiTempLabel(NULL);
- if (AOP_SIZE(cond)==1) {
- instr="cmp.b";
- } else {
+ if (size>1) {
instr="cmp.w";
+ } else {
+ instr="cmp.b";
}
emitcode (instr, "%s,#0", AOP_NAME(cond)[0]);
emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
- if (AOP_SIZE(cond) > 2) {
- if (AOP_SIZE(cond)==3) {
- instr="cmp.b";
+ if (size > 2) {
+ if (size==3) {
+ // generic pointer, forget the generic part
} else {
- instr="cmp.w";
+ emitcode (instr, "%s,#0", AOP_NAME(cond)[1]);
+ emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
}
- emitcode (instr, "%s,#0", AOP_NAME(cond)[1]);
- emitcode (trueOrFalse ? "beq" : "bne", "%05d$", tlbl->key+100);
}
emitcode ("jmp", "%05d$", jlbl->key+100);
emitcode ("", "%05d$:", tlbl->key+100);
/* genAddrOf - generates code for address of */
/*-----------------------------------------------------------------*/
static void genAddrOf (iCode * ic) {
+ int size;
operand *left=IC_LEFT(ic);
printIc ("genAddrOf", ic, 1,1,0);
- aopOp (IC_RESULT(ic),TRUE, FALSE);
+ size=aopOp (IC_RESULT(ic), FALSE, TRUE);
if (isOperandOnStack(left)) {
emitcode ("lea", "%s,%s", AOP_NAME(IC_RESULT(ic))[0],
getStackOffset(OP_SYMBOL(left)->stack));
- if (AOP_SIZE(IC_RESULT(ic)) > 2) {
+ if (size > 2) {
// this must be a generic pointer
emitcode ("mov", "%s,#0x%02x", AOP_NAME(IC_RESULT(ic))[1], FPOINTER);
}
isOperandInCodeSpace(left)) {
emitcode ("mov.w", "%s,#%s", AOP_NAME(IC_RESULT(ic))[0],
OP_SYMBOL(left)->rname);
- if (AOP_SIZE(IC_RESULT(ic)) > 2) {
+ if (size > 2) {
// this must be a generic pointer
int space=0; // dir space
if (isOperandInFarSpace(left)) {
bailOut("genAssign: result is not a symbol");
}
- aopOp(result, TRUE, FALSE);
- aopOp(right, FALSE, aopIsPtr(result));
+ aopOp(result, TRUE, TRUE);
+ aopOp(right, !aopIsPtr(result), AOP_TYPE(result)!=AOP_DIR);
size=AOP_SIZE(result);
/* if result is a bit */
return;
}
/* we need to or */
- emitcode ("mov", "%s,%s", AOP_NAME(result), toBoolean(right));
+ emitcode ("mov", "%s,%s; toBoolean", AOP_NAME(result), toBoolean(right));
return;
}
/* general case */
- if (size==1) {
- instr=MOVB;
- } else {
+ if (size>1) {
instr=MOVW;
+ } else {
+ instr=MOVB;
}
emitcode (instr, "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
- if (AOP_SIZE(result) > 2) {
+ if (size > 2) {
if (size==3) {
// generic pointer
instr=MOVB;
- } else {
- instr=MOVW;
}
emitcode (instr, "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
return;
/* genCast - gen code for casting */
/*-----------------------------------------------------------------*/
static void genCast (iCode * ic) {
+ int size;
+ operand *result=IC_RESULT(ic);
+ operand *right=IC_RIGHT(ic);
+ sym_link *ctype=operandType(IC_LEFT(ic));
+ sym_link *rtype=operandType(IC_RIGHT(ic));
+ sym_link *etype=getSpec(rtype);
+ short ptrType, signedness;
+
printIc ("genCast", ic, 1,1,1);
+
+ aopOp(result, TRUE, TRUE);
+ aopOp(right, !aopIsPtr(result), AOP_TYPE(result)!=AOP_DIR);
+ size=AOP_SIZE(result);
+
+ /* if result is a bit */
+ if (AOP_TYPE(result) == AOP_BIT) {
+ /* if right is literal, we know what the value is */
+ if (AOP_TYPE(right) == AOP_LIT) {
+ if (operandLitValue(right)) {
+ emitcode ("setb", AOP_NAME(result)[0]);
+ } else {
+ emitcode ("clr", AOP_NAME(result)[0]);
+ }
+ return;
+ }
+ /* if right is also a bit */
+ if (AOP_TYPE(right) == AOP_BIT) {
+ emitcode ("mov", "c,%s", AOP_NAME(right));
+ emitcode ("mov", "%s,c", AOP_NAME(result));
+ return;
+ }
+ /* we need to or */
+ emitcode ("mov", "%s,%s; toBoolean", AOP_NAME(result), toBoolean(right));
+ return;
+ }
+
+ /* if right is a bit */
+ if (AOP_TYPE(right)==AOP_BIT) {
+ emitcode ("mov", "c,%s", AOP_NAME(right));
+ emitcode ("mov", "%s,#0", AOP_NAME(result)[0]);
+ emitcode ("rlc", "%s,#1", AOP_NAME(result)[0]);
+ if (size>2) {
+ emitcode ("mov", "%s,#0", AOP_NAME(result)[1]);
+ }
+ return;
+ }
+
+ /* if the result is of type pointer */
+ if (IS_PTR (ctype)) {
+
+ if (AOP_SIZE(right)>1) {
+ emitcode ("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ } else {
+ emitcode ("mov", "r1l,%s", AOP_NAME(right)[0]);
+ emitcode ("sext", "r1h");
+ emitcode ("mov", "%s,r1", AOP_NAME(result)[0]);
+ }
+
+ /* if pointer to generic pointer */
+ if (IS_GENPTR (ctype)) {
+
+ if (IS_GENPTR (rtype)) {
+ bailOut("genCast: gptr -> gptr");
+ }
+
+ if (IS_PTR (rtype)) {
+ ptrType = DCL_TYPE (rtype);
+ } else {
+ /* we have to go by the storage class */
+ if (!SPEC_OCLS(etype)) {
+ bailOut("genCast: unknown storage class");
+ } else {
+ ptrType = PTR_TYPE (SPEC_OCLS (etype));
+ }
+ }
+
+ /* the generic part depends on the type */
+ switch (ptrType)
+ {
+ case POINTER:
+ emitcode ("mov", "%s,#0x00", AOP_NAME(result)[1]);
+ break;
+ case FPOINTER:
+ emitcode ("mov", "%s,#0x01", AOP_NAME(result)[1]);
+ break;
+ case CPOINTER:
+ emitcode ("mov", "%s,#0x02", AOP_NAME(result)[1]);
+ break;
+ default:
+ bailOut("genCast: got unknown storage class");
+ }
+ }
+ return;
+ }
+
+ /* do we have to sign extend? */
+ signedness = SPEC_USIGN(rtype);
+
+ /* now depending on the size */
+ switch ((AOP_SIZE(result)<<4) + AOP_SIZE(right))
+ {
+ case 0x44:
+ case 0x33:
+ emitcode("mov", "%s,%s", AOP_NAME(result)[1], AOP_NAME(right)[1]);
+ // fall through
+ case 0x24:
+ case 0x22:
+ case 0x11:
+ emitcode("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ return;
+ case 0x41:
+ case 0x21:
+ emitcode("mov", "r1l,%s", AOP_NAME(right)[0]);
+ if (signedness) {
+ emitcode("sext", "r1h");
+ } else {
+ emitcode("mov", "rlh,#0");
+ }
+ emitcode("mov", "%s,r1", AOP_NAME(result)[0]);
+ if (size==2)
+ return;
+ // fall through: case 0x41
+ emitcode("sext", AOP_NAME(result)[1]);
+ return;
+ case 14:
+ case 12:
+ emitcode("mov", "r1,%s", AOP_NAME(right)[0]);
+ emitcode("mov", "%s,r1l", AOP_NAME(result)[0]);
+ return;
+ case 24:
+ emitcode("mov", "%s,%s", AOP_NAME(result)[0], AOP_NAME(right)[0]);
+ return;
+ }
+ bailOut("genCast: unknown size");
}
+
/*-----------------------------------------------------------------*/
/* genDjnz - generate decrement & jump if not zero instrucion */
/*-----------------------------------------------------------------*/
lbl = newiTempLabel (NULL);
lbl1 = newiTempLabel (NULL);
- aopOp (IC_RESULT (ic), TRUE, FALSE);
+ aopOp (IC_RESULT (ic), FALSE, TRUE);
if (AOP_TYPE(IC_RESULT(ic))==AOP_REG || AOP_TYPE(IC_RESULT(ic))==AOP_DIR) {
emitcode ("djnz", "%s,%05d$", AOP_NAME(IC_RESULT(ic)), lbl->key+100);
lineHead = lineCurr = NULL;
- /* print the allocation information */
- if (allocInfo)
- printAllocInfo (currFunc, codeOutFile);
-
/* if debug information required */
if (options.debug && currFunc)
{
}
for (ic = lic; ic; ic = ic->next) {
- if (cln != ic->lineno) {
+ if (ic->lineno && cln != ic->lineno) {
if (options.debug) {
_G.debugLine = 1;
emitcode ("", "C$%s$%d$%d$%d ==.",
break;
case GET_VALUE_AT_ADDRESS:
- genPointerGet (ic, hasInc(IC_LEFT(ic), ic, getSize(operandType(IC_LEFT(ic)))));
+ genPointerGet (ic, hasInc(IC_LEFT(ic), ic, getSize(operandType(IC_RESULT(ic)))));
break;
case '=':