mirror of https://github.com/aamine/cbc
* net/loveruby/cflat/ast/Scope.java: ban all allocate* methods, privateEntitiesMap. Use #declareEntity instead.
* net/loveruby/cflat/ast/Frame.java: ditto. * net/loveruby/cflat/ast/ToplevelScope.java: ditto. * net/loveruby/cflat/ast/ToplevelScope.java (commonSymbols, globalVariables): static local variables are now in each Scopes, collect static local variables from them by myself. * net/loveruby/cflat/ast/Entity.java: #defineIn removed. * net/loveruby/cflat/ast/Variable.java: #defineIn removed. * net/loveruby/cflat/ast/DefinedVariable.java: #defineIn removed. * net/loveruby/cflat/ast/UndefinedVariable.java: #defineIn removed. * net/loveruby/cflat/ast/DefinedFunction.java: #defineIn removed. * net/loveruby/cflat/ast/UndefinedFunction.java: #defineIn removed. * net/loveruby/cflat/compiler/LocalReferenceResolver.java: follow these changes. git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@3886 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
parent
1ac456d72e
commit
8c885a42bd
30
ChangeLog
30
ChangeLog
|
@ -1,3 +1,33 @@
|
|||
Mon Feb 11 03:08:16 2008 Minero Aoki <aamine@loveruby.net>
|
||||
|
||||
* net/loveruby/cflat/ast/Scope.java: ban all allocate* methods,
|
||||
privateEntitiesMap. Use #declareEntity instead.
|
||||
|
||||
* net/loveruby/cflat/ast/Frame.java: ditto.
|
||||
|
||||
* net/loveruby/cflat/ast/ToplevelScope.java: ditto.
|
||||
|
||||
* net/loveruby/cflat/ast/ToplevelScope.java (commonSymbols,
|
||||
globalVariables): static local variables are now in each Scopes,
|
||||
collect static local variables from them by myself.
|
||||
|
||||
* net/loveruby/cflat/ast/Entity.java: #defineIn removed.
|
||||
|
||||
* net/loveruby/cflat/ast/Variable.java: #defineIn removed.
|
||||
|
||||
* net/loveruby/cflat/ast/DefinedVariable.java: #defineIn removed.
|
||||
|
||||
* net/loveruby/cflat/ast/UndefinedVariable.java: #defineIn
|
||||
removed.
|
||||
|
||||
* net/loveruby/cflat/ast/DefinedFunction.java: #defineIn removed.
|
||||
|
||||
* net/loveruby/cflat/ast/UndefinedFunction.java: #defineIn
|
||||
removed.
|
||||
|
||||
* net/loveruby/cflat/compiler/LocalReferenceResolver.java: follow
|
||||
these changes.
|
||||
|
||||
Mon Feb 11 00:17:28 2008 Minero Aoki <aamine@loveruby.net>
|
||||
|
||||
* net/loveruby/cflat/compiler/LocalReferenceResolver.java: add
|
||||
|
|
|
@ -47,10 +47,6 @@ public class DefinedFunction extends Function {
|
|||
return true;
|
||||
}
|
||||
|
||||
public void defineIn(ToplevelScope toplevel) {
|
||||
toplevel.defineFunction(this);
|
||||
}
|
||||
|
||||
class JumpEntry {
|
||||
public Label label;
|
||||
public long numRefered;
|
||||
|
|
|
@ -10,7 +10,6 @@ public class DefinedVariable extends Variable {
|
|||
String name, ExprNode init) {
|
||||
super(priv, type, name);
|
||||
initializer = init;
|
||||
toplevel = false;
|
||||
sequence = -1;
|
||||
}
|
||||
|
||||
|
@ -34,14 +33,6 @@ public class DefinedVariable extends Variable {
|
|||
this.initializer = expr;
|
||||
}
|
||||
|
||||
public void defineIn(ToplevelScope toplevel) {
|
||||
if (isPrivate()) {
|
||||
toplevel.allocatePrivateVariable(this);
|
||||
} else {
|
||||
toplevel.allocateVariable(this);
|
||||
}
|
||||
}
|
||||
|
||||
protected void _dump(Dumper d) {
|
||||
d.printMember("name", name);
|
||||
d.printMember("isPrivate", isPrivate);
|
||||
|
|
|
@ -50,7 +50,6 @@ abstract public class Entity extends Node {
|
|||
return (nRefered > 0);
|
||||
}
|
||||
|
||||
abstract public void defineIn(ToplevelScope toplevel);
|
||||
abstract public AsmEntity address();
|
||||
|
||||
public Location location() {
|
||||
|
|
|
@ -6,8 +6,20 @@ public class Frame extends Scope {
|
|||
super(up);
|
||||
}
|
||||
|
||||
public void allocatePrivateVariable(Variable var) {
|
||||
throw new Error("Frame#allocatePrivateVariable called");
|
||||
public List staticLocalVariables() {
|
||||
List result = new ArrayList();
|
||||
Iterator scopes = allChildren();
|
||||
while (scopes.hasNext()) {
|
||||
Scope s = (Scope)scopes.next();
|
||||
Iterator vars = s.variables();
|
||||
while (vars.hasNext()) {
|
||||
DefinedVariable var = (DefinedVariable)vars.next();
|
||||
if (var.isPrivate()) {
|
||||
result.add(var);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public long numLocalVariables() {
|
||||
|
|
|
@ -6,11 +6,9 @@ import java.util.*;
|
|||
public class Scope {
|
||||
protected Scope parent;
|
||||
protected List children;
|
||||
|
||||
protected long numAllEntities;
|
||||
protected List entities;
|
||||
protected Map entitiesMap;
|
||||
protected Map privateEntitiesMap;
|
||||
protected long numAllEntities;
|
||||
|
||||
public Scope(Scope up) {
|
||||
parent = up;
|
||||
|
@ -19,7 +17,6 @@ public class Scope {
|
|||
numAllEntities = -1;
|
||||
entities = new ArrayList();
|
||||
entitiesMap = new HashMap();
|
||||
privateEntitiesMap = new HashMap();
|
||||
}
|
||||
|
||||
public boolean isToplevel() {
|
||||
|
@ -61,55 +58,30 @@ public class Scope {
|
|||
}
|
||||
}
|
||||
|
||||
/** Allocates variable var in this scope. */
|
||||
// #@@range/allocateVariable{
|
||||
public void allocateVariable(Variable var) {
|
||||
checkDuplicatedVariable(var.name());
|
||||
addEntity(var);
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Allocates static variable var in this scope.
|
||||
* This method causes var defined in the top scope,
|
||||
* instead of this scope.
|
||||
*/
|
||||
// #@@range/allocateStaticLocalVariable{
|
||||
public void allocateStaticLocalVariable(Variable var) {
|
||||
checkDuplicatedVariable(var.name());
|
||||
toplevel().allocateStaticLocalVariable(var);
|
||||
addPrivateEntity(var);
|
||||
}
|
||||
// #@@}
|
||||
|
||||
protected void addEntity(Entity ent) {
|
||||
/** Declare variable or function in this scope. */
|
||||
// #@@range/declareEntity{
|
||||
public void declareEntity(Entity ent) {
|
||||
if (entitiesMap.containsKey(ent.name())) {
|
||||
throw new Error("duplicated entity: " + ent.name());
|
||||
}
|
||||
entities.add(ent);
|
||||
entitiesMap.put(ent.name(), ent);
|
||||
}
|
||||
|
||||
protected void addPrivateEntity(Entity ent) {
|
||||
privateEntitiesMap.put(ent.name(), ent);
|
||||
}
|
||||
|
||||
protected void checkDuplicatedVariable(String name) {
|
||||
if (entitiesMap.containsKey(name)
|
||||
|| privateEntitiesMap.containsKey(name)) {
|
||||
throw new Error("duplicated variable: " + name);
|
||||
}
|
||||
}
|
||||
// #@@}
|
||||
|
||||
public boolean isDefinedLocally(String name) {
|
||||
return (entitiesMap.containsKey(name)
|
||||
|| privateEntitiesMap.containsKey(name));
|
||||
return entitiesMap.containsKey(name);
|
||||
}
|
||||
|
||||
// #@@range/get{
|
||||
public Entity get(String name) throws SemanticException {
|
||||
Entity ent;
|
||||
ent = (Entity)privateEntitiesMap.get(name);
|
||||
if (ent != null) return ent;
|
||||
ent = (Entity)entitiesMap.get(name);
|
||||
if (ent != null) return ent;
|
||||
return parent.get(name);
|
||||
Entity ent = (Entity)entitiesMap.get(name);
|
||||
if (ent != null) {
|
||||
return ent;
|
||||
}
|
||||
else {
|
||||
return parent.get(name);
|
||||
}
|
||||
}
|
||||
// #@@}
|
||||
|
||||
|
|
|
@ -4,93 +4,24 @@ import net.loveruby.cflat.exception.*;
|
|||
import java.util.*;
|
||||
|
||||
public class ToplevelScope extends Scope {
|
||||
protected List privateEntities;
|
||||
protected Map sequenceTable;
|
||||
protected List staticLocalVariables;
|
||||
|
||||
public ToplevelScope() {
|
||||
super(null);
|
||||
privateEntities = new ArrayList();
|
||||
sequenceTable = new HashMap();
|
||||
}
|
||||
|
||||
public boolean isToplevel() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Declares external symbols */
|
||||
// #@@range/declare{
|
||||
public void declare(Entity entity) {
|
||||
addEntity(entity);
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Defines entity */
|
||||
// #@@range/define{
|
||||
public void define(Entity entity) {
|
||||
entity.defineIn(this);
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Defines (local) function */
|
||||
// #@@range/defineFunction{
|
||||
public void defineFunction(DefinedFunction f) {
|
||||
if (f.isPrivate()) {
|
||||
addPrivateEntity(f);
|
||||
}
|
||||
else {
|
||||
addEntity(f);
|
||||
}
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Allocates public global variable or common symbol */
|
||||
// #@@range/allocateVariable{
|
||||
public void allocateVariable(Variable var) {
|
||||
addEntity(var);
|
||||
var.toplevelDefinition();
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Allocates private global variable or common symbol */
|
||||
// #@@range/allocatePrivateVariable{
|
||||
public void allocatePrivateVariable(Variable var) {
|
||||
addPrivateEntity(var);
|
||||
var.toplevelDefinition();
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Allocates static local variable */
|
||||
// #@@range/allocateStaticLocalVariable{
|
||||
public void allocateStaticLocalVariable(Variable var) {
|
||||
addPrivateEntity(var);
|
||||
Long seq = (Long)sequenceTable.get(var.name());
|
||||
if (seq == null) {
|
||||
var.setSequence(0);
|
||||
sequenceTable.put(var.name(), new Long(1));
|
||||
}
|
||||
else {
|
||||
var.setSequence(seq.longValue());
|
||||
sequenceTable.put(var.name(), new Long(seq.longValue() + 1));
|
||||
}
|
||||
}
|
||||
// #@@}
|
||||
|
||||
// #@@range/addPrivateEntity{
|
||||
protected void addPrivateEntity(Entity ent) {
|
||||
super.addPrivateEntity(ent);
|
||||
privateEntities.add(ent);
|
||||
}
|
||||
// #@@}
|
||||
|
||||
/** Searches and gets entity searching scopes upto ToplevelScope. */
|
||||
// #@@range/get{
|
||||
public Entity get(String name) throws SemanticException {
|
||||
Entity ent;
|
||||
ent = (Entity)privateEntitiesMap.get(name);
|
||||
if (ent != null) return ent;
|
||||
ent = (Entity)entitiesMap.get(name);
|
||||
if (ent != null) return ent;
|
||||
throw new SemanticException("unresolved reference: " + name);
|
||||
Entity ent = (Entity)entitiesMap.get(name);
|
||||
if (ent == null) {
|
||||
throw new SemanticException("unresolved reference: " + name);
|
||||
}
|
||||
return ent;
|
||||
}
|
||||
// #@@}
|
||||
|
||||
|
@ -98,6 +29,32 @@ public class ToplevelScope extends Scope {
|
|||
throw new Error("TopScope#allVariables called");
|
||||
}
|
||||
|
||||
protected List staticLocalVariables() {
|
||||
if (staticLocalVariables == null) {
|
||||
staticLocalVariables = new ArrayList();
|
||||
Iterator frames = children.iterator();
|
||||
while (frames.hasNext()) {
|
||||
Frame f = (Frame)frames.next();
|
||||
staticLocalVariables.addAll(f.staticLocalVariables());
|
||||
}
|
||||
Map seqTable = new HashMap();
|
||||
Iterator vars = staticLocalVariables.iterator();
|
||||
while (vars.hasNext()) {
|
||||
DefinedVariable var = (DefinedVariable)vars.next();
|
||||
Long seq = (Long)seqTable.get(var.name());
|
||||
if (seq == null) {
|
||||
var.setSequence(0);
|
||||
seqTable.put(var.name(), new Long(1));
|
||||
}
|
||||
else {
|
||||
var.setSequence(seq.longValue());
|
||||
seqTable.put(var.name(), new Long(seq.longValue() + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
return staticLocalVariables;
|
||||
}
|
||||
|
||||
/** Returns the list of global variables.
|
||||
* A global variable is a variable which has
|
||||
* global scope and is initialized. */
|
||||
|
@ -105,13 +62,13 @@ public class ToplevelScope extends Scope {
|
|||
List result = new ArrayList();
|
||||
List src = new ArrayList();
|
||||
src.addAll(entities);
|
||||
src.addAll(privateEntities);
|
||||
src.addAll(staticLocalVariables());
|
||||
Iterator ents = src.iterator();
|
||||
while (ents.hasNext()) {
|
||||
Object ent = ents.next();
|
||||
if (ent instanceof DefinedVariable) {
|
||||
DefinedVariable var = (DefinedVariable)ent;
|
||||
if (var.isInitialized()) {
|
||||
if (var.hasInitializer()) {
|
||||
result.add(var);
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +83,7 @@ public class ToplevelScope extends Scope {
|
|||
List result = new ArrayList();
|
||||
List src = new ArrayList();
|
||||
src.addAll(entities);
|
||||
src.addAll(privateEntities);
|
||||
src.addAll(staticLocalVariables());
|
||||
Iterator ents = src.iterator();
|
||||
while (ents.hasNext()) {
|
||||
Object ent = ents.next();
|
||||
|
|
|
@ -19,10 +19,6 @@ public class UndefinedFunction extends Function {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void defineIn(ToplevelScope scope) {
|
||||
throw new Error("UndefinedFunction#defineIn");
|
||||
}
|
||||
|
||||
protected void _dump(Dumper d) {
|
||||
d.printMember("name", name);
|
||||
d.printMember("isPrivate", isPrivate());
|
||||
|
|
|
@ -15,10 +15,6 @@ public class UndefinedVariable extends Variable {
|
|||
return name();
|
||||
}
|
||||
|
||||
public void defineIn(ToplevelScope s) {
|
||||
throw new Error("UndefinedVariable#defineIn called");
|
||||
}
|
||||
|
||||
public void setAddress(AsmEntity addr) {
|
||||
throw new Error("UndefinedVariable#setAddress");
|
||||
}
|
||||
|
|
|
@ -3,20 +3,14 @@ import net.loveruby.cflat.type.*;
|
|||
import net.loveruby.cflat.asm.*;
|
||||
|
||||
abstract public class Variable extends Entity {
|
||||
protected boolean toplevel;
|
||||
protected long sequence;
|
||||
protected AsmEntity address;
|
||||
|
||||
public Variable(boolean priv, TypeNode type, String name) {
|
||||
super(priv, type, name);
|
||||
toplevel = false;
|
||||
sequence = -1;
|
||||
}
|
||||
|
||||
public void toplevelDefinition() {
|
||||
toplevel = true;
|
||||
}
|
||||
|
||||
public void setSequence(long seq) {
|
||||
this.sequence = seq;
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ public class LocalReferenceResolver extends Visitor {
|
|||
constantTable = ast.constantTable();
|
||||
|
||||
declareToplevelEntities(ast.declarations());
|
||||
defineToplevelEntities(ast.entities());
|
||||
declareToplevelEntities(ast.entities());
|
||||
resolveGvarInitializers(ast.variables());
|
||||
resolveFunctions(ast.functions());
|
||||
toplevel.checkReferences(errorHandler);
|
||||
|
@ -45,15 +45,7 @@ public class LocalReferenceResolver extends Visitor {
|
|||
// #@@range/declareToplevelEntities{
|
||||
protected void declareToplevelEntities(Iterator decls) {
|
||||
while (decls.hasNext()) {
|
||||
toplevel.declare((Entity)decls.next());
|
||||
}
|
||||
}
|
||||
// #@@}
|
||||
|
||||
// #@@range/defineToplevelEntities{
|
||||
protected void defineToplevelEntities(Iterator entities) {
|
||||
while (entities.hasNext()) {
|
||||
toplevel.define((Entity)entities.next());
|
||||
toplevel.declareEntity((Entity)decls.next());
|
||||
}
|
||||
}
|
||||
// #@@}
|
||||
|
@ -89,7 +81,7 @@ public class LocalReferenceResolver extends Visitor {
|
|||
error(param, "duplicated parameter: " + param.name());
|
||||
}
|
||||
else {
|
||||
frame.allocateVariable(param);
|
||||
frame.declareEntity(param);
|
||||
}
|
||||
}
|
||||
scopeStack.addLast(frame);
|
||||
|
@ -114,13 +106,7 @@ public class LocalReferenceResolver extends Visitor {
|
|||
protected void pushScope(Iterator vars) {
|
||||
Scope scope = new Scope(currentScope());
|
||||
while (vars.hasNext()) {
|
||||
Variable var = (Variable)vars.next();
|
||||
if (var.isPrivate()) {
|
||||
scope.allocateStaticLocalVariable(var);
|
||||
}
|
||||
else {
|
||||
scope.allocateVariable(var);
|
||||
}
|
||||
scope.declareEntity((DefinedVariable)vars.next());
|
||||
}
|
||||
scopeStack.addLast(scope);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue