Commits

Anonymous committed f0de85a

#179 interim move; check-in on branch

  • Participants
  • Parent commits b25102c
  • Branches issue_0179_dwhall_unify_plat_dirs

Comments (0)

Files changed (4)

File src/vm/string.c

-/*
- * PyMite - A flyweight Python interpreter for 8-bit and larger microcontrollers.
- * Copyright 2002 Dean Hall.  All rights reserved.
- * PyMite is offered through one of two licenses: commercial or open-source.
- * See the LICENSE file at the root of this package for licensing details.
- */
-
-
-#undef __FILE_ID__
-#define __FILE_ID__ 0x12
-
-
-/**
- * String Object Type
- *
- * String object type opeartions.
- */
-
-#include "pm.h"
-
-
-#if USE_STRING_CACHE
-/** String obj cachche: a list of all string objects. */
-static pPmString_t pstrcache = C_NULL;
-#endif /* USE_STRING_CACHE */
-
-
-/*
- * If USE_STRING_CACHE is defined nonzero, the string cache
- * will be searched for an existing String object.
- * If not found, a new object is created and inserted
- * into the cache.
- */
-PmReturn_t
-string_create(PmMemSpace_t memspace, uint8_t const **paddr, uint8_t isimg,
-              int16_t n, pPmObj_t *r_pstring)
-{
-    PmReturn_t retval = PM_RET_OK;
-    uint16_t len = 0;
-    pPmString_t pstr = C_NULL;
-    uint8_t *pdst = C_NULL;
-    uint8_t const *psrc = C_NULL;
-
-#if USE_STRING_CACHE
-    pPmString_t pcacheentry = C_NULL;
-#endif /* USE_STRING_CACHE */
-    uint8_t *pchunk;
-
-    /* If not loading from image */
-    if (isimg == (uint8_t)0)
-    {
-        /* Get length of string */
-        len = mem_getStringLength(memspace, *paddr);
-    }
-
-    /* If loading from an img */
-    else
-    {
-        /* Get length of string */
-        len = mem_getWord(memspace, paddr);
-    }
-
-    /* Get space for String obj */
-    retval = heap_getChunk(sizeof(PmString_t) + len * n, &pchunk);
-    PM_RETURN_IF_ERROR(retval);
-    pstr = (pPmString_t)pchunk;
-
-    /* Fill the string obj */
-    OBJ_SET_TYPE(pstr, OBJ_TYPE_STR);
-    pstr->length = len * n;
-
-    /* Copy C-string into String obj */
-    pdst = (uint8_t *)&(pstr->val);
-    while (--n >= 0)
-    {
-        psrc = *paddr;
-        mem_copy(memspace, &pdst, &psrc, len);
-    }
-
-    /* Be sure paddr points to one byte past the end of the source string */
-    *paddr = psrc;
-
-    /* Zero-pad end of string */
-    for (; pdst < (uint8_t *)pstr + OBJ_GET_SIZE(pstr); pdst++)
-    {
-        *pdst = 0;
-    }
-
-#if USE_STRING_CACHE
-    /* Check for twin string in cache */
-    for (pcacheentry = pstrcache;
-         pcacheentry != C_NULL; pcacheentry = pcacheentry->next)
-    {
-        /* If string already exists */
-        if (string_compare(pcacheentry, pstr) == C_SAME)
-        {
-            /* Free the string */
-            retval = heap_freeChunk((pPmObj_t)pstr);
-
-            /* Return ptr to old */
-            *r_pstring = (pPmObj_t)pcacheentry;
-            return retval;
-        }
-    }
-
-    /* Insert string obj into cache */
-    pstr->next = pstrcache;
-    pstrcache = pstr;
-
-#endif /* USE_STRING_CACHE */
-
-    *r_pstring = (pPmObj_t)pstr;
-    return PM_RET_OK;
-}
-
-
-PmReturn_t
-string_newFromChar(uint8_t const c, pPmObj_t *r_pstring)
-{
-    PmReturn_t retval;
-    uint8_t cstr[2];
-    uint8_t const *pcstr;
-
-    cstr[0] = c;
-    cstr[1] = '\0';
-    pcstr = cstr;
-
-    retval = string_new(&pcstr, r_pstring);
-
-    /* If c was a null character, force the length to 1 */
-    if (c == '\0')
-    {
-        ((pPmString_t)*r_pstring)->length = 1;
-    }
-
-    return retval;
-}
-
-
-int8_t
-string_compare(pPmString_t pstr1, pPmString_t pstr2)
-{
-    /* Return false if lengths are not equal */
-    if (pstr1->length != pstr2->length)
-    {
-        return C_DIFFER;
-    }
-
-    /* Compare the strings' contents */
-    return sli_strncmp((char const *)&(pstr1->val),
-                       (char const *)&(pstr2->val),
-                       pstr1->length) == 0 ? C_SAME : C_DIFFER;
-}
-
-
-#ifdef HAVE_PRINT
-PmReturn_t
-string_print(pPmObj_t pstr, uint8_t marshall)
-{
-    uint16_t i;
-    uint8_t ch;
-    uint8_t nibble;
-    PmReturn_t retval = PM_RET_OK;
-
-    C_ASSERT(pstr != C_NULL);
-
-    /* Ensure string obj */
-    if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR)
-    {
-        PM_RAISE(retval, PM_RET_EX_TYPE);
-        return retval;
-    }
-
-    if (marshall)
-    {
-        retval = plat_putByte('\'');
-        PM_RETURN_IF_ERROR(retval);
-    }
-
-    for (i = 0; i < (((pPmString_t)pstr)->length); i++)
-    {
-        ch = ((pPmString_t)pstr)->val[i];
-        if (ch == '\\')
-        {
-            /* Output an additional backslash to escape it. */
-            retval = plat_putByte('\\');
-            PM_RETURN_IF_ERROR(retval);
-        }
-
-        /* If the marshalled char is not printable, print its hex escape code */
-        if (marshall && (ch < (uint8_t)32 || ch >= (uint8_t)128))
-        {
-            plat_putByte('\\');
-            plat_putByte('x');
-
-            nibble = (ch >> (uint8_t)4) + '0';
-            if (nibble > '9')
-                nibble += ('a' - '0' - (uint8_t)10);
-            plat_putByte(nibble);
-
-            nibble = (ch & (uint8_t)0x0F) + '0';
-            if (nibble > '9')
-                nibble += ('a' - '0' - (uint8_t)10);
-            plat_putByte(nibble);
-        }
-        else
-        {
-            /* Simply output character */
-            retval = plat_putByte(ch);
-            PM_RETURN_IF_ERROR(retval);
-        }
-    }
-    if (marshall)
-    {
-        retval = plat_putByte('\'');
-    }
-
-    return retval;
-}
-#endif /* HAVE_PRINT */
-
-
-PmReturn_t
-string_cacheInit(void)
-{
-#if USE_STRING_CACHE
-    pstrcache = C_NULL;
-#endif
-    return PM_RET_OK;
-}
-
-
-PmReturn_t
-string_getCache(pPmString_t **r_ppstrcache)
-{
-#if USE_STRING_CACHE
-    *r_ppstrcache = &pstrcache;
-#else
-    *r_ppstrcache = C_NULL;
-#endif
-    return PM_RET_OK;
-}

