/* This file is part of GNU epsilon, a functional language implementation

Copyright (C) 2003 Luca Saiu

GNU epsilon is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2, or (at your
option) any later version.

GNU epsilon is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with epsilon; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */


/* Code for the s_mkba_i instruction */

word_t* array;

/* Allocate array: */
ASSIGN_CREATE_ARRAY(array, (integer_t)PARAMETER_1 + 1);

array[0] = PARAMETER_1; /* Store length in the first element */

/* Store the other elements, if they exist: */
if((integer_t)PARAMETER_1 == 0){
  /* Pop nothing, push the newly-created array: */
  PUSH_OBJECT(array);
}
else if((integer_t)PARAMETER_1 == 1){
  /* One-element array: no need to look at the stack */
  array[1] = top;
  top = (word_t) array;
}
else{
  /* The array will have more than one element */
  integer_t i;
  
  /* Copy the element from top: */
  array[(integer_t)PARAMETER_1] = top;
  
  /* Copy all elements from stack: */
  for(i = 1; i <= (integer_t)PARAMETER_1 - 1; i++)
    array[i] = stack[undertop_stack_pointer + i - (integer_t)PARAMETER_1 + 1];
  
  /* Set top to the array and remove the other elements from the stack: */
  top = (word_t) array;
  undertop_stack_pointer -= ((integer_t)PARAMETER_1 - 1);
}
