Better reg packing, first peephole 20038 163 1873
With assign packing 19281 165 1849
5/3/00 17741 185 17B6
+ With reg params for mul and div 16234 202 162D
Michael Hope <michaelh@earthling.net> 2000
Based on the mcs51 generator -
int pushed_de;
} stack;
int frameId;
+ bool flush_statics;
+ bool in_home;
} _G;
static char *aopGet(asmop *aop, int offset, bool bit16);
/* otherwise it is fairly simple */
if (!IS_FLOAT(val->type)) {
unsigned long v = floatFromVal(val);
+
if (offset == 2)
v >>= 16;
+
if (with_hash)
tsprintf(buffer, "!immedword", v);
else
}
}
+static char *fetchLitSpecial(asmop *aop, bool negate, bool xor)
+{
+ unsigned long v;
+ value * val = aop->aopu.aop_lit;
+
+ wassert(aop->type == AOP_LIT);
+ wassert(!IS_FLOAT(val->type));
+
+ v = floatFromVal(val);
+
+ if (xor)
+ v ^= 0x8000;
+ if (negate)
+ v = -v;
+ v &= 0xFFFF;
+
+ tsprintf(buffer, "!immedword", v);
+ return gc_strdup(buffer);
+}
+
static void fetchLitPair(PAIR_ID pairId, asmop *left, int offset)
{
const char *l;
const char *pair = _pairs[pairId].name;
- l = aopGetLitWordLong(left, offset, FALSE);
+ l = aopGetLitWordLong(left, 0, FALSE);
wassert(l && pair);
if (isPtr(pair)) {
_G.pairs[pairId].lit = gc_strdup(l);
_G.pairs[pairId].offset = offset;
}
+ if (IS_GB && pairId == PAIR_DE && 0) {
+ if (_G.pairs[pairId].lit && !strcmp(_G.pairs[pairId].lit, l)) {
+ if (abs(_G.pairs[pairId].offset - offset) < 3) {
+ adjustPair(pair, &_G.pairs[pairId].offset, offset);
+ return;
+ }
+ }
+ _G.pairs[pairId].last_type = left->type;
+ _G.pairs[pairId].lit = gc_strdup(l);
+ _G.pairs[pairId].offset = offset;
+ }
/* Both a lit on the right and a true symbol on the left */
- /* PENDING: for re-target */
-#if 0
if (offset)
- emit2("ld %s,!hashedstr + %d", pair, l, offset);
- else
-#endif
+ emit2("ld %s,!hashedstr + %u", pair, l, offset);
+ else
emit2("ld %s,!hashedstr", pair, l);
}
else { /* we need to get it byte by byte */
if (pairId == PAIR_HL && IS_GB && requiresHL(aop)) {
aopGet(aop, offset, FALSE);
- emit2("!ldahli");
- emit2("ld h,!*hl");
- emit2("ld l,a");
+ switch (aop->size) {
+ case 1:
+ emit2("ld l,!*hl");
+ emit2("ld h,!immedbyte", 0);
+ break;
+ case 2:
+ emit2("!ldahli");
+ emit2("ld h,!*hl");
+ emit2("ld l,a");
+ break;
+ default:
+ emit2("; WARNING: mlh woosed out. This code is invalid.");
+ }
}
else if (IS_Z80 && aop->type == AOP_IY) {
/* Instead of fetching relative to IY, just grab directly
tsprintf(s, "!*hl");
}
else {
- tsprintf(s,"!*ixx", aop->aopu.aop_stk+offset);
+ if (aop->aopu.aop_stk >= 0)
+ offset += _G.stack.param_offset;
+ tsprintf(s,"!*ixx ; x", aop->aopu.aop_stk+offset);
}
ALLOC_ATOMIC(rs,strlen(s)+1);
strcpy(rs,s);
emit2("ld !*hl,%s", s);
}
else {
+ if (aop->aopu.aop_stk >= 0)
+ offset += _G.stack.param_offset;
if (!canAssignToPtr(s)) {
emit2("ld a,%s", s);
emit2("ld !*ixx,a", aop->aopu.aop_stk+offset);
/** Take the value in carry and put it into a register
*/
-void outBitC(operand *result)
+void outBitCLong(operand *result, bool swap_sense)
{
/* if the result is bit */
if (AOP_TYPE(result) == AOP_CRY) {
else {
emit2("ld a,!zero");
emit2("rla");
+ if (swap_sense)
+ emit2("xor a,!immedbyte", 1);
outAcc(result);
}
}
+void outBitC(operand *result)
+{
+ outBitCLong(result, FALSE);
+}
+
/*-----------------------------------------------------------------*/
/* toBoolean - emit code for orl a,operator(sizeop) */
/*-----------------------------------------------------------------*/
return ret;
}
+/* This is quite unfortunate */
+static void setArea(int inHome)
+{
+ static int lastArea = 0;
+
+ /*
+ if (_G.in_home != inHome) {
+ if (inHome) {
+ const char *sz = port->mem.code_name;
+ port->mem.code_name = "HOME";
+ emit2("!area", CODE_NAME);
+ port->mem.code_name = sz;
+ }
+ else
+ emit2("!area", CODE_NAME);*/
+ _G.in_home = inHome;
+ // }
+}
+
+static bool isInHome(void)
+{
+ return _G.in_home;
+}
+
/** Emit the code for a call statement
*/
static void emitCall(iCode *ic, bool ispcall)
int pushed_de = 0;
link *detype = getSpec(operandType(IC_LEFT(ic)));
- if (IS_BANKED(detype))
- emit2("; call to a banked function");
-
/* if caller saves & we have not saved then */
if (!ic->regsSaved) {
/* PENDING */
}
if (ispcall) {
- if (IS_BANKED(detype)) {
+ if (IS_BANKEDCALL(detype)) {
werror(W_INDIR_BANKED);
}
aopOp(IC_LEFT(ic),ic,FALSE, FALSE);
char *name = OP_SYMBOL(IC_LEFT(ic))->rname[0] ?
OP_SYMBOL(IC_LEFT(ic))->rname :
OP_SYMBOL(IC_LEFT(ic))->name;
- if (IS_BANKED(detype)) {
+ if (IS_BANKEDCALL(detype)) {
emit2("call banked_call");
emit2("!dws", name);
emit2("!dw !bankimmeds", name);
static void genCall (iCode *ic)
{
link *detype = getSpec(operandType(IC_LEFT(ic)));
- if (IS_BANKED(detype)) emit2("; call to a banked function");
emitCall(ic, FALSE);
}
return 0;
}
+extern set *publics;
+
/*-----------------------------------------------------------------*/
/* genFunction - generated code for function entry */
/*-----------------------------------------------------------------*/
link *fetype;
nregssaved = 0;
+ setArea(IS_NONBANKED(sym->etype));
+
+ /* PENDING: hack */
+ if (!IS_STATIC(sym->etype)) {
+ addSetIfnotP(&publics, sym);
+ }
+
/* create the function header */
emit2("!functionheader", sym->name);
/* PENDING: portability. */
/* if critical function then turn interrupts off */
if (SPEC_CRTCL(fetype))
emit2("!di");
- if (SPEC_BANKED(fetype)) emit2("; Iam banked");
/* if this is an interrupt service routine then
save acc, b, dpl, dph */
/* PENDING: portability. */
emit2("__%s_end:", sym->rname);
}
+ _G.flush_statics = 1;
_G.stack.pushed = 0;
_G.stack.offset = 0;
}
if (AOP_TYPE(IC_LEFT(ic)) == AOP_STK ||
AOP_TYPE(IC_RIGHT(ic)) == AOP_STK ||
AOP_TYPE(IC_RESULT(ic)) == AOP_STK) {
- if (size == 2) {
+ if ((AOP_SIZE(IC_LEFT(ic)) == 2 ||
+ AOP_SIZE(IC_RIGHT(ic)) == 2) &&
+ (AOP_SIZE(IC_LEFT(ic)) <= 2 &&
+ AOP_SIZE(IC_RIGHT(ic)) <= 2)) {
if (getPairId(AOP(IC_RIGHT(ic))) == PAIR_BC) {
/* Swap left and right */
operand *t = IC_RIGHT(ic);
if (AOP_TYPE(IC_LEFT(ic)) == AOP_STK ||
AOP_TYPE(IC_RIGHT(ic)) == AOP_STK ||
AOP_TYPE(IC_RESULT(ic)) == AOP_STK) {
- if (size == 2) {
+ if ((AOP_SIZE(IC_LEFT(ic)) == 2 ||
+ AOP_SIZE(IC_RIGHT(ic)) == 2) &&
+ (AOP_SIZE(IC_LEFT(ic)) <= 2 &&
+ AOP_SIZE(IC_RIGHT(ic)) <= 2)) {
PAIR_ID left = getPairId(AOP(IC_LEFT(ic)));
PAIR_ID right = getPairId(AOP(IC_RIGHT(ic)));
/* if true label then we jump if condition
supplied is true */
- if ( IC_TRUE(ic) ) {
+ if (IC_TRUE(ic)) {
jlbl = IC_TRUE(ic);
if (!strcmp(jval, "a")) {
inst = "nz";
else if (!strcmp(jval, "c")) {
inst = "c";
}
+ else if (!strcmp(jval, "nc")) {
+ inst = "nc";
+ }
else {
/* The buffer contains the bit on A that we should test */
inst = "nz";
else if (!strcmp(jval, "c")) {
inst = "nc";
}
+ else if (!strcmp(jval, "nc")) {
+ inst = "c";
+ }
else {
/* The buffer contains the bit on A that we should test */
inst = "z";
}
else if (!strcmp(jval, "c")) {
}
+ else if (!strcmp(jval, "nc")) {
+ }
else {
emitcode("bit", "%s,a", jval);
}
ic->generated = 1;
}
+static const char *_getPairIdName(PAIR_ID id)
+{
+ return _pairs[id].name;
+}
+
/** Generic compare for > or <
*/
static void genCmp (operand *left,operand *right,
{
int size, offset = 0 ;
unsigned long lit = 0L;
+ bool swap_sense = FALSE;
/* if left & right are bit variables */
if (AOP_TYPE(left) == AOP_CRY &&
emitcode("cp", "%s", aopGet(AOP(right), offset, FALSE));
}
else {
+ /* Special cases:
+ On the GB:
+ If the left or the right is a lit:
+ Load -lit into HL, add to right via, check sense.
+ */
+ if (size == 2 && (AOP_TYPE(right) == AOP_LIT || AOP_TYPE(left) == AOP_LIT)) {
+ PAIR_ID id = PAIR_DE;
+ asmop *lit = AOP(right);
+ asmop *op = AOP(left);
+ swap_sense = TRUE;
+
+ if (AOP_TYPE(left) == AOP_LIT) {
+ swap_sense = FALSE;
+ lit = AOP(left);
+ op = AOP(right);
+ }
+ if (sign) {
+ emit2("ld e,%s", aopGet(op, 0, 0));
+ emit2("ld a,%s", aopGet(op, 1, 0));
+ emit2("xor a,!immedbyte", 0x80);
+ emit2("ld d,a");
+ }
+ else {
+ id = getPairId(op);
+ if (id == PAIR_INVALID) {
+ fetchPair(PAIR_DE, op);
+ id = PAIR_DE;
+ }
+ }
+ spillPair(PAIR_HL);
+ emit2("ld hl,%s", fetchLitSpecial(lit, TRUE, sign));
+ emit2("add hl,%s", _getPairIdName(id));
+ goto release;
+ }
if(AOP_TYPE(right) == AOP_LIT) {
lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit);
/* optimize if(x < 0) or if(x >= 0) */
release:
if (AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) {
- outBitC(result);
+ outBitCLong(result, swap_sense);
} else {
/* if the result is used in the next
ifx conditional branch then generate
code a little differently */
- if (ifx )
- genIfxJump (ifx,"c");
+ if (ifx)
+ genIfxJump(ifx, swap_sense ? "nc" : "c");
else
- outBitC(result);
+ outBitCLong(result, swap_sense);
/* leave the result in acc */
}
}
}
if (size) {
emit2("inc %s", _pairs[pair].name);
+ _G.pairs[pair].offset++;
}
}
}
}
if (size) {
emitcode("inc", _pairs[pairId].name);
+ _G.pairs[pairId].offset++;
}
offset++;
}
spillCached();
if (sym->onStack) {
/* if it has an offset then we need to compute it */
- emitcode("ld", "hl,#%d+%d+%d", sym->stack, _G.stack.pushed, _G.stack.offset);
+ if (sym->stack > 0)
+ emitcode("ld", "hl,#%d+%d+%d+%d", sym->stack, _G.stack.pushed, _G.stack.offset, _G.stack.param_offset);
+ else
+ emitcode("ld", "hl,#%d+%d+%d", sym->stack, _G.stack.pushed, _G.stack.offset);
emitcode("add", "hl,sp");
}
else {
if (!options.nopeep)
peepHole (&lineHead);
+ /* This is unfortunate */
/* now do the actual printing */
- printLine (lineHead,codeOutFile);
- return;
+ {
+ FILE *fp = codeOutFile;
+ if (isInHome() && codeOutFile == code->oFile)
+ codeOutFile = home->oFile;
+ printLine (lineHead, codeOutFile);
+ if (_G.flush_statics) {
+ flushStatics();
+ _G.flush_statics = 0;
+ }
+ codeOutFile = fp;
+ }
}