File src/vm/string.h

-/*
- * PyMite - A flyweight Python interpreter for 8-bit and larger microcontrollers.
- * Copyright 2002 Dean Hall.  All rights reserved.
- * PyMite is offered through one of two licenses: commercial or open-source.
- * See the LICENSE file at the root of this package for licensing details.
- */
-
-
-#ifndef __STRING_H__
-#define __STRING_H__
-
-
-/**
- * String Object Type
- *
- * String object type header.
- */
-
-
-/** Set to nonzero to enable string cache.  DO NOT REMOVE THE DEFINITION. */
-#define USE_STRING_CACHE 1
-
-
-/**
- * Load string from image
- *
- * @param ms memoryspace paddr points to
- * @param paddr address in memoryspace of source string
- */
-#define string_loadFromImg(ms, paddr, r_pstring) \
-    string_create((ms), (paddr), (uint8_t)1, (int16_t)1, (r_pstring))
-
-/**
- * Creates String object from character array in RAM
- *
- * @param paddr pointer to address of source string
- * @param r_pstring Return arg; addr of ptr to string
- */
-#define string_new(paddr, r_pstring) \
-    string_create(MEMSPACE_RAM, (paddr), (uint8_t)0, (int16_t)1, (r_pstring))
-
-/**
- * Creates String object by replicating an existing C string, n times
- *
- * @param paddr pointer to address of source string
- * @param n number of times to replicate the source string
- * @param r_pstring Return arg; addr of ptr to string
- */
-#define string_replicate(paddr, n, r_pstring) \
-    string_create(MEMSPACE_RAM, (paddr), (uint8_t)0, (n), (r_pstring))
-
-/***************************************************************
- * Types
- **************************************************************/
-
-/**
- * String obj
- *
- * Null terminated array of chars.
- */
-typedef struct PmString_s
-{
-    /** Object descriptor */
-    PmObjDesc_t od;
-
-    /** Length of string */
-    uint16_t length;
-
-#if USE_STRING_CACHE
-    /** Ptr to next string in cache */
-    struct PmString_s *next;
-#endif                          /* USE_STRING_CACHE */
-
-    /**
-     * Null-term char array
-     *
-     * Use length 1 here so that string-alloc function can use
-     * "sizeof(PmString_t) + len" and there will be room for the null-term
-     */
-    uint8_t val[1];
-} PmString_t,
- *pPmString_t;
-
-
-/***************************************************************
- * Prototypes
- **************************************************************/
-
-/**
- * Creates a new String obj.
- * If isimg is zero, load from a String image.
- *      A string image has the following structure:
- *          -type:      int8 - OBJ_TYPE_STRING
- *          -length:    uint16 - number of bytes in the string
- *          -val:       uint8[] - array of chars with null term
- *
- * If isimg is not zero, create from a C string.
- * Returns by reference a ptr to String obj.
- *
- * Obtain space for String from the heap.
- * Copy string from memspace.
- * Leave contents of paddr pointing one byte past end of str.
- *
- * THE PROGRAMMER SHOULD NOT CALL THIS FUNCTION DIRECTLY.
- * Instead, use one of the two macros string_loadFromImg()
- * or string_new().
- *
- * @param   memspace memory space where *paddr points
- * @param   paddr ptr to ptr to null term character array or image.
- * @param   isimg if 0, create from C string;
- *          else load from image.
- * @param   Return arg; ptr to String obj
- * @return  Return status
- */
-PmReturn_t string_create(PmMemSpace_t memspace, uint8_t const **paddr,
-                         uint8_t isimg, int16_t n, pPmObj_t *r_pstring);
-;
-
-/**
- * Creates a new String object from a single character.
- *
- * @param   c the character to become the string
- * @param   r_psting Return arg; ptr to String obj
- * @return  Return status
- */
-PmReturn_t string_newFromChar(uint8_t const c, pPmObj_t *r_pstring);
-
-/**
- * Compares two String objects for equality.
- *
- * @param   pstr1 Ptr to first string
- * @param   pstr2 Ptr to second string
- * @return  C_SAME if the strings are equivalent, C_DIFFER otherwise
- */
-int8_t string_compare(pPmString_t, pPmString_t);
-
-#ifdef HAVE_PRINT
-/**
- * Sends out a string object bytewise. Escaping and framing is configurable
- * via marshall.
- *
- * @param pobj Ptr to string object
- * @param marshall If 0, print out string as is. Otherwise escape unprintable
- *                 characters and surround string with single quotes.
- * @return Return status
- */
-PmReturn_t string_print(pPmObj_t pstr, uint8_t marshall);
-#endif /* HAVE_PRINT */
-
-/**
- * Clears the string cache if one exists.
- * Called by heap_init()
- *
- * @return Return status
- */
-PmReturn_t string_cacheInit(void);
-
-
-/** Returns a pointer to the base of the string cache */
-PmReturn_t string_getCache(pPmString_t **r_ppstrcache);
-
-#endif /* __STRING_H__ */

