manashmandal / python-on-a-chip

Automatically exported from code.google.com/p/python-on-a-chip
Other
0 stars 0 forks source link

SLICE_1 (SLICE+1) implementation in Python #223

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Test a recursive list reversing code in Pymite.
2. When dissembled, it will show a "SLICE+1" bytecode in the dissembly.
3. It seems like this has not been implemented in Pymite. Is there any plans to 
implement this ? 

What version of the product are you using? On what operating system?
0.9 on Ubuntu x86_64 (make ipm)

Original issue reported on code.google.com by yodara...@gmail.com on 9 Feb 2012 at 5:06

GoogleCodeExporter commented 9 years ago
Hi,

I had implemented my own 'SLICE+1' bytecode support in PyMite 0.9 which might 
be helpful to you. You actually need to modify two files in PyMite 0.9. They 
are:
pymite-09/src/vm/interp.c
pymite-09/src/tools/pmImgCreator.py

I tested my implementation with the following simple code in ipm:

ipm> a=[1,2,3]
ipm> a[1:]
[2, 3]

You can patch your interp.c file with this:

567,614d566
<             case SLICE_1:
<                 /* Implements TOS = TOS1[TOS:] */
< 
<                 /* Create a copy if it is a list and remove 
<                    items from [0:TOS-1] from this list */
<                 if ((OBJ_GET_TYPE(TOS) == OBJ_TYPE_INT)
<                     && (OBJ_GET_TYPE(TOS1) == OBJ_TYPE_LST))
<                 {
<                     t16 = (int16_t)((pPmInt_t)TOS)->val;
< 
<                     if ( t16 <=  -((pPmList_t)TOS1)->length )
<                     {
<                         t16 = 0; // No items to be removed
<                     }
<                     else if ( t16 > -((pPmList_t)TOS1)->length && t16 < 0 )
<                     {
<                         t16 = t16 % (int16_t)((pPmList_t)TOS1)->length;
<                     }
<                     else if ( t16 > (int16_t)((pPmList_t)TOS1)->length )
<                     {
<                         // All items will be removed
<                         t16 = (int16_t)((pPmList_t)TOS1)->length;             

<                     }
< 
<                     retval = list_copy(TOS1, &pobj2);
< 
<                     PM_BREAK_IF_ERROR(retval);
< 
<                     for (; t16 > 0; t16--)
<                     {
<                         retval = list_delItem(pobj2, 0);
<                         PM_BREAK_IF_ERROR(retval);
<                     }
< 
<                     TOS = pobj2;
<                 }
< 
<                 /* If TOS is an immutable sequence leave it (no op) */
< 
<                 /* Raise a TypeError for types that can not be sliced */
<                 else if ((OBJ_GET_TYPE(TOS) != OBJ_TYPE_STR)
<                          && (OBJ_GET_TYPE(TOS) != OBJ_TYPE_TUP))
<                 {
<                     PM_RAISE(retval, PM_RET_EX_TYPE);
<                     break;
<                 }
<                 continue;
< 

And patch your pmImgCreator.py with this:

150,151c150
< #    "SLICE+1", "SLICE+2", "SLICE+3",
<     "SLICE+2", "SLICE+3",
---
>     "SLICE+1", "SLICE+2", "SLICE+3",

I hope it helps! Let me know if you have any questions.

Bin Huang

Original comment by bin.art...@gmail.com on 15 Feb 2012 at 7:43

GoogleCodeExporter commented 9 years ago
I'm expanding this issue to implement SLICE_1, 2 and 3 for List, Tuple and 
String data types.

Original comment by dwhall...@gmail.com on 19 Jun 2013 at 2:52

GoogleCodeExporter commented 9 years ago
This issue was closed by revision a52d92e88c3e.

Original comment by dwhall...@gmail.com on 19 Jun 2013 at 2:56

GoogleCodeExporter commented 9 years ago

Original comment by dwhall...@gmail.com on 19 Jun 2013 at 2:58