Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Compiler {
// Mapping from a type name to a Type. The string name matches
// `type.str()` call. No TypeMemPtrs are in here, because Simple does not
// have C-style '*ptr' references.
public static HashMap<String, Type> TYPES = new HashMap<>();
private final HashMap<String, Type> types = new HashMap<>();

private ArrayList<Node> ctorStack = new ArrayList<>();

Expand All @@ -70,10 +70,9 @@ public Compiler(CodeGen codeGen, TypeInteger arg) {

public void parse() {
this.typeDictionary = createAST(_code._src);
Map<String, Type> types = new HashMap<>();
types.clear();
populateDefaultTypes(types);
populateTypes(types);
TYPES.putAll(types);
ZERO = con(TypeInteger.ZERO).keep();
NIL = con(Type.NIL).keep();
XCTRL= new XCtrlNode().peephole().keep();
Expand Down Expand Up @@ -269,7 +268,7 @@ private void defineScopedVars(Scope scope, ScopeNode scopeNode, FunNode fun) {
for (Symbol symbol: scope.getLocalSymbols()) {
if (symbol instanceof Symbol.VarSymbol varSymbol) {
varSymbol.regNumber = REGNUM++;
Type sonType = TYPES.get(varSymbol.type.name());
Type sonType = types.get(varSymbol.type.name());
if (sonType == null)
throw new CompilerException("Unknown SON Type "+varSymbol.type.name());
Node init = null;
Expand All @@ -290,7 +289,7 @@ private void generateFunction(Symbol.FunctionTypeSymbol functionTypeSymbol) {
ctrl(XCTRL);
_scope.mem(new MemMergeNode(false));

var funType = (TypeFunPtr) TYPES.get(functionTypeSymbol.name);
var funType = (TypeFunPtr) types.get(functionTypeSymbol.name);
if (funType == null) throw new CompilerException("Function " + functionTypeSymbol.name + " not found");

// Parse whole program, as-if function header "{ int arg -> body }"
Expand Down Expand Up @@ -612,11 +611,11 @@ private Node newStruct(TypeStruct obj, Node size) {
private Node compileNewExpr(AST.NewExpr newExpr) {
EZType type = newExpr.type;
if (type instanceof EZType.EZTypeArray typeArray) {
TypeMemPtr tarray = (TypeMemPtr) TYPES.get(typeArray.name());
TypeMemPtr tarray = (TypeMemPtr) types.get(typeArray.name());
return newArray(tarray._obj,newExpr.len==null?ZERO:compileExpr(newExpr.len));
}
else if (type instanceof EZType.EZTypeStruct typeStruct) {
TypeMemPtr tptr = (TypeMemPtr) TYPES.get(typeStruct.name());
TypeMemPtr tptr = (TypeMemPtr) types.get(typeStruct.name());
return newStruct(tptr._obj,con(tptr._obj.offset(tptr._obj._fields.length)));
}
else
Expand Down Expand Up @@ -703,7 +702,7 @@ else if (constantExpr.type instanceof EZType.EZTypeNull)

private Node compileSymbolExpr(AST.NameExpr symbolExpr) {
if (symbolExpr.type instanceof EZType.EZTypeFunction functionType)
return con(TYPES.get(functionType.name));
return con(types.get(functionType.name));
else {
Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) symbolExpr.symbol;
Var v = _scope.lookup(makeVarName(varSymbol));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.compilerprogramming.ezlang.compiler.type.*;


/**
* The tracked fields are now complex enough to deserve a array-of-structs layout
*/
Expand All @@ -27,10 +26,8 @@ public Var(int idx, String name, Type type, boolean xfinal, boolean fref) {
_fref = fref;
}
public Type type() {
if( !_type.isFRef() ) return _type;
// Update self to no longer use the forward ref type
Type def = Compiler.TYPES.get(((TypeMemPtr)_type)._obj._name);
return (_type=_type.meet(def));
assert !_type.isFRef();
return _type;
}

// Forward reference variables (not types) must be BOTTOM and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum Phase {
// ---------------------------
public CodeGen( String src ) { this(src, TypeInteger.BOT, 123L ); }
public CodeGen( String src, TypeInteger arg, long workListSeed ) {
Type.resetInternTable();
CODE = this;
_phase = null;
_callingConv = null;
Expand All @@ -51,6 +52,7 @@ public CodeGen( String src, TypeInteger arg, long workListSeed ) {
_src = src;
_arg = arg;
_iter = new IterPeeps(workListSeed);
_main = makeFun(TypeTuple.MAIN, Type.BOTTOM);
P = new Compiler(this,arg);
}

Expand Down Expand Up @@ -141,7 +143,7 @@ public TypeFunPtr makeFun2(TypeTuple sig, Type ret ) {
return new TypeFunPtr((byte)2,sig,ret, 1L<<fidx );
}
// Signature for MAIN
public TypeFunPtr _main = makeFun(TypeTuple.MAIN, Type.BOTTOM);
public TypeFunPtr _main;
// Reverse from a constant function pointer to the IR function being called
public FunNode link( TypeFunPtr tfp ) {
assert tfp.isConstant();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ public static Type[] gather() {
return ts.toArray(new Type[ts.size()]);
}

private static HashMap<Type, Type> INTERN_BASELINE;

/**
* Call this at the start of each front-end compilation
* run to reset the intern table to core types that the SoN backend
* needs. The reset drops any types added by a previous
* compile run.
*/
public static void resetInternTable() {
if( INTERN_BASELINE == null ) {
gather();
INTERN_BASELINE = new HashMap<>(INTERN);
} else {
INTERN.clear();
INTERN.putAll(INTERN_BASELINE);
}
}

// Is high or on the lattice centerline.
public boolean isHigh () { return _type==TTOP || _type==TXCTRL || _type==TXNIL; }
public boolean isHighOrConst() { return isHigh() || isConstant(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ func foo()->Int {
compileSrc(src);
}

@Test
public void testStructLayoutsDoNotLeakAcrossCompilations() {
compileSrc("""
struct Point { var x: Int }
func foo()->Int {
var p = new Point { x = 1 }
return p.x
}
""");
compileSrc("""
struct Point { var y: Int }
func foo()->Int {
var p = new Point { y = 2 }
return p.y
}
""");
}

@Ignore("Bug in backend")
@Test
public void testArrayLoadNullCheckNarrowsNullableElement() {
Expand Down
Loading