KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > flow > CreateConstantArray


1 /* CreateConstantArray Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: CreateConstantArray.java,v 1.16.2.1 2002/05/28 17:34:08 hoenicke Exp $
18  */

19
20 package jode.flow;
21 import jode.GlobalOptions;
22 import jode.expr.*;
23 import jode.type.Type;
24
25 public class CreateConstantArray {
26
27     public static boolean transform(InstructionContainer ic,
28                                     StructuredBlock last) {
29         /* Situation:
30          * PUSH new Array[] // or a constant array operator.
31          * DUP // duplicate array reference
32          * POP[index] = value
33          * ...
34          */

35         if (last.outer instanceof SequentialBlock) {
36
37         SequentialBlock sequBlock = (SequentialBlock) last.outer;
38
39             if (!(ic.getInstruction() instanceof StoreInstruction)
40         || ic.getInstruction().getFreeOperandCount() != 1
41                 || !(sequBlock.subBlocks[0] instanceof SpecialBlock)
42                 || !(sequBlock.outer instanceof SequentialBlock))
43                 return false;
44
45             StoreInstruction store = (StoreInstruction) ic.getInstruction();
46
47         if (!(store.getLValue() instanceof ArrayStoreOperator))
48         return false;
49
50         ArrayStoreOperator lvalue = (ArrayStoreOperator) store.getLValue();
51
52         if (!(lvalue.getSubExpressions()[0] instanceof NopOperator)
53         || !(lvalue.getSubExpressions()[1] instanceof ConstOperator))
54         return false;
55
56             Expression expr = store.getSubExpressions()[1];
57             ConstOperator indexOp
58         = (ConstOperator) lvalue.getSubExpressions()[1];
59             SpecialBlock dup = (SpecialBlock) sequBlock.subBlocks[0];
60             sequBlock = (SequentialBlock) sequBlock.outer;
61
62             if (dup.type != SpecialBlock.DUP
63                 || dup.depth != 0 || dup.count != 1
64         || !(indexOp.getValue() instanceof Integer JavaDoc)
65                 || !(sequBlock.subBlocks[0] instanceof InstructionBlock))
66                 return false;
67
68             int index = ((Integer JavaDoc) indexOp.getValue()).intValue();
69             InstructionBlock ib = (InstructionBlock)sequBlock.subBlocks[0];
70
71             if (ib.getInstruction() instanceof NewArrayOperator) {
72                 /* This is the first element */
73                 NewArrayOperator newArray =
74                     (NewArrayOperator) ib.getInstruction();
75                 if (newArray.getDimensions() != 1
76                     || !(newArray.getSubExpressions()[0]
77                          instanceof ConstOperator))
78                     return false;
79                     
80                 ConstOperator countop =
81                     (ConstOperator) newArray.getSubExpressions()[0];
82                 if (!(countop.getValue() instanceof Integer JavaDoc))
83                     return false;
84
85                 int arraylength = ((Integer JavaDoc) countop.getValue()).intValue();
86                 if (arraylength <= index)
87                     return false;
88
89                 if (GlobalOptions.verboseLevel > 0)
90                     GlobalOptions.err.print('a');
91
92                 ConstantArrayOperator cao
93                     = new ConstantArrayOperator(newArray.getType(),
94                                                 arraylength);
95                 cao.setValue(index, expr);
96                 ic.setInstruction(cao);
97                 ic.moveDefinitions(sequBlock, last);
98                 last.replace(sequBlock);
99                 return true;
100
101             } else if (ib.getInstruction() instanceof ConstantArrayOperator) {
102                 ConstantArrayOperator cao
103                     = (ConstantArrayOperator) ib.getInstruction();
104                 if (cao.setValue(index, expr)) {
105                     /* adding Element succeeded */
106                     ic.setInstruction(cao);
107                     ic.moveDefinitions(sequBlock, last);
108                     last.replace(sequBlock);
109                     return true;
110                 }
111             }
112             
113         }
114         return false;
115     }
116 }
117
Popular Tags