///////////////////////////////////////////////////////////////////////////////
//                             Lisaac Compiler                               //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program 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 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name        := LOCAL_SEQ;

  - copyright   := "2003-2007 Benoit Sonntag";

  
  - author      := "Sonntag Benoit (bsonntag@loria.fr)";
  - comment     := "Local slot";
  
Section Inherit
  
  - parent_any:ANY := ANY;
  
Section Private
  
  - list_busy:FAST_ARRAY[LOCAL] := FAST_ARRAY[LOCAL].create_with_capacity 512;
  
  - list_free:FAST_ARRAY[LOCAL_SEQ] := FAST_ARRAY[LOCAL_SEQ].create_with_capacity 512;
  
  - clean <-
  (
    last_write := NULL;
  );
  
Section Public  
    
  + last_write:WRITE;
  + last_seq_index:UINTEGER_32;
  + last_seq_or_and:UINTEGER_32;
  + last_seq_call_and_loop:UINTEGER_32;  
  + last_seq_call_local_and_loop:UINTEGER_32;
  
  + last_list_current:LIST;
  + last_index:INTEGER;
  
  - set_last_write w:WRITE <-
  (
    last_write := w;
  );
  
  - set_last_index i:INTEGER <-
  (
    last_index := i;
  );
  
  - new l:LOCAL <-
  ( + result:LOCAL_SEQ;
    
    (list_free.is_empty).if {
      result := clone;
    } else {
      result := list_free.last;
      list_free.remove_last;
    };
    result.clean;
    list_busy.add_last l;
    l.set_last_seq result;
  );
  
  - set_seq w:WRITE <-
  (
    // Save context
    last_write                  := w;
    last_seq_index              := seq_index;
    last_seq_or_and             := seq_or_and;
    last_seq_call_and_loop      := seq_call_and_loop;
    last_seq_call_local_and_loop:= seq_call_local_and_loop;    
    //
    last_list_current := list_current;
    last_index        := list_current.index; 
  );
  
  - clear <-
  ( + l:LOCAL;
    
    (list_busy.upper).downto (list_busy.lower) do { j:INTEGER;
      l := list_busy.item j;
      list_free.add_last (l.last_seq);      
      l.set_last_seq NULL;
    };
    list_busy.clear;
  );
