[Tinyos-commits] CVS: tinyos-1.x/tos/lib/VM/languages/motlle/mate MOPcall.nc, 1.2, 1.3 MOPcallM.nc, 1.3, 1.4 MOPhandlerM.nc, 1.2, 1.3 MemoryM.nc, 1.4, 1.5 MotlleStack.nc, 1.4, 1.5

David Gay idgay at users.sourceforge.net
Thu Oct 27 20:21:23 PDT 2005


Update of /cvsroot/tinyos/tinyos-1.x/tos/lib/VM/languages/motlle/mate
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20440

Modified Files:
	MOPcall.nc MOPcallM.nc MOPhandlerM.nc MemoryM.nc 
	MotlleStack.nc 
Log Message:
tail recursion elimination


Index: MOPcall.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/tos/lib/VM/languages/motlle/mate/MOPcall.nc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** MOPcall.nc	30 Nov 2004 18:52:42 -0000	1.2
--- MOPcall.nc	28 Oct 2005 03:21:21 -0000	1.3
***************
*** 14,18 ****
  }
  implementation {
!   components MOPcallM, MProxy, MotllePrimitives;
  
    Exec = MOPcallM.Exec;
--- 14,18 ----
  }
  implementation {
!   components MOPcallM, MProxy, Memory, MotllePrimitives;
  
    Exec = MOPcallM.Exec;
***************
*** 35,38 ****
--- 35,39 ----
    MOPcallM.G -> MProxy;
    MOPcallM.C -> MProxy;
+   MOPcallM.HandlerStore -> Memory;
  
    MOPcallM.Primitives -> MotllePrimitives;

Index: MOPcallM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/tos/lib/VM/languages/motlle/mate/MOPcallM.nc,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** MOPcallM.nc	23 Oct 2005 21:09:24 -0000	1.3
--- MOPcallM.nc	28 Oct 2005 03:21:21 -0000	1.4
***************
*** 21,24 ****
--- 21,25 ----
      interface MateError as E;
      interface MateBytecode as Primitives[uint16_t id];
+     interface MateHandlerStore as HandlerStore[uint8_t id];
    }
  }
***************
*** 31,34 ****
--- 32,58 ----
    };
  
+   /* Pop current stack frame while preserving stack_move_count stack
+      entries, and set pc correctly.
+      Used for regular returns and early tail-call returns
+   */
+   void do_return(MateContext *context, uint8_t stack_move_count) {
+     struct interpret_frame *frame = call S.current_frame(context);
+ 
+     if (call S.pop_frame(context, sizeof(struct interpret_frame) +
+ 			 frame->nb_locals * sizeof(svalue), stack_move_count))
+       context->pc = 1; // Terminate handler (see getOpcode in MemoryM)
+     else // not last frame
+       context->pc = frame->retpc;
+   }
+ 
+   bool is_tail_call(MateContext *context) {
+     return call HandlerStore.getOpcode[context->currentHandler](context->pc) == OP_MRETURN;
+   }
+ 
+   void tailcall_earlyreturn(MateContext *context, uint8_t nargs) {
+     if (is_tail_call(context))
+       do_return(context, nargs);
+   }
+ 
    MINLINE svalue *getvar(svalue *loc) {
      return call V.data(call V.pointer(call V.read(loc)));
***************
*** 149,154 ****
      msize frame_size;
      uint16_t i;
!     uint16_t oldpc = context->pc;
  
      /* Set context pc, to an offset from the start of our memory block.
         ASSUME: code is not garbage collected. */
--- 173,181 ----
      msize frame_size;
      uint16_t i;
!     uint16_t oldpc;
  
+     tailcall_earlyreturn(context, call_args);
+ 
+     oldpc = context->pc;
      /* Set context pc, to an offset from the start of our memory block.
         ASSUME: code is not garbage collected. */
***************
*** 216,219 ****
--- 243,248 ----
      int8_t nargs;
  
+     tailcall_earlyreturn(context, call_args);
+ 
      lastClosure = call T.closure(closure);
      op = call T.primitive(fn);
***************
*** 290,308 ****
  
    command result_t Return.execute(uint8_t instr, MateContext *context) {
!     struct interpret_frame *frame = call S.current_frame(context);
!     mvalue v = call S.pop(context, 1);
! 
!     if (call S.pop_frame(context, sizeof(struct interpret_frame) +
! 			 frame->nb_locals * sizeof(svalue)))
!       {
! 	// Terminate handler (see MemoryM.HandlerStore.getOpcode)
! 	context->pc = 1;
!       }
!     else // not last frame
!       {
! 	context->pc = frame->retpc;
! 	call S.qpush(context, v);
!       }
! 
      return SUCCESS;
    }
--- 319,323 ----
  
    command result_t Return.execute(uint8_t instr, MateContext *context) {
!     do_return(context, 1);
      return SUCCESS;
    }
***************
*** 311,313 ****
--- 326,332 ----
      return 1;
    }
