X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCast.c;h=c92f179b3b1aeb2086d18e374432f25a505db952;hb=6b5e433931b8b1ba61bb6c694f74f42f48c3acf5;hp=63a7896651d62ebcd3bdaab85fb3d8270d8528d8;hpb=00680ef6a2a1b41c9ef9a2af0ce0dade280d293c;p=fw%2Fsdcc diff --git a/src/SDCCast.c b/src/SDCCast.c index 63a78966..c92f179b 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -667,7 +667,20 @@ processParms (ast * func, if (IS_INTEGRAL (ftype) && (getSize (ftype) < (unsigned) INTSIZE)) { - newType = newAst_LINK(INTTYPE); + if (IS_AST_OP(actParm) && + (actParm->opval.op == LEFT_OP || + actParm->opval.op == '*' || + actParm->opval.op == '+' || + actParm->opval.op == '-') && + actParm->right) { + // we should cast an operand instead of the result + actParm->decorated = 0; + actParm->left = newNode( CAST, newAst_LINK(newIntLink()), + actParm->left); + actParm = decorateType(actParm); + } else { + newType = newAst_LINK(INTTYPE); + } } if (IS_PTR(ftype) && !IS_GENPTR(ftype)) @@ -1886,6 +1899,7 @@ decorateType (ast * tree) /*----------------------------*/ /* leaf has been reached */ /*----------------------------*/ + lineno=tree->lineno; /* if this is of type value */ /* just get the type */ if (tree->type == EX_VALUE) @@ -1959,20 +1973,24 @@ decorateType (ast * tree) if (dtr != tree->right) tree->right = dtr; - /* special case for left shift operation : cast up right->left if type of left - has greater size than right */ - if (tree->left && tree->right && tree->right->opval.op == LEFT_OP) { - int lsize = getSize(LTYPE(tree)); - int rsize = getSize(RTYPE(tree)); - - if (lsize > rsize) { - tree->right->decorated = 0; - tree->right->left = newNode( CAST, (lsize == 2 ? - newAst_LINK(newIntLink()) : - newAst_LINK(newLongLink())), - tree->right->left); - tree->right = decorateType(tree->right); - } + if (IS_AST_OP(tree) && + (tree->opval.op == CAST || tree->opval.op == '=') && + (getSize(LTYPE(tree)) > getSize(RTYPE(tree))) && + (getSize(RTYPE(tree)) < INTSIZE)) { + // this is a cast/assign to a bigger type + if (IS_AST_OP(tree->right) && + IS_INTEGRAL(tree->right->ftype) && + (tree->right->opval.op == LEFT_OP || + tree->right->opval.op == '*' || + tree->right->opval.op == '+' || + tree->right->opval.op == '-') && + tree->right->right) { + // we should cast an operand instead of the result + tree->right->decorated = 0; + tree->right->left = newNode( CAST, newAst_LINK(newIntLink()), + tree->right->left); + tree->right = decorateType(tree->right); + } } } @@ -2392,18 +2410,16 @@ decorateType (ast * tree) } LRVAL (tree) = RRVAL (tree) = 1; + TETYPE (tree) = getSpec (TTYPE (tree) = + computeType (LTYPE (tree), + RTYPE (tree))); + /* promote result to int if left & right are char this will facilitate hardware multiplies 8bit x 8bit = 16bit */ if (IS_CHAR(LETYPE(tree)) && IS_CHAR(RETYPE(tree))) { - TETYPE (tree) = getSpec (TTYPE (tree) = - computeType (LTYPE (tree), - RTYPE (tree))); SPEC_NOUN(TETYPE(tree)) = V_INT; - } else { - TETYPE (tree) = getSpec (TTYPE (tree) = - computeType (LTYPE (tree), - RTYPE (tree))); } + return tree; /*------------------------------------------------------------------*/ @@ -2487,7 +2503,7 @@ decorateType (ast * tree) LRVAL (tree) = RRVAL (tree) = 1; /* if the left is a pointer */ - if (IS_PTR (LTYPE (tree)) || IS_ARRAY (LTYPE (tree))) + if (IS_PTR (LTYPE (tree)) || IS_AGGREGATE (LTYPE (tree)) ) TETYPE (tree) = getSpec (TTYPE (tree) = LTYPE (tree)); else @@ -2688,46 +2704,24 @@ decorateType (ast * tree) tree->opval.val->type); return tree; } -#if 0 - /* a left shift must be done with at least 16bits */ - if ((tree->opval.op==LEFT_OP) && (getSize(LTYPE(tree))<2)) { - // insert a cast - if (IS_AST_SYM_VALUE(tree->left) || IS_AST_OP(tree->left)) { - tree->left = - decorateType (newNode (CAST, - newAst_LINK(copyLinkChain(LTYPE(tree))), - tree->left)); - SPEC_NOUN(tree->left->left->ftype)=V_INT; - } else { - // must be a literal, we can do it right away - SPEC_NOUN(tree->left->opval.val->type)=V_INT; - } - } -#endif + /* if only the right side is a literal & we are shifting more than size of the left operand then zero */ if (IS_LITERAL (RTYPE (tree)) && ((unsigned) floatFromVal (valFromType (RETYPE (tree)))) >= (getSize (LTYPE (tree)) * 8)) { - /* if left shift then cast up */ - if (tree->opval.op==LEFT_OP) { - int size = getSize(LTYPE(tree)); - tree->left = - decorateType (newNode (CAST, - (size == 1 ? newAst_LINK(newIntLink()) : - (size == 2 ? newAst_LINK(newLongLink()) : - newAst_LINK(newIntLink()))), - tree->left)); - } else { - werror (W_SHIFT_CHANGED, - (tree->opval.op == LEFT_OP ? "left" : "right")); - tree->type = EX_VALUE; - tree->left = tree->right = NULL; - tree->opval.val = constVal ("0"); - TETYPE (tree) = TTYPE (tree) = tree->opval.val->type; - return tree; - } + if (tree->opval.op==LEFT_OP || + (tree->opval.op==RIGHT_OP && SPEC_USIGN(LETYPE(tree)))) { + lineno=tree->lineno; + werror (W_SHIFT_CHANGED, + (tree->opval.op == LEFT_OP ? "left" : "right")); + tree->type = EX_VALUE; + tree->left = tree->right = NULL; + tree->opval.val = constVal ("0"); + TETYPE (tree) = TTYPE (tree) = tree->opval.val->type; + return tree; + } } LRVAL (tree) = RRVAL (tree) = 1; if (IS_LITERAL (LTYPE (tree)) && !IS_LITERAL (RTYPE (tree)))