#include <string.h>
#include <ctype.h>
-#include <common.h>
+#include "common.h"
#include "ralloc.h"
#include "gen.h"
#include "SDCCglobl.h"
}
}
+#ifdef KEVIN_SCREWED_UP
// Macro to aopOp all three operands of an ic. If this cannot be done,
// the IC_LEFT and IC_RIGHT operands will be aopOp'd, and the rc parameter
// will be set TRUE. The caller must then handle the case specially, noting
aopOp (IC_RIGHT(ic),ic,FALSE, FALSE); \
aopOp (IC_LEFT(ic),ic,FALSE, (AOP_TYPE(IC_RIGHT(ic)) == AOP_DPTR));
+#else // Kevin didn't screw up...
+
+#define AOP_IS_STR(x) (IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
+
+// The guts of AOP_OP_3_NOFATAL. Generates the left & right opcodes of an IC,
+// generates the result if possible. If result is generated, returns TRUE; otherwise
+// returns false and caller must deal with fact that result isn't aopOp'd.
+bool aopOp3(iCode * ic)
+{
+ bool dp1InUse, dp2InUse;
+
+ // First, generate the right opcode. DPTR may be used if neither left nor result are
+ // of type AOP_STR.
+
+// D(emitcode(";", "aopOp3: AOP_IS_STR left: %s right: %s result: %s",
+// AOP_IS_STR(IC_LEFT(ic)) ? "true" : "false",
+// AOP_IS_STR(IC_RIGHT(ic)) ? "true" : "false",
+// AOP_IS_STR(IC_RESULT(ic)) ? "true" : "false");
+// );
+
+ aopOp (IC_RIGHT(ic),ic,FALSE, AOP_IS_STR(IC_LEFT(ic)) || AOP_IS_STR(IC_RESULT(ic)));
+
+ // Now, the left opcode. This can use DPTR if the right didn't AND the result is not
+ // AOP_STR (however, if the result is the same operand as the left, then DPTR may be used).
+ aopOp (IC_LEFT(ic),ic,FALSE, AOP_USESDPTR(IC_RIGHT(ic)) ||
+ (AOP_IS_STR(IC_RESULT(ic)) && !isOperandEqual(IC_LEFT(ic),IC_RESULT(ic))));
+
+ // We've op'd the left. So, if left & result are the same operand, we know aopOp
+ // will succeed, and we can just do it & bail.
+ if (isOperandEqual(IC_LEFT(ic),IC_RESULT(ic)))
+ {
+// D(emitcode(";", "aopOp3: left & result equal"););
+ aopOp(IC_RESULT(ic),ic,TRUE, FALSE);
+ return TRUE;
+ }
+
+ // Note which dptrs are currently in use.
+ dp1InUse = (AOP_TYPE(IC_RIGHT(ic)) == AOP_DPTR) || (AOP_TYPE(IC_LEFT(ic)) == AOP_DPTR) ||
+ AOP_IS_STR(IC_RIGHT(ic)) || AOP_IS_STR(IC_LEFT(ic));
+ dp2InUse = (AOP_TYPE(IC_RIGHT(ic)) == AOP_DPTR2) || (AOP_TYPE(IC_LEFT(ic)) == AOP_DPTR2);
+
+ // OK, now if either left or right uses DPTR and the result is an AOP_STR, we cannot generate it.
+ if (dp1InUse && AOP_IS_STR(IC_RESULT(ic)))
+ {
+ return FALSE;
+ }
+
+ // or, if both dp1 & dp2 are in use and the result needs a dptr, we're out of luck
+ if (dp1InUse && dp2InUse && isOperandInFarSpace(IC_RESULT(ic)))
+ {
+ return FALSE;
+ }
+
+ aopOp (IC_RESULT(ic),ic,TRUE, dp1InUse);
+
+ // SOme sanity checking...
+ if (AOP_TYPE(IC_LEFT(ic)) == AOP_DPTR2 &&
+ AOP_TYPE(IC_RESULT(ic)) == AOP_DPTR2)
+ {
+ fprintf(stderr,
+ "Ack: got unexpected DP2! (%s:%d %s:%d)\n",
+ __FILE__, __LINE__, ic->filename, ic->lineno);
+ }
+
+ return TRUE;
+}
+
+// Macro to aopOp all three operands of an ic. If this cannot be done,
+// the IC_LEFT and IC_RIGHT operands will be aopOp'd, and the rc parameter
+// will be set TRUE. The caller must then handle the case specially, noting
+// that the IC_RESULT operand is not aopOp'd.
+//
+#define AOP_OP_3_NOFATAL(ic, rc) \
+ do { rc = !aopOp3(ic); } while (0)
+
+// aopOp the left & right operands of an ic.
+#define AOP_OP_2(ic) \
+ aopOp (IC_RIGHT(ic),ic,FALSE, AOP_IS_STR(IC_LEFT(ic))); \
+ aopOp (IC_LEFT(ic),ic,FALSE, AOP_USESDPTR(IC_RIGHT(ic)));
+
+#endif
+
// convienience macro.
#define AOP_SET_LOCALS(ic) \
left = IC_LEFT(ic); \
aopOp (IC_RIGHT (ic), ic, FALSE,
(AOP_INDPTRn(IC_LEFT(ic)) ? FALSE : (AOP_USESDPTR(IC_LEFT(ic)) ? TRUE : FALSE)));
if ((AOP_TYPE (IC_LEFT (ic)) == AOP_DPTR) &&
- (AOP_TYPE (IC_RIGHT (ic)) == AOP_DPTR2))
+ ((AOP_TYPE (IC_RIGHT (ic)) == AOP_DPTR2)
+ || OP_SYMBOL(IC_RESULT(ic))->ruonly))
{
pushResult = TRUE;
}
}
/*-----------------------------------------------------------------*/
-/* emitcodePointerGet - gget value from code space */
+/* genCodePointerGet - get value from code space */
/*-----------------------------------------------------------------*/
static void
-emitcodePointerGet (operand * left,
+genCodePointerGet (operand * left,
operand * result, iCode * ic, iCode *pi)
{
int size, offset, dopi=1;
else
{ /* we need to get it byte by byte */
_startLazyDPSEvaluation ();
+#if 0 // I see no point at all to this code.
+ // So I yanked it. Kill at some future date if no bugs rear their heads.
if (AOP(left)->type==AOP_DPTR2) {
char *l;
l=aopGet(AOP(left),0,FALSE,FALSE,TRUE);
} else {
emitcode ("mov", "b,%s", aopGet (AOP(left),2,FALSE,FALSE,TRUE));
}
- } else {
+ }
+ else
+#endif
+ {
emitcode ("mov", "dpl,%s", aopGet (AOP(left),0,FALSE,FALSE,TRUE));
emitcode ("mov", "dph,%s", aopGet (AOP(left),1,FALSE,FALSE,TRUE));
if (options.model == MODEL_FLAT24) {
_endLazyDPSEvaluation ();
}
}
- /* so dptr know contains the address */
+
+ /* so dptr-b now contains the address */
+ _G.bInUse++;
aopOp (result, ic, FALSE, TRUE);
+ _G.bInUse--;
/* if bit then unpack */
if (IS_BITVAR (retype) || IS_BITVAR (letype))
+ {
genUnpackBits (result, "dptr", GPOINTER);
+ }
else
{
- size = AOP_SIZE (result);
- offset = 0;
+ size = AOP_SIZE (result);
+ offset = 0;
- while (size--)
+ while (size--)
{
- emitcode ("lcall", "__gptrget");
- aopPut (AOP (result), "a", offset++);
- if (size || (pi && AOP_TYPE (left) != AOP_IMMD))
- emitcode ("inc", "dptr");
+ if (size)
+ {
+ // Get two bytes at a time, results in _AP & A.
+ // dptr will be incremented ONCE by __gptrgetWord.
+ //
+ // Note: any change here must be coordinated
+ // with the implementation of __gptrgetWord
+ // in device/lib/_gptrget.c
+ emitcode ("lcall", "__gptrgetWord");
+ aopPut (AOP (result), DP2_RESULT_REG, offset++);
+ aopPut (AOP (result), "a", offset++);
+ size--;
+ }
+ else
+ {
+ // Only one byte to get.
+ emitcode ("lcall", "__gptrget");
+ aopPut (AOP (result), "a", offset++);
+ }
+
+ if (size || (pi && AOP_TYPE (left) != AOP_IMMD))
+ {
+ emitcode ("inc", "dptr");
+ }
}
}
if (p_type == GPOINTER && OP_SYMBOL(left)->remat &&
IS_CAST_ICODE(OP_SYMBOL(left)->rematiCode)) {
left = IC_RIGHT(OP_SYMBOL(left)->rematiCode);
- type = type = operandType (left);
+ type = operandType (left);
p_type = DCL_TYPE (type);
}
/* now that we have the pointer type we assign
break;
case CPOINTER:
- emitcodePointerGet (left, result, ic, pi);
+ genCodePointerGet (left, result, ic, pi);
break;
case GPOINTER:
if (p_type == GPOINTER && OP_SYMBOL(result)->remat &&
IS_CAST_ICODE(OP_SYMBOL(result)->rematiCode)) {
result = IC_RIGHT(OP_SYMBOL(result)->rematiCode);
- type = type = operandType (result);
+ type = operandType (result);
p_type = DCL_TYPE (type);
}
case GPOINTER:
genGenPointerSet (right, result, ic, pi);
break;
+
+ default:
+ werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+ "genPointerSet: illegal pointer type");
}
}
/* pointer to generic pointer */
if (IS_GENPTR (ctype))
{
- char *l = zero;
-
if (IS_PTR (type))
{
p_type = DCL_TYPE (type);
_endLazyDPSEvaluation ();
/* the last byte depending on type */
- switch (p_type)
{
- case IPOINTER:
- case POINTER:
- l = zero;
- break;
- case FPOINTER:
- l = one;
- break;
- case CPOINTER:
- l = "#0x02";
- break;
- case PPOINTER:
- l = "#0x03";
- break;
-
- default:
- /* this should never happen */
- werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
- "got unknown pointer type");
- exit (1);
+ int gpVal = pointerTypeToGPByte(p_type, NULL, NULL);
+ char gpValStr[10];
+
+ if (gpVal == -1)
+ {
+ // pointerTypeToGPByte will have bitched.
+ exit(1);
+ }
+
+ sprintf(gpValStr, "#0x%d", gpVal);
+ aopPut (AOP (result), gpValStr, GPTRSIZE - 1);
}
- aopPut (AOP (result), l, GPTRSIZE - 1);
goto release;
}