]> git.sur5r.net Git - cc65/blobdiff - src/cc65/stdfunc.c
Rewrote type conversions
[cc65] / src / cc65 / stdfunc.c
index 219ad470676da2404e15c010d87da75a8f04371c..40d74f692695bb5c49d81411287e0681a35bc27a 100644 (file)
@@ -48,6 +48,7 @@
 #include "litpool.h"
 #include "scanner.h"
 #include "stdfunc.h"
+#include "typeconv.h"
 
 
 
@@ -106,35 +107,36 @@ static struct StdFuncDesc* FindFunc (const char* Name)
 
 
 
-static unsigned ParseArg (type* Type, ExprDesc* pval)
+static unsigned ParseArg (type* Type, ExprDesc* Arg)
 /* Parse one argument but do not push it onto the stack. Return the code
  * generator flags needed to do the actual push.
  */
 {
-    unsigned CFlags;
-    unsigned Flags;
+    /* We have a prototype, so chars may be pushed as chars */
+    unsigned Flags = CF_FORCECHAR;
 
-    /* Do some optimization: If we have a constant value to push,
-     * use a special function that may optimize.
-     */
-    CFlags = CF_NONE;
-    if (CheckedSizeOf (Type) == 1) {
-        CFlags = CF_FORCECHAR;
-    }
-    Flags = CF_NONE;
-    if (evalexpr (CFlags, hie1, pval) == 0) {
-        /* A constant value */
-        Flags |= CF_CONST;
-    }
+    /* Read the expression we're going to pass to the function */
+    int k = hie1 (InitExprDesc (Arg));
 
-    /* Promote the argument if needed */
-    assignadjust (Type, pval);
+    /* Convert this expression to the expected type */
+    k = TypeConversion (Arg, k, Type);
 
-    /* We have a prototype, so chars may be pushed as chars */
-    Flags |= CF_FORCECHAR;
+    /* If the value is not a constant, load it into the primary */
+    if (k != 0 || Arg->Flags != E_MCONST) {
+
+        /* Load into the primary */
+        exprhs (CF_NONE, k, Arg);
+        k = 0;
+
+    } else {
+
+        /* Remember that we have a constant value */
+        Flags |= CF_CONST;
+
+    }
 
     /* Use the type of the argument for the push */
-    return (Flags | TypeOf (pval->Type));
+    return (Flags | TypeOf (Arg->Type));
 }
 
 
@@ -208,20 +210,17 @@ static void StdFunc_strlen (FuncDesc* F attribute ((unused)),
                             ExprDesc* lval attribute ((unused)))
 /* Handle the strlen function */
 {
-    static type ParamType[] = { T_PTR, T_SCHAR, T_END };
-
-    ExprDesc Param;
-    unsigned CodeFlags;
+    static type   ParamType[] = { T_PTR, T_SCHAR, T_END };
+    int           k;
+    ExprDesc      Param;
+    unsigned      CodeFlags;
     unsigned long ParamName;
 
-    /* Fetch the parameter */
-    int k = hie1 (InitExprDesc (&Param));
-
     /* Setup the argument type string */
     ParamType[1] = GetDefaultChar () | T_QUAL_CONST;
 
-    /* Convert the parameter type to the type needed, check for mismatches */
-    assignadjust (ParamType, &Param);
+    /* Fetch the parameter and convert it to the type needed */
+    k = TypeConversion (&Param, hie1 (InitExprDesc (&Param)), ParamType);
 
     /* Check if the parameter is a constant array of some type, or a numeric
      * address cast to a pointer.