src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/TestSeq.java
author joehw
Thu, 21 Dec 2017 17:08:05 -0800
changeset 48412 d4412e380f6b
parent 48409 5ab69533994b
permissions -rw-r--r--
8184431: References to @sun.com Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
/*
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     5
 * Licensed to the Apache Software Foundation (ASF) under one or more
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     6
 * contributor license agreements.  See the NOTICE file distributed with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     7
 * this work for additional information regarding copyright ownership.
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     8
 * The ASF licenses this file to You under the Apache License, Version 2.0
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
     9
 * (the "License"); you may not use this file except in compliance with
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    10
 * the License.  You may obtain a copy of the License at
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: TestSeq.java,v 1.2.4.1 2005/09/12 11:31:38 pvedula Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
package com.sun.org.apache.xalan.internal.xsltc.compiler;
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import com.sun.org.apache.bcel.internal.generic.GOTO_W;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.bcel.internal.generic.InstructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    31
import java.util.ArrayList;
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    32
import java.util.List;
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
    33
import java.util.Map;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * A test sequence is a sequence of patterns that
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 *  (1) occured in templates in the same mode
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 *  (2) share the same kernel node type (e.g. A/B and C/C/B)
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 *  (3) may also contain patterns matching "*" and "node()"
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 *      (element sequence only) or matching "@*" (attribute
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 *      sequence only).
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * A test sequence may have a default template, which will be
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * instantiated if none of the other patterns match.
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * @author Jacek Ambroziak
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * @author Santiago Pericas-Geertsen
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * @author Erwin Bolwidt <ejb@klomp.org>
48412
d4412e380f6b 8184431: References to @sun.com
joehw
parents: 48409
diff changeset
    49
 * @author Morten Jorgensen
48409
5ab69533994b 8193568: @LastModified tag in license header
joehw
parents: 47712
diff changeset
    50
 * @LastModified: Nov 2017
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
final class TestSeq {
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
     * Integer code for the kernel type of this test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
    private int _kernelType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
    /**
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    60
     * ArrayList of all patterns in the test sequence. May include
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
     * patterns with "*", "@*" or "node()" kernel.
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    63
    private List<LocationPathPattern> _patterns = null;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
     * A reference to the Mode object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
    private Mode _mode = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
     * Default template for this test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
    private Template _default = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
     * Instruction list representing this test sequence.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
    private InstructionList _instructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
     * Cached handle to avoid compiling more than once.
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
    private InstructionHandle _start = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
     * Creates a new test sequence given a set of patterns and a mode.
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
     */
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    88
    public TestSeq(List<LocationPathPattern> patterns, Mode mode) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
        this(patterns, -2, mode);
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
    92
    public TestSeq(List<LocationPathPattern> patterns, int kernelType, Mode mode) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
        _patterns = patterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
        _kernelType = kernelType;
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
        _mode = mode;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
     * Returns a string representation of this test sequence. Notice
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
     * that test sequences are mutable, so the value returned by this
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
     * method is different before and after calling reduce().
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
    public String toString() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
        final int count = _patterns.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        final StringBuffer result = new StringBuffer();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
        for (int i = 0; i < count; i++) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   108
            final LocationPathPattern pattern = _patterns.get(i);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
            if (i == 0) {
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   111
                result.append("Testseq for kernel ").append(_kernelType)
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
                      .append('\n');
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
            }
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   114
            result.append("   pattern ").append(i).append(": ")
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
                  .append(pattern.toString())
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
                  .append('\n');
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
        return result.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * Returns the instruction list for this test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
    public InstructionList getInstructionList() {
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        return _instructionList;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
     * Return the highest priority for a pattern in this test
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
     * sequence. This is either the priority of the first or
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
     * of the default pattern.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    public double getPriority() {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   134
        final Template template = (_patterns.isEmpty()) ? _default
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   135
            : ((Pattern) _patterns.get(0)).getTemplate();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        return template.getPriority();
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
     * Returns the position of the highest priority pattern in
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
     * this test sequence.
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    public int getPosition() {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   144
        final Template template = (_patterns.isEmpty()) ? _default
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   145
            : ((Pattern) _patterns.get(0)).getTemplate();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
        return template.getPosition();
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
     * Reduce the patterns in this test sequence. Creates a new
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
     * vector of patterns and sets the default pattern if it
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
     * finds a patterns that is fully reduced.
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    public void reduce() {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   155
        final List<LocationPathPattern> newPatterns = new ArrayList<>();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   157
        for (LocationPathPattern pattern : _patterns) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
            // Reduce this pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
            pattern.reduceKernelPattern();
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
            // Is this pattern fully reduced?
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
            if (pattern.isWildcard()) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
                _default = pattern.getTemplate();
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
                break;          // Ignore following patterns
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
            else {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   167
                newPatterns.add(pattern);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
        _patterns = newPatterns;
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
     * Returns, by reference, the templates that are included in
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
     * this test sequence. Note that a single template can occur
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
     * in several test sequences if its pattern is a union.
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
     */
33349
975138b77cff 8068842: Better JAXP data handling
joehw
parents: 25868
diff changeset
   178
    public void findTemplates(Map<Template, Object> templates) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
        if (_default != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
            templates.put(_default, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
        }
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   182
        for (LocationPathPattern pattern : _patterns) {
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
            templates.put(pattern.getTemplate(), this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
     * Get the instruction handle to a template's code. This is
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
     * used when a single template occurs in several test
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
     * sequences; that is, if its pattern is a union of patterns
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
     * (e.g. match="A/B | A/C").
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    private InstructionHandle getTemplateHandle(Template template) {
47712
bde0215f1f70 8181151: Fix lint warnings in JAXP repo: cast
joehw
parents: 47359
diff changeset
   194
        return _mode.getTemplateInstructionHandle(template);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
     * Returns pattern n in this test sequence
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    private LocationPathPattern getPattern(int n) {
47359
e1a6c0168741 8181150: Fix lint warnings in JAXP repo: rawtypes and unchecked
joehw
parents: 47216
diff changeset
   201
        return _patterns.get(n);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
     * Compile the code for this test sequence. Compile patterns
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
     * from highest to lowest priority. Note that since patterns
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
     * can be share by multiple test sequences, instruction lists
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
     * must be copied before backpatching.
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
    public InstructionHandle compile(ClassGenerator classGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
                                     MethodGenerator methodGen,
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
                                     InstructionHandle continuation)
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
        // Returned cached value if already compiled
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
        if (_start != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
            return _start;
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
        // If not patterns, then return handle for default template
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
        final int count = _patterns.size();
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
        if (count == 0) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
            return (_start = getTemplateHandle(_default));
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
        // Init handle to jump when all patterns failed
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
        InstructionHandle fail = (_default == null) ? continuation
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
            : getTemplateHandle(_default);
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
        // Compile all patterns in reverse order
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
        for (int n = count - 1; n >= 0; n--) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
            final LocationPathPattern pattern = getPattern(n);
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
            final Template template = pattern.getTemplate();
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
            final InstructionList il = new InstructionList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
            // Patterns expect current node on top of stack
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
            il.append(methodGen.loadCurrentNode());
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
            // Apply the test-code compiled for the pattern
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
            InstructionList ilist = methodGen.getInstructionList(pattern);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
            if (ilist == null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
                ilist = pattern.compile(classGen, methodGen);
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
                methodGen.addInstructionList(pattern, ilist);
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
            // Make a copy of the instruction list for backpatching
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
            InstructionList copyOfilist = ilist.copy();
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
            FlowList trueList = pattern.getTrueList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
            if (trueList != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
                trueList = trueList.copyAndRedirect(ilist, copyOfilist);
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
            FlowList falseList = pattern.getFalseList();
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
            if (falseList != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
                falseList = falseList.copyAndRedirect(ilist, copyOfilist);
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
            il.append(copyOfilist);
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
            // On success branch to the template code
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
            final InstructionHandle gtmpl = getTemplateHandle(template);
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
            final InstructionHandle success = il.append(new GOTO_W(gtmpl));
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
            if (trueList != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
                trueList.backPatch(success);
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
            if (falseList != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
                falseList.backPatch(fail);
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
            // Next pattern's 'fail' target is this pattern's first instruction
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
            fail = il.getStart();
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
            // Append existing instruction list to the end of this one
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
            if (_instructionList != null) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
                il.append(_instructionList);
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
            // Set current instruction list to be this one
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
            _instructionList = il;
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
        return (_start = fail);
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
}