+/* pic16_initPointer - pointer initialization code massaging */
+/*-----------------------------------------------------------------*/
+value *pic16_initPointer (initList * ilist, sym_link *toType)
+{
+ value *val;
+ ast *expr;
+
+ if (!ilist) {
+ return valCastLiteral(toType, 0.0);
+ }
+
+ expr = decorateType(resolveSymbols( list2expr (ilist) ), FALSE);
+// expr = list2expr( ilist );
+
+ if (!expr)
+ goto wrong;
+
+ /* try it the old way first */
+ if ((val = constExprValue (expr, FALSE)))
+ return val;
+
+ /* ( ptr + constant ) */
+ if (IS_AST_OP (expr) &&
+ (expr->opval.op == '+' || expr->opval.op == '-') &&
+ IS_AST_SYM_VALUE (expr->left) &&
+ (IS_ARRAY(expr->left->ftype) || IS_PTR(expr->left->ftype)) &&
+ compareType(toType, expr->left->ftype) &&
+ IS_AST_LIT_VALUE (expr->right)) {
+ return valForCastAggr (expr->left, expr->left->ftype,
+ expr->right,
+ expr->opval.op);
+ }
+
+ /* (char *)&a */
+ if (IS_AST_OP(expr) && expr->opval.op==CAST &&
+ IS_AST_OP(expr->right) && expr->right->opval.op=='&') {
+ if (compareType(toType, expr->left->ftype)!=1) {
+ werror (W_INIT_WRONG);
+ printFromToType(expr->left->ftype, toType);
+ }
+ // skip the cast ???
+ expr=expr->right;
+ }
+
+ /* no then we have to do these cludgy checks */
+ /* pointers can be initialized with address of
+ a variable or address of an array element */
+ if (IS_AST_OP (expr) && expr->opval.op == '&') {
+ /* address of symbol */
+ if (IS_AST_SYM_VALUE (expr->left)) {
+ val = copyValue (AST_VALUE (expr->left));
+ val->type = newLink (DECLARATOR);
+ if(SPEC_SCLS (expr->left->etype) == S_CODE) {
+ DCL_TYPE (val->type) = CPOINTER;
+ DCL_PTR_CONST (val->type) = port->mem.code_ro;
+ }
+ else if (SPEC_SCLS (expr->left->etype) == S_XDATA)
+ DCL_TYPE (val->type) = FPOINTER;
+ else if (SPEC_SCLS (expr->left->etype) == S_XSTACK)
+ DCL_TYPE (val->type) = PPOINTER;
+ else if (SPEC_SCLS (expr->left->etype) == S_IDATA)
+ DCL_TYPE (val->type) = IPOINTER;
+ else if (SPEC_SCLS (expr->left->etype) == S_EEPROM)
+ DCL_TYPE (val->type) = EEPPOINTER;
+ else
+ DCL_TYPE (val->type) = POINTER;
+
+ val->type->next = expr->left->ftype;
+ val->etype = getSpec (val->type);
+ return val;
+ }
+
+ /* if address of indexed array */
+ if (IS_AST_OP (expr->left) && expr->left->opval.op == '[')
+ return valForArray (expr->left);
+
+ /* if address of structure element then
+ case 1. a.b ; */
+ if (IS_AST_OP (expr->left) &&
+ expr->left->opval.op == '.') {
+ return valForStructElem (expr->left->left,
+ expr->left->right);
+ }
+
+ /* case 2. (&a)->b ;
+ (&some_struct)->element */
+ if (IS_AST_OP (expr->left) &&
+ expr->left->opval.op == PTR_OP &&
+ IS_ADDRESS_OF_OP (expr->left->left)) {
+ return valForStructElem (expr->left->left->left,
+ expr->left->right);
+ }
+ }
+ /* case 3. (((char *) &a) +/- constant) */
+ if (IS_AST_OP (expr) &&
+ (expr->opval.op == '+' || expr->opval.op == '-') &&
+ IS_AST_OP (expr->left) && expr->left->opval.op == CAST &&
+ IS_AST_OP (expr->left->right) &&
+ expr->left->right->opval.op == '&' &&
+ IS_AST_LIT_VALUE (expr->right)) {
+
+ return valForCastAggr (expr->left->right->left,
+ expr->left->left->opval.lnk,
+ expr->right, expr->opval.op);
+
+ }
+ /* case 4. (char *)(array type) */
+ if (IS_CAST_OP(expr) && IS_AST_SYM_VALUE (expr->right) &&
+ IS_ARRAY(expr->right->ftype)) {
+
+ val = copyValue (AST_VALUE (expr->right));
+ val->type = newLink (DECLARATOR);
+ if (SPEC_SCLS (expr->right->etype) == S_CODE) {
+ DCL_TYPE (val->type) = CPOINTER;
+ DCL_PTR_CONST (val->type) = port->mem.code_ro;
+ }
+ else if (SPEC_SCLS (expr->right->etype) == S_XDATA)
+ DCL_TYPE (val->type) = FPOINTER;
+ else if (SPEC_SCLS (expr->right->etype) == S_XSTACK)
+ DCL_TYPE (val->type) = PPOINTER;
+ else if (SPEC_SCLS (expr->right->etype) == S_IDATA)
+ DCL_TYPE (val->type) = IPOINTER;
+ else if (SPEC_SCLS (expr->right->etype) == S_EEPROM)
+ DCL_TYPE (val->type) = EEPPOINTER;
+ else
+ DCL_TYPE (val->type) = POINTER;
+ val->type->next = expr->right->ftype->next;
+ val->etype = getSpec (val->type);
+ return val;
+ }
+
+ wrong:
+ if (expr)
+ werrorfl (expr->filename, expr->lineno, E_INCOMPAT_PTYPES);
+ else
+ werror (E_INCOMPAT_PTYPES);
+ return NULL;
+
+}
+
+
+/*-----------------------------------------------------------------*/
+/* printPointerType - generates ival for pointer type */
+/*-----------------------------------------------------------------*/
+void _pic16_printPointerType (const char *name, char ptype, void *p)
+{
+ char buf[256];
+
+ sprintf(buf, "LOW(%s)", name);
+ pic16_emitDS(buf, ptype, p);
+ sprintf(buf, "HIGH(%s)", name);
+ pic16_emitDS(buf, ptype, p);
+}
+
+/*-----------------------------------------------------------------*/
+/* printPointerType - generates ival for pointer type */
+/*-----------------------------------------------------------------*/
+void pic16_printPointerType (const char *name, char ptype, void *p)
+{
+ _pic16_printPointerType (name, ptype, p);
+ pic16_flushDB(ptype, p);
+}
+
+/*-----------------------------------------------------------------*/
+/* printGPointerType - generates ival for generic pointer type */
+/*-----------------------------------------------------------------*/
+void pic16_printGPointerType (const char *iname, const char *oname, const unsigned int itype,
+ const unsigned int type, char ptype, void *p)
+{
+ _pic16_printPointerType (iname, ptype, p);
+
+ if(itype == FPOINTER || itype == CPOINTER) { // || itype == GPOINTER) {
+ char buf[256];
+
+ sprintf(buf, "UPPER(%s)", iname);
+ pic16_emitDS(buf, ptype, p);
+ }
+
+ pic16_flushDB(ptype, p);
+}
+
+
+/* set to 0 to disable debug messages */
+#define DEBUG_PRINTIVAL 0
+
+/*-----------------------------------------------------------------*/
+/* pic16_printIvalType - generates ival for int/char */