#
#  File: icoptimize.k3
#  Handles the intermediate code instruction optimizations.
#
#  Copyright 2005 Radboud University of Nijmegen
#
#  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 2 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 Library General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  CVS ID: "$Id: icoptimize.k3,v 1.7 2006/01/25 15:57:14 marcs Exp $"
#
MODULE icoptimize.

DEFINES ACTION optimize intermediate code.

USES globals, id, rule, intermediate.

ACTION optimize intermediate code:
  if generative grammar option;
  reschedule intermediate code.

ACTION reschedule intermediate code:
  get number of syntax nonterminals (INT),
    reschedule (INT).

ACTION reschedule (>INT):
  [INT -> 0];
  [INT - 1 -> INT],
    convert INT to NONTERMINAL,
    reschedule (NONTERMINAL),
    reschedule (INT).

ACTION reschedule (>NONTERMINAL):
  get ic rule (NONTERMINAL, ICRULE),
    reschedule (ICRULE),
    store ic rule (NONTERMINAL, ICRULE).

FUNCTION reschedule (>ICRULE>):
  [ICRULE -> NR_FORMALS NR_LOCALS nr sons INT ICDEFS],
    reschedule (ICDEFS),
    [NR_FORMALS NR_LOCALS nr sons INT ICDEFS -> ICRULE];
  [ICRULE -> unused];
  ?.

FUNCTION reschedule (>ICDEFS>):
  [ICDEFS -> nil];
  [ICDEFS -> ICDEF ICDEFS],
    reschedule (ICDEF),
    reschedule (ICDEFS),
    [ICDEF ICDEFS -> ICDEFS];
  ?.

FUNCTION reschedule (>ICDEF>):
  [ICDEF -> lhs ICACTS rhs ICALTS],
    reschedule lhs ICACTS and rhs ICALTS,
    [lhs nil rhs ICALTS -> ICDEF].

FUNCTION reschedule lhs >ICACTS and rhs >ICALTS>:
  [ICALTS -> nil];
  [ICALTS -> ICALT ICALTS],
    reschedule lhs ICACTS and rhs ICALT,
    reschedule lhs ICACTS and rhs ICALTS,
    [ICALT ICALTS -> ICALTS];
  ?.

FUNCTION reschedule lhs >ICACTS and rhs >ICALT>:
  [ICALT -> ALTNO ICACTS1 COMMITED],
    append ICACTS1 to ICACTS,
    reschedule (ICACTS),
    [ALTNO ICACTS COMMITED -> ICALT].

FUNCTION reschedule (>ICACTS>):
  if negative memo option,
    (get negative memo test (ICACTS, ICACT),
       [ICACT ICACTS -> ICACTS];
     +);
  if directors option, # directors without negmemos
    (get director test (ICACTS, ICACT),
       [ICACT ICACTS -> ICACTS];
     +);
  +.


### Director retrieval:

TEST get director test (>ICACTS>, ICACT>):
  [ICACTS -> ICACT1 ICACTS1],
    (is director test (ICACT1),
       [ICACT1 -> ICACT],
       [ICACTS1 -> ICACTS];
     get director test (ICACTS1, ICACT),
       [ICACT1 ICACTS1 -> ICACTS]);
  [ICACTS -> nil],
    -;
  ?.

### Negative memo retrieval (similar):

TEST get negative memo test (>ICACTS>, ICACT>):
  [ICACTS -> ICACT1 ICACTS1],
    (is negative memo test (ICACT1),
       [ICACT1 -> ICACT],
       [ICACTS1 -> ICACTS];
     get negative memo test (ICACTS1, ICACT),
       [ICACT1 ICACTS1 -> ICACTS]);
  [ICACTS -> nil],
    -;
  ?.

