/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This file is part of SimpleC. * * See the file "SimpleC-LICENSE" for Copyright information and * * the terms and conditions for copying, distribution and * * modification of SimpleC. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package org.sablecc.simplec; import java.util.*; import org.sablecc.simplec.node.*; import java.util.*; import java.util.Map.Entry; class PointsTo { Start ast; Declarations declarations; Hashtable variables = new Hashtable(1); PointsTo(Start ast, Declarations declarations) { this.ast = ast; this.declarations = declarations; initialize(); new PointsToAnalysis(this); } private void initialize() { // for all function declarations for(Iterator i = declarations.function_declarations.iterator(); i.hasNext();) { TIdentifier id = (TIdentifier) i.next(); // if there is no function definition if(declarations.find_fct_def.get(id.getText()) == null) { Ref ref = new Ref(); ref.lam = new Variable(new Lam()); // f = Ref(bottom, lam) variables.put(id, new Variable(ref)); } } // for all global variables for(Iterator i = declarations.global_variables.iterator(); i.hasNext();) { TIdentifier id = (TIdentifier) i.next(); // v = Ref(bottom, bottom) variables.put(id, new Variable(new Ref())); } // for all function definitions for(Iterator i = declarations.function_definitions.iterator(); i.hasNext();) { TIdentifier fdef = (TIdentifier) i.next(); // for all local variables for(Iterator j = ((LinkedList) declarations.local_variables.get(fdef)).iterator(); j.hasNext();) { TIdentifier id = (TIdentifier) j.next(); // v = Ref(bottom, bottom) variables.put(id, new Variable(new Ref())); } Lam lam = new Lam(); // for all parameters for(Iterator j = ((LinkedList) declarations.parameters.get(fdef)).iterator(); j.hasNext();) { TIdentifier id = (TIdentifier) j.next(); Variable var = new Variable(new Ref()); // p = Ref(bottom, bottom) variables.put(id, var); // Lam(..., Type(param(i)), ...) lam.args.add(var); } Ref ref = new Ref(); ref.lam = new Variable(lam); variables.put(fdef, new Variable(ref)); } } public String toString() { TreeMap tbl = new TreeMap(IntegerComparator.instance); for(Enumeration e = variables.keys(); e.hasMoreElements();) { Object node = e.nextElement(); Integer var = new Integer(((Variable) variables.get(node)).ecr().number); if(tbl.get(var) == null) { tbl.put(var, new LinkedList()); } ((LinkedList) tbl.get(var)).add(node); } StringBuffer s = new StringBuffer(); for(Iterator e = tbl.entrySet().iterator(); e.hasNext(); ) { Map.Entry entry = (Map.Entry) e.next(); Integer var = (Integer) entry.getKey(); LinkedList list = (LinkedList) entry.getValue(); boolean comma = false; for(Iterator i = list.iterator(); i.hasNext();) { TIdentifier id = (TIdentifier) i.next(); if(comma) { s.append(","); } else { comma = true; } s.append(id.getText()); } s.append(" : T" + var); s.append(System.getProperty("line.separator")); } for(Iterator i = Variable.variables.iterator(); i.hasNext();) { Variable var = (Variable) i.next(); if((var == var.ecr()) && (var.type != Bottom.instance)) { s.append(var); s.append(System.getProperty("line.separator")); } } return s.toString(); } }