File src/vm/strobj.c

+/*
+ * PyMite - A flyweight Python interpreter for 8-bit and larger microcontrollers.
+ * Copyright 2002 Dean Hall.  All rights reserved.
+ * PyMite is offered through one of two licenses: commercial or open-source.
+ * See the LICENSE file at the root of this package for licensing details.
+ */
+
+
+#undef __FILE_ID__
+#define __FILE_ID__ 0x12
+
+
+/**
+ * String Object Type
+ *
+ * String object type opeartions.
+ */
+
+#include "pm.h"
+
+
+#if USE_STRING_CACHE
+/** String obj cachche: a list of all string objects. */
+static pPmString_t pstrcache = C_NULL;
+#endif /* USE_STRING_CACHE */
+
+
+/*
+ * If USE_STRING_CACHE is defined nonzero, the string cache
+ * will be searched for an existing String object.
+ * If not found, a new object is created and inserted
+ * into the cache.
+ */
+PmReturn_t
+string_create(PmMemSpace_t memspace, uint8_t const **paddr, uint8_t isimg,
+              int16_t n, pPmObj_t *r_pstring)
+{
+    PmReturn_t retval = PM_RET_OK;
+    uint16_t len = 0;
+    pPmString_t pstr = C_NULL;
+    uint8_t *pdst = C_NULL;
+    uint8_t const *psrc = C_NULL;
+
+#if USE_STRING_CACHE
+    pPmString_t pcacheentry = C_NULL;
+#endif /* USE_STRING_CACHE */
+    uint8_t *pchunk;
+
+    /* If not loading from image */
+    if (isimg == (uint8_t)0)
+    {
+        /* Get length of string */
+        len = mem_getStringLength(memspace, *paddr);
+    }
+
+    /* If loading from an img */
+    else
+    {
+        /* Get length of string */
+        len = mem_getWord(memspace, paddr);
+    }
+
+    /* Get space for String obj */
+    retval = heap_getChunk(sizeof(PmString_t) + len * n, &pchunk);
+    PM_RETURN_IF_ERROR(retval);
+    pstr = (pPmString_t)pchunk;
+
+    /* Fill the string obj */
+    OBJ_SET_TYPE(pstr, OBJ_TYPE_STR);
+    pstr->length = len * n;
+
+    /* Copy C-string into String obj */
+    pdst = (uint8_t *)&(pstr->val);
+    while (--n >= 0)
+    {
+        psrc = *paddr;
+        mem_copy(memspace, &pdst, &psrc, len);
+    }
+
+    /* Be sure paddr points to one byte past the end of the source string */
+    *paddr = psrc;
+
+    /* Zero-pad end of string */
+    for (; pdst < (uint8_t *)pstr + OBJ_GET_SIZE(pstr); pdst++)
+    {
+        *pdst = 0;
+    }
+
+#if USE_STRING_CACHE
+    /* Check for twin string in cache */
+    for (pcacheentry = pstrcache;
+         pcacheentry != C_NULL; pcacheentry = pcacheentry->next)
+    {
+        /* If string already exists */
+        if (string_compare(pcacheentry, pstr) == C_SAME)
+        {
+            /* Free the string */
+            retval = heap_freeChunk((pPmObj_t)pstr);
+
+            /* Return ptr to old */
+            *r_pstring = (pPmObj_t)pcacheentry;
+            return retval;
+        }
+    }
+
+    /* Insert string obj into cache */
+    pstr->next = pstrcache;
+    pstrcache = pstr;
+
+#endif /* USE_STRING_CACHE */
+
+    *r_pstring = (pPmObj_t)pstr;
+    return PM_RET_OK;
+}
+
+
+PmReturn_t
+string_newFromChar(uint8_t const c, pPmObj_t *r_pstring)
+{
+    PmReturn_t retval;
+    uint8_t cstr[2];
+    uint8_t const *pcstr;
+
+    cstr[0] = c;
+    cstr[1] = '\0';
+    pcstr = cstr;
+
+    retval = string_new(&pcstr, r_pstring);
+
+    /* If c was a null character, force the length to 1 */
+    if (c == '\0')
+    {
+        ((pPmString_t)*r_pstring)->length = 1;
+    }
+
+    return retval;
+}
+
+
+int8_t
+string_compare(pPmString_t pstr1, pPmString_t pstr2)
+{
+    /* Return false if lengths are not equal */
+    if (pstr1->length != pstr2->length)
+    {
+        return C_DIFFER;
+    }
+
+    /* Compare the strings' contents */
+    return sli_strncmp((char const *)&(pstr1->val),
+                       (char const *)&(pstr2->val),
+                       pstr1->length) == 0 ? C_SAME : C_DIFFER;
+}
+
+
+#ifdef HAVE_PRINT
+PmReturn_t
+string_print(pPmObj_t pstr, uint8_t marshall)
+{
+    uint16_t i;
+    uint8_t ch;
+    uint8_t nibble;
+    PmReturn_t retval = PM_RET_OK;
+
+    C_ASSERT(pstr != C_NULL);
+
+    /* Ensure string obj */
+    if (OBJ_GET_TYPE(pstr) != OBJ_TYPE_STR)
+    {
+        PM_RAISE(retval, PM_RET_EX_TYPE);
+        return retval;
+    }
+
+    if (marshall)
+    {
+        retval = plat_putByte('\'');
+        PM_RETURN_IF_ERROR(retval);
+    }
+
+    for (i = 0; i < (((pPmString_t)pstr)->length); i++)
+    {
+        ch = ((pPmString_t)pstr)->val[i];
+        if (ch == '\\')
+        {
+            /* Output an additional backslash to escape it. */
+            retval = plat_putByte('\\');
+            PM_RETURN_IF_ERROR(retval);
+        }
+
+        /* If the marshalled char is not printable, print its hex escape code */
+        if (marshall && (ch < (uint8_t)32 || ch >= (uint8_t)128))
+        {
+            plat_putByte('\\');
+            plat_putByte('x');
+
+            nibble = (ch >> (uint8_t)4) + '0';
+            if (nibble > '9')
+                nibble += ('a' - '0' - (uint8_t)10);
+            plat_putByte(nibble);
+
+            nibble = (ch & (uint8_t)0x0F) + '0';
+            if (nibble > '9')
+                nibble += ('a' - '0' - (uint8_t)10);
+            plat_putByte(nibble);
+        }
+        else
+        {
+            /* Simply output character */
+            retval = plat_putByte(ch);
+            PM_RETURN_IF_ERROR(retval);
+        }
+    }
+    if (marshall)
+    {
+        retval = plat_putByte('\'');
+    }
+
+    return retval;
+}
+#endif /* HAVE_PRINT */
+
+
+PmReturn_t
+string_cacheInit(void)
+{
+#if USE_STRING_CACHE
+    pstrcache = C_NULL;
+#endif
+    return PM_RET_OK;
+}
+
+
+PmReturn_t
+string_getCache(pPmString_t **r_ppstrcache)
+{
+#if USE_STRING_CACHE
+    *r_ppstrcache = &pstrcache;
+#else
+    *r_ppstrcache = C_NULL;
+#endif
+    return PM_RET_OK;
+}