+ 
+   event void HandlerStore.handlerChanged[uint8_t id]() {
+     // Do nothing
+   }
  }

Index: MOPhandlerM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/tos/lib/VM/languages/motlle/mate/MOPhandlerM.nc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** MOPhandlerM.nc	30 Nov 2004 18:52:42 -0000	1.2
--- MOPhandlerM.nc	28 Oct 2005 03:21:21 -0000	1.3
***************
*** 29,32 ****
--- 29,33 ----
  	context->currentHandler = MATE_HANDLER_NUM;
  
+ 	call S.reset(context);
  	if (call S.push(context, hval))
  	  call Exec.execute(OP_MEXEC4 + 0, context);

Index: MemoryM.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/tos/lib/VM/languages/motlle/mate/MemoryM.nc,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** MemoryM.nc	25 Oct 2005 16:54:08 -0000	1.4
--- MemoryM.nc	28 Oct 2005 03:21:21 -0000	1.5
***************
*** 112,117 ****
        call G.write(i, call T.nil());
      posgc = gc_heap = gvars + sizeof(svalue) * nglobals;
-     sp = fp = mcapsule.data + sizeof mcapsule.data;
-     reset_splimit();
      for (i = 0; i < MATE_HANDLER_NUM; i++)
        signal HandlerStore.handlerChanged[i]();
--- 112,115 ----
***************
*** 216,224 ****
    }
  
!   MINLINE command bool S.pop_frame(MateContext *context, msize size) {
      upframe(&fp, &sp, size);
      return fp == (uint8_t *)(mcapsule.data + sizeof mcapsule.data);
    }
  
    MINLINE command void *S.fp(MateContext *context) {
      return fp;
--- 214,235 ----
    }
  
!   MINLINE command bool S.pop_frame(MateContext *context, msize size,
! 				   uint8_t stack_move_count) {
!     uint8_t *old_sp = sp;
! 
      upframe(&fp, &sp, size);
+ 
+     /* Move the requested number of stack values */
+     sp -= sizeof(svalue) * stack_move_count;
+     memmove(sp, old_sp, sizeof(svalue) * stack_move_count);
+ 
      return fp == (uint8_t *)(mcapsule.data + sizeof mcapsule.data);
    }
  
+   command void S.reset(MateContext *context) {
+     sp = fp = mcapsule.data + sizeof mcapsule.data;
+     reset_splimit();
+   }
+ 
    MINLINE command void *S.fp(MateContext *context) {
      return fp;

Index: MotlleStack.nc
===================================================================
RCS file: /cvsroot/tinyos/tinyos-1.x/tos/lib/VM/languages/motlle/mate/MotlleStack.nc,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** MotlleStack.nc	25 Oct 2005 16:54:08 -0000	1.4
--- MotlleStack.nc	28 Oct 2005 03:21:21 -0000	1.5
***************
*** 5,11 ****
    // Frame allocation
    command void *alloc_frame(MateContext *context, framekind kind, msize size);
!   command bool pop_frame(MateContext *context, msize size);
    command void *current_frame(MateContext *context);
  
    command void *fp(MateContext *context);
    command void *sp(MateContext *context);
--- 5,14 ----
    // Frame allocation
    command void *alloc_frame(MateContext *context, framekind kind, msize size);
!   command bool pop_frame(MateContext *context, msize size,
! 			 uint8_t stack_move_count);
    command void *current_frame(MateContext *context);
  
+   command void reset(MateContext *context);
+ 
    command void *fp(MateContext *context);
    command void *sp(MateContext *context);



More information about the Tinyos-commits mailing list