r4896@macbookpro: aamine | 2009-05-24 02:08:03 +0900

* utilize final field.
 


git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@4236 1b9489fe-b721-0410-924e-b54b9192deb8
This commit is contained in:
Minero Aoki 2009-05-23 17:08:24 +00:00
parent c7254ff547
commit ad7af66f8b
12 changed files with 99 additions and 100 deletions

View File

@ -1,3 +1,7 @@
Sun May 24 02:08:47 2009 Minero Aoki <aamine@loveruby.net>
* utilize final field.
Sun May 24 01:18:36 2009 Minero Aoki <aamine@loveruby.net>
* net/loveruby/cflat/compiler/IRGenerator.java: reorder methods.

View File

@ -20,7 +20,7 @@ public class Compiler {
new Compiler(ProgramName).commandMain(args);
}
private ErrorHandler errorHandler;
private final ErrorHandler errorHandler;
public Compiler(String programName) {
this.errorHandler = new ErrorHandler(programName);
@ -111,7 +111,7 @@ public class Compiler {
TypeTable types = opts.typeTable();
AST sem = semanticAnalyze(ast, types, opts);
if (dumpSemant(sem, opts.mode())) return;
IR ir = new IRGenerator(errorHandler).generate(sem, types);
IR ir = new IRGenerator(types, errorHandler).generate(sem);
if (dumpIR(ir, opts.mode())) return;
String asm = generateAssembly(ir, opts);
if (dumpAsm(asm, opts.mode())) return;
@ -124,17 +124,17 @@ public class Compiler {
opts.loader(), errorHandler, opts.doesDebugParser());
}
public AST semanticAnalyze(AST ast, TypeTable typeTable,
public AST semanticAnalyze(AST ast, TypeTable types,
Options opts) throws SemanticException {
new LocalResolver(errorHandler).resolve(ast);
new TypeResolver(errorHandler).resolve(ast, typeTable);
typeTable.semanticCheck(errorHandler);
new TypeResolver(types, errorHandler).resolve(ast);
types.semanticCheck(errorHandler);
if (opts.mode() == CompilerMode.DumpReference) {
ast.dump();
return ast;
}
new DereferenceChecker(errorHandler).check(ast, typeTable);
new TypeChecker(errorHandler).check(ast, typeTable);
new DereferenceChecker(types, errorHandler).check(ast);
new TypeChecker(types, errorHandler).check(ast);
return ast;
}

View File

@ -45,7 +45,7 @@ enum CompilerMode {
return m;
}
private String option;
private final String option;
CompilerMode(String option) {
this.option = option;

View File

@ -8,19 +8,17 @@ import java.util.*;
class DereferenceChecker extends Visitor {
// #@@range/ctor{
private ErrorHandler errorHandler;
private final TypeTable typeTable;
private final ErrorHandler errorHandler;
public DereferenceChecker(ErrorHandler h) {
public DereferenceChecker(TypeTable typeTable, ErrorHandler h) {
this.typeTable = typeTable;
this.errorHandler = h;
}
// #@@}
// #@@range/check_AST{
private TypeTable typeTable;
public void check(AST ast, TypeTable typeTable)
throws SemanticException {
this.typeTable = typeTable;
public void check(AST ast) throws SemanticException {
for (DefinedVariable var : ast.definedVariables()) {
checkToplevelVariable(var);
}

View File

@ -11,19 +11,18 @@ import net.loveruby.cflat.exception.*;
import java.util.*;
class IRGenerator implements ASTVisitor<Void, Expr> {
private ErrorHandler errorHandler;
private TypeTable typeTable;
private final TypeTable typeTable;
private final ErrorHandler errorHandler;
// #@@range/ctor{
public IRGenerator(ErrorHandler errorHandler) {
public IRGenerator(TypeTable typeTable, ErrorHandler errorHandler) {
this.typeTable = typeTable;
this.errorHandler = errorHandler;
}
// #@@}
// #@@range/generate{
public IR generate(AST ast, TypeTable typeTable)
throws SemanticException {
this.typeTable = typeTable;
public IR generate(AST ast) throws SemanticException {
for (DefinedVariable var : ast.definedVariables()) {
if (var.hasInitializer()) {
var.setIR(transformExpr(var.initializer()));
@ -33,7 +32,7 @@ class IRGenerator implements ASTVisitor<Void, Expr> {
f.setIR(compileFunctionBody(f));
}
if (errorHandler.errorOccured()) {
throw new SemanticException("Simplify failed.");
throw new SemanticException("IR generation failed.");
}
return ast.ir();
}
@ -43,11 +42,11 @@ class IRGenerator implements ASTVisitor<Void, Expr> {
// Definitions
//
private List<Stmt> stmts;
private LinkedList<LocalScope> scopeStack;
private LinkedList<Label> breakStack;
private LinkedList<Label> continueStack;
private Map<String, JumpEntry> jumpMap;
List<Stmt> stmts;
LinkedList<LocalScope> scopeStack;
LinkedList<Label> breakStack;
LinkedList<Label> continueStack;
Map<String, JumpEntry> jumpMap;
public List<Stmt> compileFunctionBody(DefinedFunction f) {
stmts = new ArrayList<Stmt>();

View File

@ -1,7 +1,7 @@
package net.loveruby.cflat.compiler;
class LdOption implements LdArg {
private String arg;
private final String arg;
LdOption(String arg) {
this.arg = arg;

View File

@ -7,30 +7,29 @@ import java.util.*;
public class LocalResolver extends Visitor {
// #@@range/ctor{
protected ErrorHandler errorHandler;
protected ToplevelScope toplevel;
protected LinkedList<Scope> scopeStack;
protected ConstantTable constantTable;
private final LinkedList<Scope> scopeStack;
private final ConstantTable constantTable;
private final ErrorHandler errorHandler;
public LocalResolver(ErrorHandler h) {
this.errorHandler = h;
this.scopeStack = new LinkedList<Scope>();
this.constantTable = new ConstantTable();
}
// #@@}
protected void resolve(StmtNode n) {
private void resolve(StmtNode n) {
n.accept(this);
}
protected void resolve(ExprNode n) {
private void resolve(ExprNode n) {
n.accept(this);
}
// #@@range/resolve{
public void resolve(AST ast) throws SemanticException {
toplevel = new ToplevelScope();
scopeStack = new LinkedList<Scope>();
ToplevelScope toplevel = new ToplevelScope();
scopeStack.add(toplevel);
constantTable = new ConstantTable();
// #@@range/declareToplevel{
for (Entity decl : ast.declarations()) {
@ -56,7 +55,7 @@ public class LocalResolver extends Visitor {
// #@@}
// #@@range/resolveGvarInitializers{
protected void resolveGvarInitializers(List<DefinedVariable> gvars) {
private void resolveGvarInitializers(List<DefinedVariable> gvars) {
for (DefinedVariable gvar : gvars) {
if (gvar.hasInitializer()) {
resolve(gvar.initializer());
@ -72,7 +71,7 @@ public class LocalResolver extends Visitor {
}
// #@@range/resolveFunctions{
protected void resolveFunctions(List<DefinedFunction> funcs) {
private void resolveFunctions(List<DefinedFunction> funcs) {
for (DefinedFunction func : funcs) {
pushScope(func.parameters());
resolve(func.body());
@ -91,7 +90,7 @@ public class LocalResolver extends Visitor {
// #@@}
// #@@range/pushScope{
protected void pushScope(List<? extends DefinedVariable> vars) {
private void pushScope(List<? extends DefinedVariable> vars) {
LocalScope scope = new LocalScope(currentScope());
for (DefinedVariable var : vars) {
if (scope.isDefinedLocally(var.name())) {
@ -107,13 +106,13 @@ public class LocalResolver extends Visitor {
// #@@}
// #@@range/popScope{
protected LocalScope popScope() {
private LocalScope popScope() {
return (LocalScope)scopeStack.removeLast();
}
// #@@}
// #@@range/currentScope{
protected Scope currentScope() {
private Scope currentScope() {
return scopeStack.getLast();
}
// #@@}
@ -139,11 +138,11 @@ public class LocalResolver extends Visitor {
}
// #@@}
protected void error(Node node, String message) {
private void error(Node node, String message) {
errorHandler.error(node.location(), message);
}
protected void error(Location loc, String message) {
private void error(Location loc, String message) {
errorHandler.error(loc, message);
}
}

View File

@ -7,28 +7,28 @@ import net.loveruby.cflat.exception.*;
import java.util.*;
class TypeChecker extends Visitor {
protected TypeTable typeTable;
protected ErrorHandler errorHandler;
private DefinedFunction currentFunction;
private final TypeTable typeTable;
private final ErrorHandler errorHandler;
// #@@range/ctor{
public TypeChecker(ErrorHandler errorHandler) {
public TypeChecker(TypeTable typeTable, ErrorHandler errorHandler) {
this.typeTable = typeTable;
this.errorHandler = errorHandler;
}
// #@@}
protected void check(StmtNode node) {
private void check(StmtNode node) {
visitStmt(node);
}
protected void check(ExprNode node) {
private void check(ExprNode node) {
visitExpr(node);
}
// #@@range/check_AST{
public void check(AST ast, TypeTable typeTable)
throws SemanticException {
this.typeTable = typeTable;
DefinedFunction currentFunction;
public void check(AST ast) throws SemanticException {
for (DefinedVariable var : ast.definedVariables()) {
checkVariable(var);
}
@ -44,13 +44,13 @@ class TypeChecker extends Visitor {
}
// #@@}
protected void checkReturnType(DefinedFunction f) {
private void checkReturnType(DefinedFunction f) {
if (isInvalidReturnType(f.returnType())) {
error(f.location(), "returns invalid type: " + f.returnType());
}
}
protected void checkParamTypes(DefinedFunction f) {
private void checkParamTypes(DefinedFunction f) {
for (Parameter param : f.parameters()) {
if (isInvalidParameterType(param.type())) {
error(param.location(),
@ -73,7 +73,7 @@ class TypeChecker extends Visitor {
return null;
}
protected void checkVariable(DefinedVariable var) {
private void checkVariable(DefinedVariable var) {
if (isInvalidVariableType(var.type())) {
error(var.location(), "invalid variable type");
return;
@ -115,7 +115,7 @@ class TypeChecker extends Visitor {
return null;
}
protected void checkCond(ExprNode cond) {
private void checkCond(ExprNode cond) {
mustBeScalar(cond, "condition expression");
}
@ -193,7 +193,7 @@ class TypeChecker extends Visitor {
*
* "0" has a type integer, but we can cast (int)0 to (char)0 safely.
*/
protected boolean isSafeIntegerCast(Node node, Type type) {
private boolean isSafeIntegerCast(Node node, Type type) {
if (! type.isInteger()) return false;
IntegerType t = (IntegerType)type;
if (! (node instanceof IntegerLiteralNode)) return false;
@ -201,7 +201,7 @@ class TypeChecker extends Visitor {
return t.isInDomain(n.value());
}
protected boolean checkLHS(ExprNode lhs) {
private boolean checkLHS(ExprNode lhs) {
if (lhs.isParameter()) {
// parameter is always assignable.
return true;
@ -319,7 +319,7 @@ class TypeChecker extends Visitor {
}
}
protected ExprNode integralPromotedExpr(ExprNode expr) {
private ExprNode integralPromotedExpr(ExprNode expr) {
Type t = integralPromotion(expr.type());
if (t.isSameType(expr.type())) {
return expr;
@ -331,7 +331,7 @@ class TypeChecker extends Visitor {
// +, -, *, /, %, &, |, ^, <<, >>
// #@@range/expectsSameInteger{
protected void expectsSameInteger(BinaryOpNode node) {
private void expectsSameInteger(BinaryOpNode node) {
if (! mustBeInteger(node.left(), node.operator())) return;
if (! mustBeInteger(node.right(), node.operator())) return;
arithmeticImplicitCast(node);
@ -339,7 +339,7 @@ class TypeChecker extends Visitor {
// #@@}
// ==, !=, >, >=, <, <=, &&, ||
protected void expectsComparableScalars(BinaryOpNode node) {
private void expectsComparableScalars(BinaryOpNode node) {
if (! mustBeScalar(node.left(), node.operator())) return;
if (! mustBeScalar(node.right(), node.operator())) return;
if (node.left().type().isPointer()) {
@ -358,7 +358,7 @@ class TypeChecker extends Visitor {
}
// cast slave node to master node.
protected ExprNode forcePointerType(ExprNode master, ExprNode slave) {
private ExprNode forcePointerType(ExprNode master, ExprNode slave) {
if (master.type().isCompatible(slave.type())) {
// needs no cast
return slave;
@ -372,7 +372,7 @@ class TypeChecker extends Visitor {
// Processes usual arithmetic conversion for binary operations.
// #@@range/arithmeticImplicitCast{
protected void arithmeticImplicitCast(BinaryOpNode node) {
private void arithmeticImplicitCast(BinaryOpNode node) {
Type r = integralPromotion(node.right().type());
Type l = integralPromotion(node.left().type());
Type target = usualArithmeticConversion(l, r);
@ -414,7 +414,7 @@ class TypeChecker extends Visitor {
return null;
}
protected void expectsScalarLHS(UnaryArithmeticOpNode node) {
private void expectsScalarLHS(UnaryArithmeticOpNode node) {
if (node.expr().isParameter()) {
// parameter is always a scalar.
}
@ -504,7 +504,7 @@ class TypeChecker extends Visitor {
// Utilities
//
protected boolean checkRHS(ExprNode rhs) {
private boolean checkRHS(ExprNode rhs) {
if (isInvalidRHSType(rhs.type())) {
error(rhs, "invalid RHS expression type: " + rhs.type());
return false;
@ -514,7 +514,7 @@ class TypeChecker extends Visitor {
// Processes forced-implicit-cast.
// Applied To: return expr, assignment RHS, funcall argument
protected ExprNode implicitCast(Type targetType, ExprNode expr) {
private ExprNode implicitCast(Type targetType, ExprNode expr) {
if (expr.type().isSameType(targetType)) {
return expr;
}
@ -534,7 +534,7 @@ class TypeChecker extends Visitor {
// Process integral promotion (integers only).
// #@@range/integralPromotion{
protected Type integralPromotion(Type t) {
private Type integralPromotion(Type t) {
if (!t.isInteger()) {
throw new Error("integralPromotion for " + t);
}
@ -551,7 +551,7 @@ class TypeChecker extends Visitor {
// Usual arithmetic conversion for ILP32 platform (integers only).
// Size of l, r >= sizeof(int).
// #@@range/usualArithmeticConversion{
protected Type usualArithmeticConversion(Type l, Type r) {
private Type usualArithmeticConversion(Type l, Type r) {
Type s_int = typeTable.signedInt();
Type u_int = typeTable.unsignedInt();
Type s_long = typeTable.signedLong();
@ -575,33 +575,33 @@ class TypeChecker extends Visitor {
}
// #@@}
protected boolean isInvalidStatementType(Type t) {
private boolean isInvalidStatementType(Type t) {
return t.isStruct() || t.isUnion();
}
protected boolean isInvalidReturnType(Type t) {
private boolean isInvalidReturnType(Type t) {
return t.isStruct() || t.isUnion() || t.isArray();
}
protected boolean isInvalidParameterType(Type t) {
private boolean isInvalidParameterType(Type t) {
return t.isStruct() || t.isUnion() || t.isVoid()
|| t.isIncompleteArray();
}
protected boolean isInvalidVariableType(Type t) {
private boolean isInvalidVariableType(Type t) {
return t.isVoid() || (t.isArray() && ! t.isAllocatedArray());
}
protected boolean isInvalidLHSType(Type t) {
private boolean isInvalidLHSType(Type t) {
// Array is OK if it is declared as a type of parameter.
return t.isStruct() || t.isUnion() || t.isVoid() || t.isArray();
}
protected boolean isInvalidRHSType(Type t) {
private boolean isInvalidRHSType(Type t) {
return t.isStruct() || t.isUnion() || t.isVoid();
}
protected boolean mustBeInteger(ExprNode expr, String op) {
private boolean mustBeInteger(ExprNode expr, String op) {
if (! expr.type().isInteger()) {
wrongTypeError(expr, op);
return false;
@ -609,7 +609,7 @@ class TypeChecker extends Visitor {
return true;
}
protected boolean mustBeScalar(ExprNode expr, String op) {
private boolean mustBeScalar(ExprNode expr, String op) {
if (! expr.type().isScalar()) {
wrongTypeError(expr, op);
return false;
@ -617,23 +617,23 @@ class TypeChecker extends Visitor {
return true;
}
protected void invalidCastError(Node n, Type l, Type r) {
private void invalidCastError(Node n, Type l, Type r) {
error(n, "invalid cast from " + l + " to " + r);
}
protected void wrongTypeError(ExprNode expr, String op) {
private void wrongTypeError(ExprNode expr, String op) {
error(expr, "wrong operand type for " + op + ": " + expr.type());
}
protected void warn(Node n, String msg) {
private void warn(Node n, String msg) {
errorHandler.warn(n.location(), msg);
}
protected void error(Node n, String msg) {
private void error(Node n, String msg) {
errorHandler.error(n.location(), msg);
}
protected void error(Location loc, String msg) {
private void error(Location loc, String msg) {
errorHandler.error(loc, msg);
}
}

View File

@ -9,17 +9,17 @@ import java.util.*;
public class TypeResolver extends Visitor
implements EntityVisitor<Void>, DeclarationVisitor<Void> {
// #@@range/ctor{
protected TypeTable typeTable;
protected ErrorHandler errorHandler;
private final TypeTable typeTable;
private final ErrorHandler errorHandler;
public TypeResolver(ErrorHandler errorHandler) {
public TypeResolver(TypeTable typeTable, ErrorHandler errorHandler) {
this.typeTable = typeTable;
this.errorHandler = errorHandler;
}
// #@@}
// #@@range/resolveProgram{
public void resolve(AST ast, TypeTable typeTable) {
this.typeTable = typeTable;
public void resolve(AST ast) {
defineTypes(ast.types());
// #@@range/resolveProgram_core{
for (TypeDefinition t : ast.types()) {
@ -139,7 +139,7 @@ public class TypeResolver extends Visitor
}
// #@@range/resolveFunctionHeader{
protected void resolveFunctionHeader(Function func) {
private void resolveFunctionHeader(Function func) {
bindType(func.typeNode());
for (Parameter param : func.parameters()) {
// arrays must be converted to pointers in a function parameter.
@ -195,7 +195,7 @@ public class TypeResolver extends Visitor
return null;
}
protected void error(Node node, String msg) {
private void error(Node node, String msg) {
errorHandler.error(node.location(), msg);
}
}

View File

@ -76,10 +76,10 @@ public class Parser {
// #@@}
// #@@range/instance_members{
protected String sourceName;
protected LibraryLoader loader;
protected ErrorHandler errorHandler;
protected Set<String> knownTypedefs;
private String sourceName;
private LibraryLoader loader;
private ErrorHandler errorHandler;
private Set<String> knownTypedefs;
// #@@}
// #@@range/ctor1{

View File

@ -14,9 +14,9 @@ public class CodeGenerator
IRVisitor<Void,Void>,
ELFConstants {
// #@@range/ctor{
private CodeGeneratorOptions options;
private Type naturalType;
private ErrorHandler errorHandler;
final CodeGeneratorOptions options;
final Type naturalType;
final ErrorHandler errorHandler;
public CodeGenerator(CodeGeneratorOptions options,
Type naturalType,

View File

@ -3,7 +3,7 @@ import net.loveruby.cflat.parser.Parser;
import java.io.*;
abstract public class TextUtils {
static final protected byte vtab = 013;
static final private byte vtab = 013;
static public String dumpString(String str) {
try {
@ -15,8 +15,7 @@ abstract public class TextUtils {
}
static public String dumpString(String string, String encoding)
throws UnsupportedEncodingException
{
throws UnsupportedEncodingException {
byte[] src = string.getBytes(encoding);
StringBuffer buf = new StringBuffer();
buf.append("\"");
@ -38,7 +37,7 @@ abstract public class TextUtils {
return buf.toString();
}
static protected int toUnsigned(byte b) {
static private int toUnsigned(byte b) {
return b >= 0 ? b : 256 + b;
}