001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TypesContext.java 3705 2007-03-28 20:00:24Z gbevin $
007: */
008: package com.uwyn.rife.continuations.instrument;
009:
010: import com.uwyn.rife.asm.Type;
011: import com.uwyn.rife.continuations.instrument.ContinuationDebug;
012: import com.uwyn.rife.continuations.instrument.TypesContext;
013: import com.uwyn.rife.continuations.instrument.TypesNode;
014: import com.uwyn.rife.tools.ExceptionUtils;
015: import java.util.HashMap;
016: import java.util.Map;
017: import java.util.Stack;
018: import java.util.logging.Level;
019: import java.util.logging.Logger;
020:
021: class TypesContext implements Cloneable {
022: public final static String CAT1_BOOLEAN = "1Z";
023: public final static String CAT1_CHAR = "1C";
024: public final static String CAT1_FLOAT = "1F";
025: public final static String CAT1_BYTE = "1B";
026: public final static String CAT1_SHORT = "1S";
027: public final static String CAT1_INT = "1I";
028: public final static String CAT1_ADDRESS = "1A";
029: public final static String CAT2_DOUBLE = "2D";
030: public final static String CAT2_LONG = "2J";
031: public final static String ARRAY_BOOLEAN = "[Z";
032: public final static String ARRAY_CHAR = "[C";
033: public final static String ARRAY_FLOAT = "[F";
034: public final static String ARRAY_BYTE = "[B";
035: public final static String ARRAY_SHORT = "[S";
036: public final static String ARRAY_INT = "[I";
037: public final static String ARRAY_DOUBLE = "[D";
038: public final static String ARRAY_LONG = "[J";
039: public final static String NULL = "NULL";
040:
041: private Map<Integer, String> mVars = null;
042: private Stack<String> mStack = null;
043:
044: private int mSort = TypesNode.REGULAR;
045:
046: private String mDebugIndent = null;
047:
048: TypesContext() {
049: mVars = new HashMap<Integer, String>();
050: mStack = new Stack<String>();
051: }
052:
053: TypesContext(Map<Integer, String> vars, Stack<String> stack) {
054: mVars = vars;
055: mStack = stack;
056: }
057:
058: Map<Integer, String> getVars() {
059: return mVars;
060: }
061:
062: Stack<String> getStack() {
063: return mStack;
064: }
065:
066: boolean hasVar(int var) {
067: return mVars.containsKey(var);
068: }
069:
070: String getVar(int var) {
071: return mVars.get(var);
072: }
073:
074: void setVar(int var, String type) {
075: mVars.put(var, type);
076: }
077:
078: int getVarType(int var) {
079: String type = getVar(var);
080: if (CAT1_INT == type) {
081: return Type.INT;
082: } else if (CAT1_FLOAT == type) {
083: return Type.FLOAT;
084: } else if (CAT2_LONG == type) {
085: return Type.LONG;
086: } else if (CAT2_DOUBLE == type) {
087: return Type.DOUBLE;
088: } else {
089: return Type.OBJECT;
090: }
091: }
092:
093: String peek() {
094: return mStack.peek();
095: }
096:
097: String pop() {
098: String result = null;
099: if (mStack.size() > 0) {
100: result = mStack.pop();
101: }
102: printStack();
103: return result;
104: }
105:
106: void push(String type) {
107: mStack.push(type);
108: printStack();
109: }
110:
111: Stack<String> getStackClone() {
112: return (Stack<String>) mStack.clone();
113: }
114:
115: void cloneVars() {
116: mVars = new HashMap<Integer, String>(mVars);
117: }
118:
119: void setSort(int type) {
120: mSort = type;
121: }
122:
123: int getSort() {
124: return mSort;
125: }
126:
127: void printStack() {
128: ///CLOVER:OFF
129: if (ContinuationDebug.LOGGER.isLoggable(Level.FINEST)) {
130: if (0 == mStack.size()) {
131: ContinuationDebug.LOGGER.finest(mDebugIndent
132: + " | empty");
133: } else {
134: for (int i = 0; i < mStack.size(); i++) {
135: ContinuationDebug.LOGGER.finest(mDebugIndent
136: + " | " + i + " : " + mStack.get(i));
137: }
138: }
139: }
140: ///CLOVER:ON
141: }
142:
143: void setDebugIndent(String debugIndent) {
144: mDebugIndent = debugIndent;
145: }
146:
147: TypesContext clone(TypesNode node) {
148: TypesContext new_context = new TypesContext(
149: new HashMap<Integer, String>(mVars),
150: (Stack<String>) mStack.clone());
151: new_context.setSort(node.getSort());
152: return new_context;
153: }
154:
155: public TypesContext clone() {
156: TypesContext new_context = null;
157: try {
158: new_context = (TypesContext) super .clone();
159: } catch (CloneNotSupportedException e) {
160: // this should never happen
161: Logger.getLogger("com.uwyn.rife.continuations").severe(
162: ExceptionUtils.getExceptionStackTrace(e));
163: }
164:
165: new_context.mVars = new HashMap<Integer, String>(mVars);
166: new_context.mStack = (Stack<String>) mStack.clone();
167:
168: return new_context;
169: }
170: }
|