Commits

Anonymous committed 3ef0765

#108: Mainlining directly

  • Participants
  • Parent commits eb5dbc8

Comments (0)

Files changed (4)

File src/lib/__bi.py

     pPmObj_t px;
     pPmObj_t py;
     pPmObj_t pn;
-    int32_t x;
-    int32_t y;
-    int32_t n;
     PmReturn_t retval;
 
     /* If wrong number of args, raise TypeError */
     px = NATIVE_GET_LOCAL(0);
     py = NATIVE_GET_LOCAL(1);
 
-    /* Raise TypeError if args aren't ints */
-    if ((OBJ_GET_TYPE(*px) != OBJ_TYPE_INT)
-        || (OBJ_GET_TYPE(*py) != OBJ_TYPE_INT))
-    {
-        PM_RAISE(retval, PM_RET_EX_TYPE);
-        return retval;
-    }
-
-    x = ((pPmInt_t)px)->val;
-    y = ((pPmInt_t)py)->val;
-
-    /* Raise Value error if exponent is negative */
-    if (y < 0)
-    {
-        PM_RAISE(retval, PM_RET_EX_VAL);
-        return retval;
-    }
-
-    /* Calculate x raised to y */
-    n = 1;
-    while (y > 0)
-    {
-        n = n * x;
-        y--;
-    }
-    retval = int_new(n, &pn);
+    /* Calculate integer power */
+    retval = int_pow(px, py, &pn);
     PM_RETURN_IF_ERROR(retval);
 
     /* Push result on stack */
     NATIVE_SET_TOS(pn);
 
-    return PM_RET_OK;
+    return retval;
     """
     pass
 

File src/vm/int.c

     return _int_printHex(((pPmInt_t)pint)->val);
 }
 #endif /* HAVE_PRINT */
+
+
+PmReturn_t
+int_pow(pPmObj_t px, pPmObj_t py, pPmObj_t *r_pn)
+{
+    int32_t x;
+    int32_t y;
+    int32_t n;
+    PmReturn_t retval;
+
+    /* Raise TypeError if args aren't ints */
+    if ((OBJ_GET_TYPE(*px) != OBJ_TYPE_INT)
+        || (OBJ_GET_TYPE(*py) != OBJ_TYPE_INT))
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    x = ((pPmInt_t)px)->val;
+    y = ((pPmInt_t)py)->val;
+
+    /* Raise Value error if exponent is negative */
+    if (y < 0)
+    {
+        PM_RAISE(retval, PM_RET_EX_VAL);
+        return retval;
+    }
+
+    /* Calculate x raised to y */
+    n = 1;
+    while (y > 0)
+    {
+        n = n * x;
+        y--;
+    }
+    retval = int_new(n, r_pn);
+
+    return retval;
+}

File src/vm/int.h

 {
     /** Object descriptor */
     PmObjDesc_t od;
-    
+
     /** Integer value */
     int32_t val;
 } PmInt_t,
 PmReturn_t int_printHex(pPmObj_t pint);
 #endif /* HAVE_PRINT */
 
+/**
+ * Returns by reference an integer that is x raised to the power of y.
+ *
+ * @param x The integer base
+ * @param y The integer exponent
+ * @param r_pn The return value of x ** y
+ * @return Return status
+ */
+PmReturn_t int_pow(pPmObj_t px, pPmObj_t py, pPmObj_t *r_pn);
+
 #endif /* __INT_H__ */

File src/vm/interp.c

 
 extern PmReturn_t (*std_nat_fxn_table[]) (pPmFrame_t *, signed char);
 extern PmReturn_t (*usr_nat_fxn_table[]) (pPmFrame_t *, signed char);
-PmReturn_t nat___bi_pow(pPmFrame_t *pframe, signed char numargs);
 
 
 /***************************************************************
 
             case BINARY_POWER:
             case INPLACE_POWER:
-                /* Put args in native frame */
-                gVmGlobal.nativeframe.nf_locals[1] = PM_POP();
-                gVmGlobal.nativeframe.nf_locals[0] = TOS;
+                /* Pop args right to left */
+                pobj2 = PM_POP();
+                pobj1 = TOS;
 
-                /* CALL NATIVE FXN */
-                retval = nat___bi_pow(&FP, 2);
-                /* RETURN FROM NATIVE FXN */
+                /* Calculate integer power */
+                retval = int_pow(pobj1, pobj2, &pobj3);
+                PM_BREAK_IF_ERROR(retval);
 
-                /* Put result on stack */
-                TOS = gVmGlobal.nativeframe.nf_stack;
-                PM_BREAK_IF_ERROR(retval);
+                /* Set return value */
+                TOS = pobj3;
                 continue;
 
             case GET_ITER:
                     {
                         ((pPmTuple_t)pobj3)->val[t16] = PM_POP();
                     }
-                    
+
                     /* Set func's default args */
                     ((pPmFunc_t)pobj2)->f_defaultargs = (pPmTuple_t)pobj3;
                 }
-                
+
                 /* Push func obj */
                 PM_PUSH(pobj2);
                 continue;