sizeof.py

Go to the documentation of this file.
00001 # This file is Copyright 2009, 2010 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 # 
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING in this directory.
00012 
00013 ## @file
00014 #  @copybrief sizeof
00015 
00016 ## @package sizeof
00017 #  @brief Provides PyMite's sizeof module.
00018 #
00019 # <b>USAGE</b>
00020 #
00021 # \code sizeof.sizeof(obj) \endcode
00022 #
00023 # Prints the size of the given object.  If obj is an integer from 0..31,
00024 # the size of the object type represented by that integer will be returned.
00025 
00026 """__NATIVE__
00027 #include "pm.h"
00028 """
00029 
00030 
00031 def sizeof(obj):
00032     """__NATIVE__
00033     pPmObj_t pobj;
00034     pPmObj_t psize;
00035     int32_t n;
00036     PmReturn_t retval = PM_RET_OK;
00037     int32_t static size[] = {
00038         sizeof(PmObj_t), /* None type */
00039         sizeof(PmInt_t),
00040         sizeof(PmFloat_t),
00041         sizeof(PmString_t),
00042         sizeof(PmTuple_t),
00043         sizeof(PmCo_t),
00044         sizeof(PmFunc_t), /* Module Obj uses func struct */
00045         sizeof(PmClass_t),
00046         sizeof(PmFunc_t),
00047         sizeof(PmClass_t), /* Class instance */
00048         0, /* CIM */
00049         0, /* NIM */
00050         sizeof(PmCo_t), /* NOB */
00051         sizeof(PmThread_t),
00052         sizeof(PmClass_t), /* Exception instance */
00053         sizeof(PmBoolean_t),
00054         sizeof(PmCodeImgObj_t),
00055         sizeof(PmList_t),
00056         sizeof(PmDict_t),
00057         0,
00058         0,
00059         0,
00060         0,
00061         0,
00062         0,
00063         sizeof(PmFrame_t),
00064         sizeof(PmBlock_t),
00065         sizeof(Segment_t),
00066         sizeof(Seglist_t),
00067         sizeof(PmSeqIter_t),
00068         sizeof(PmNativeFrame_t),
00069     };
00070 
00071     /* If wrong number of args, raise TypeError */
00072     if (NATIVE_GET_NUM_ARGS() != 1)
00073     {
00074         PM_RAISE(retval, PM_RET_EX_TYPE);
00075         return retval;
00076     }
00077 
00078     pobj = NATIVE_GET_LOCAL(0);
00079     if (OBJ_GET_TYPE(pobj) == OBJ_TYPE_INT)
00080     {
00081         n = ((pPmInt_t)pobj)->val;
00082         if ((n >= 0) && (n < 32))
00083         {
00084             /* Return the size of the type represented by the integer */
00085             retval = int_new(size[n], &psize);
00086         }
00087         else
00088         {
00089             /* Return the size of an integer object */
00090             retval = int_new(OBJ_GET_SIZE(pobj), &psize);
00091         }
00092     }
00093     else
00094     {
00095         /* Return the size of the given non-integer object */
00096         retval = int_new(OBJ_GET_SIZE(pobj), &psize);
00097     }
00098 
00099     NATIVE_SET_TOS(psize);
00100     return retval;
00101     """
00102     pass
00103 
00104 
00105 def print_sizes():
00106     types = (
00107         'NON',
00108         'INT',
00109         'FLT',
00110         'STR',
00111         'TUP',
00112         'COB',
00113         'MOD',
00114         'CLO',
00115         'FXN',
00116         'CLI',
00117         'CIM',
00118         'NIM',
00119         'NOB',
00120         'THR',
00121         0,
00122         'BOL',
00123         'CIO',
00124         'LST',
00125         'DIC',
00126         0, 0, 0, 0, 0, 0,
00127         'FRM',
00128         'BLK',
00129         'SEG',
00130         'SGL',
00131         'SQI',
00132         'NFM',
00133         0,
00134     )
00135     for i in range(32):
00136         if types[i] != 0:
00137             print "sizeof(", types[i], ") = ", sizeof(i)
00138     
00139 #:mode=c:

Generated on Mon Oct 18 07:40:46 2010 for Python-on-a-chip by  doxygen 1.5.9