File src/vm/strobj.h

+/*
+ * PyMite - A flyweight Python interpreter for 8-bit and larger microcontrollers.
+ * Copyright 2002 Dean Hall.  All rights reserved.
+ * PyMite is offered through one of two licenses: commercial or open-source.
+ * See the LICENSE file at the root of this package for licensing details.
+ */
+
+
+#ifndef __STRING_H__
+#define __STRING_H__
+
+
+/**
+ * String Object Type
+ *
+ * String object type header.
+ */
+
+
+/** Set to nonzero to enable string cache.  DO NOT REMOVE THE DEFINITION. */
+#define USE_STRING_CACHE 1
+
+
+/**
+ * Load string from image
+ *
+ * @param ms memoryspace paddr points to
+ * @param paddr address in memoryspace of source string
+ */
+#define string_loadFromImg(ms, paddr, r_pstring) \
+    string_create((ms), (paddr), (uint8_t)1, (int16_t)1, (r_pstring))
+
+/**
+ * Creates String object from character array in RAM
+ *
+ * @param paddr pointer to address of source string
+ * @param r_pstring Return arg; addr of ptr to string
+ */
+#define string_new(paddr, r_pstring) \
+    string_create(MEMSPACE_RAM, (paddr), (uint8_t)0, (int16_t)1, (r_pstring))
+
+/**
+ * Creates String object by replicating an existing C string, n times
+ *
+ * @param paddr pointer to address of source string
+ * @param n number of times to replicate the source string
+ * @param r_pstring Return arg; addr of ptr to string
+ */
+#define string_replicate(paddr, n, r_pstring) \
+    string_create(MEMSPACE_RAM, (paddr), (uint8_t)0, (n), (r_pstring))
+
+/***************************************************************
+ * Types
+ **************************************************************/
+
+/**
+ * String obj
+ *
+ * Null terminated array of chars.
+ */
+typedef struct PmString_s
+{
+    /** Object descriptor */
+    PmObjDesc_t od;
+
+    /** Length of string */
+    uint16_t length;
+
+#if USE_STRING_CACHE
+    /** Ptr to next string in cache */
+    struct PmString_s *next;
+#endif                          /* USE_STRING_CACHE */
+
+    /**
+     * Null-term char array
+     *
+     * Use length 1 here so that string-alloc function can use
+     * "sizeof(PmString_t) + len" and there will be room for the null-term
+     */
+    uint8_t val[1];
+} PmString_t,
+ *pPmString_t;
+
+
+/***************************************************************
+ * Prototypes
+ **************************************************************/
+
+/**
+ * Creates a new String obj.
+ * If isimg is zero, load from a String image.
+ *      A string image has the following structure:
+ *          -type:      int8 - OBJ_TYPE_STRING
+ *          -length:    uint16 - number of bytes in the string
+ *          -val:       uint8[] - array of chars with null term
+ *
+ * If isimg is not zero, create from a C string.
+ * Returns by reference a ptr to String obj.
+ *
+ * Obtain space for String from the heap.
+ * Copy string from memspace.
+ * Leave contents of paddr pointing one byte past end of str.
+ *
+ * THE PROGRAMMER SHOULD NOT CALL THIS FUNCTION DIRECTLY.
+ * Instead, use one of the two macros string_loadFromImg()
+ * or string_new().
+ *
+ * @param   memspace memory space where *paddr points
+ * @param   paddr ptr to ptr to null term character array or image.
+ * @param   isimg if 0, create from C string;
+ *          else load from image.
+ * @param   Return arg; ptr to String obj
+ * @return  Return status
+ */
+PmReturn_t string_create(PmMemSpace_t memspace, uint8_t const **paddr,
+                         uint8_t isimg, int16_t n, pPmObj_t *r_pstring);
+;
+
+/**
+ * Creates a new String object from a single character.
+ *
+ * @param   c the character to become the string
+ * @param   r_psting Return arg; ptr to String obj
+ * @return  Return status
+ */
+PmReturn_t string_newFromChar(uint8_t const c, pPmObj_t *r_pstring);
+
+/**
+ * Compares two String objects for equality.
+ *
+ * @param   pstr1 Ptr to first string
+ * @param   pstr2 Ptr to second string
+ * @return  C_SAME if the strings are equivalent, C_DIFFER otherwise
+ */
+int8_t string_compare(pPmString_t, pPmString_t);
+
+#ifdef HAVE_PRINT
+/**
+ * Sends out a string object bytewise. Escaping and framing is configurable
+ * via marshall.
+ *
+ * @param pobj Ptr to string object
+ * @param marshall If 0, print out string as is. Otherwise escape unprintable
+ *                 characters and surround string with single quotes.
+ * @return Return status
+ */
+PmReturn_t string_print(pPmObj_t pstr, uint8_t marshall);
+#endif /* HAVE_PRINT */
+
+/**
+ * Clears the string cache if one exists.
+ * Called by heap_init()
+ *
+ * @return Return status
+ */
+PmReturn_t string_cacheInit(void);
+
+
+/** Returns a pointer to the base of the string cache */
+PmReturn_t string_getCache(pPmString_t **r_ppstrcache);
+
+#endif /* __STRING_H__ */