From 1881b277aa81bf6dab7fa4a02235e6e80bf41a5d Mon Sep 17 00:00:00 2001 From: dibyendumajumdar Date: Sun, 27 Jul 2025 17:42:00 +0100 Subject: [PATCH 1/9] Rename Type to EZType - and all sub types too so that it is easy to merge code from Simple --- .../ezlang/compiler/CompiledFunction.java | 56 ++++++------- .../ezlang/compiler/Compiler.java | 4 +- .../ezlang/compiler/Instruction.java | 18 ++-- .../ezlang/compiler/Operand.java | 12 +-- .../ezlang/compiler/Register.java | 8 +- .../ezlang/compiler/RegisterPool.java | 8 +- .../SparseConditionalConstantPropagation.java | 4 +- .../ezlang/interpreter/Interpreter.java | 6 +- .../ezlang/interpreter/Value.java | 10 +-- .../compiler/TestInterferenceGraph.java | 8 +- .../ezlang/compiler/TestLiveness.java | 4 +- .../ezlang/compiler/TestSSATransform.java | 7 +- .../ezlang/parser/AST.java | 4 +- .../ezlang/compiler/CompiledFunction.java | 44 +++++----- .../ezlang/compiler/Compiler.java | 4 +- .../ezlang/compiler/Instruction.java | 18 ++-- .../ezlang/compiler/Operand.java | 14 ++-- .../ezlang/interpreter/Interpreter.java | 6 +- .../ezlang/interpreter/Value.java | 10 +-- .../ezlang/compiler/Compiler.java | 50 +++++------ .../ezlang/semantic/SemaAssignTypes.java | 82 +++++++++---------- .../ezlang/semantic/SemaDefineTypes.java | 30 +++---- .../ezlang/compiler/CompiledFunction.java | 16 ++-- .../ezlang/compiler/Compiler.java | 4 +- .../ezlang/compiler/Instruction.java | 10 +-- .../ezlang/types/{Type.java => EZType.java} | 70 ++++++++-------- .../ezlang/types/Symbol.java | 16 ++-- .../ezlang/types/TypeDictionary.java | 60 +++++++------- .../ezlang/types/TestTypes.java | 13 +-- 29 files changed, 294 insertions(+), 302 deletions(-) rename types/src/main/java/com/compilerprogramming/ezlang/types/{Type.java => EZType.java} (74%) diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java index b1a2b45..d2c1d40 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java @@ -4,7 +4,7 @@ import com.compilerprogramming.ezlang.parser.AST; import com.compilerprogramming.ezlang.types.Scope; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import java.util.*; @@ -17,7 +17,7 @@ public class CompiledFunction { public BasicBlock currentBlock; private BasicBlock currentBreakTarget; private BasicBlock currentContinueTarget; - public Type.TypeFunction functionType; + public EZType.EZTypeFunction functionType; public final RegisterPool registerPool; private final TypeDictionary typeDictionary; @@ -39,7 +39,7 @@ public class CompiledFunction { public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary typeDictionary, EnumSet options) { AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl; - this.functionType = (Type.TypeFunction) functionSymbol.type; + this.functionType = (EZType.EZTypeFunction) functionSymbol.type; this.registerPool = new RegisterPool(); // Incremental SSA is an optional feature this.issa = (options != null && options.contains(Options.ISSA)) ? new IncrementalSSABraun(this) : new NoopIncrementalSSA(); @@ -60,8 +60,8 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol, TypeDictionary typeDictionary) { this(functionSymbol,typeDictionary,null); } - public CompiledFunction(Type.TypeFunction functionType, TypeDictionary typeDictionary) { - this.functionType = (Type.TypeFunction) functionType; + public CompiledFunction(EZType.EZTypeFunction functionType, TypeDictionary typeDictionary) { + this.functionType = (EZType.EZTypeFunction) functionType; this.registerPool = new RegisterPool(); this.issa = new NoopIncrementalSSA(); this.BID = 0; this.entry = this.currentBlock = createBlock(); @@ -325,7 +325,7 @@ private boolean compileExpr(AST.Expr expr) { private boolean compileCallExpr(AST.CallExpr callExpr) { compileExpr(callExpr.callee); var callee = pop(); - Type.TypeFunction calleeType = null; + EZType.EZTypeFunction calleeType = null; if (callee instanceof Operand.LocalFunctionOperand functionOperand) calleeType = functionOperand.functionType; else throw new CompilerException("Cannot call a non function type"); @@ -347,20 +347,20 @@ private boolean compileCallExpr(AST.CallExpr callExpr) { for (int i = 0; i < args.size(); i++) pop(); Operand.TempRegisterOperand ret = null; - if (callExpr.callee.type instanceof Type.TypeFunction tf && - !(tf.returnType instanceof Type.TypeVoid)) { + if (callExpr.callee.type instanceof EZType.EZTypeFunction tf && + !(tf.returnType instanceof EZType.EZTypeVoid)) { ret = createTemp(tf.returnType); } codeCall(returnStackPos, ret, calleeType, args.toArray(new Operand.RegisterOperand[args.size()])); return false; } - private Type.TypeStruct getStructType(Type t) { - if (t instanceof Type.TypeStruct typeStruct) { + private EZType.EZTypeStruct getStructType(EZType t) { + if (t instanceof EZType.EZTypeStruct typeStruct) { return typeStruct; } - else if (t instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeStruct typeStruct) { + else if (t instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeStruct typeStruct) { return typeStruct; } else @@ -368,7 +368,7 @@ else if (t instanceof Type.TypeNullable ptr && } private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) { - Type.TypeStruct typeStruct = getStructType(fieldExpr.object.type); + EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type); int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName); if (fieldIndex < 0) throw new CompilerException("Field " + fieldExpr.fieldName + " not found"); @@ -391,7 +391,7 @@ private boolean compileArrayIndexExpr(AST.ArrayLoadExpr arrayIndexExpr) { } private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) { - Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.object.type; + EZType.EZTypeStruct structType = (EZType.EZTypeStruct) setFieldExpr.object.type; int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName); if (fieldIndex == -1) throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name); @@ -441,7 +441,7 @@ private boolean compileInitExpr(AST.InitExpr initExpr) { } private boolean compileSymbolExpr(AST.NameExpr symbolExpr) { - if (symbolExpr.type instanceof Type.TypeFunction functionType) + if (symbolExpr.type instanceof EZType.EZTypeFunction functionType) pushOperand(new Operand.LocalFunctionOperand(functionType)); else { Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) symbolExpr.symbol; @@ -549,29 +549,29 @@ private boolean compileUnaryExpr(AST.UnaryExpr unaryExpr) { } private boolean compileConstantExpr(AST.LiteralExpr constantExpr) { - if (constantExpr.type instanceof Type.TypeInteger) + if (constantExpr.type instanceof EZType.EZTypeInteger) pushConstant(constantExpr.value.num.intValue(), constantExpr.type); - else if (constantExpr.type instanceof Type.TypeNull) + else if (constantExpr.type instanceof EZType.EZTypeNull) pushNullConstant(constantExpr.type); else throw new CompilerException("Invalid constant type"); return false; } - private void pushConstant(long value, Type type) { + private void pushConstant(long value, EZType type) { pushOperand(new Operand.ConstantOperand(value, type)); } - private void pushNullConstant(Type type) { + private void pushNullConstant(EZType type) { pushOperand(new Operand.NullConstantOperand(type)); } - private Operand.TempRegisterOperand createTemp(Type type) { + private Operand.TempRegisterOperand createTemp(EZType type) { var tempRegister = new Operand.TempRegisterOperand(registerPool.newTempReg(type)); pushOperand(tempRegister); return tempRegister; } - Type typeOfOperand(Operand operand) { + EZType typeOfOperand(Operand operand) { if (operand instanceof Operand.ConstantOperand constant) return constant.type; else if (operand instanceof Operand.NullConstantOperand nullConstantOperand) @@ -582,7 +582,7 @@ else if (operand instanceof Operand.RegisterOperand registerOperand) } private Operand.TempRegisterOperand createTempAndMove(Operand src) { - Type type = typeOfOperand(src); + EZType type = typeOfOperand(src); var temp = createTemp(type); codeMove(src, temp); return temp; @@ -644,16 +644,16 @@ else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand) codeMove(value, indexed); } - private void codeNew(Type type, AST.Expr len, AST.Expr initVal) { - if (type instanceof Type.TypeArray typeArray) + private void codeNew(EZType type, AST.Expr len, AST.Expr initVal) { + if (type instanceof EZType.EZTypeArray typeArray) codeNewArray(typeArray, len, initVal); - else if (type instanceof Type.TypeStruct typeStruct) + else if (type instanceof EZType.EZTypeStruct typeStruct) codeNewStruct(typeStruct); else throw new CompilerException("Unexpected type: " + type); } - private void codeNewArray(Type.TypeArray typeArray, AST.Expr len, AST.Expr initVal) { + private void codeNewArray(EZType.EZTypeArray typeArray, AST.Expr len, AST.Expr initVal) { var temp = createTemp(typeArray); Operand lenOperand = null; Operand initValOperand = null; @@ -685,7 +685,7 @@ private void codeNewArray(Type.TypeArray typeArray, AST.Expr len, AST.Expr initV code(insn); } - private void codeNewStruct(Type.TypeStruct typeStruct) { + private void codeNewStruct(EZType.EZTypeStruct typeStruct) { var temp = createTemp(typeStruct); var target = (Operand.RegisterOperand) issa.write(temp); var insn = new Instruction.NewStruct(typeStruct, target); @@ -729,7 +729,7 @@ private void codeCBR(BasicBlock block, Operand condition, BasicBlock trueBlock, private void codeCall(int newBase, Operand.RegisterOperand targetOperand, - Type.TypeFunction calleeType, + EZType.EZTypeFunction calleeType, Operand.RegisterOperand ...arguments) { if (targetOperand != null) targetOperand = (Operand.RegisterOperand) issa.write(targetOperand); diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index b15a157..7f33025 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -5,7 +5,7 @@ import com.compilerprogramming.ezlang.semantic.SemaAssignTypes; import com.compilerprogramming.ezlang.semantic.SemaDefineTypes; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import java.util.EnumSet; @@ -15,7 +15,7 @@ public class Compiler { private void compile(TypeDictionary typeDictionary, EnumSet options) { for (Symbol symbol: typeDictionary.getLocalSymbols()) { if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) { - Type.TypeFunction functionType = (Type.TypeFunction) functionSymbol.type; + EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type; var function = new CompiledFunction(functionSymbol, typeDictionary, options); if (options.contains(Options.DUMP_INITIAL_IR)) function.dumpIR(false, "Initial IR"); diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java index f7a3c6b..3ea0f44 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.exceptions.CompilerException; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import java.util.ArrayList; import java.util.Collections; @@ -139,16 +139,16 @@ public StringBuilder toStr(StringBuilder sb) { } public static class NewArray extends Instruction { - public final Type.TypeArray type; - public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand) { + public final EZType.EZTypeArray type; + public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand) { super(I_NEW_ARRAY, destOperand); this.type = type; } - public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand, Operand len) { + public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand, Operand len) { super(I_NEW_ARRAY, destOperand, len); this.type = type; } - public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand, Operand len, Operand initValue) { + public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand, Operand len, Operand initValue) { super(I_NEW_ARRAY, destOperand, len, initValue); this.type = type; } @@ -170,8 +170,8 @@ public StringBuilder toStr(StringBuilder sb) { } public static class NewStruct extends Instruction { - public final Type.TypeStruct type; - public NewStruct(Type.TypeStruct type, Operand.RegisterOperand destOperand) { + public final EZType.EZTypeStruct type; + public NewStruct(EZType.EZTypeStruct type, Operand.RegisterOperand destOperand) { super(I_NEW_STRUCT, destOperand); this.type = type; } @@ -324,9 +324,9 @@ public StringBuilder toStr(StringBuilder sb) { } public static class Call extends Instruction { - public final Type.TypeFunction callee; + public final EZType.EZTypeFunction callee; public final int newbase; - public Call(int newbase, Operand.RegisterOperand returnOperand, Type.TypeFunction callee, Operand.RegisterOperand... args) { + public Call(int newbase, Operand.RegisterOperand returnOperand, EZType.EZTypeFunction callee, Operand.RegisterOperand... args) { super(I_CALL, returnOperand, args); this.callee = callee; this.newbase = newbase; diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java index 8ff84e4..f6af988 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java @@ -1,15 +1,15 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; public class Operand { - Type type; + EZType type; public static class ConstantOperand extends Operand { public final long value; - public ConstantOperand(long value, Type type) { + public ConstantOperand(long value, EZType type) { this.value = value; this.type = type; } @@ -20,7 +20,7 @@ public String toString() { } public static class NullConstantOperand extends Operand { - public NullConstantOperand(Type type) { + public NullConstantOperand(EZType type) { this.type = type; } @Override @@ -61,8 +61,8 @@ public RegisterOperand copy(Register register) { } public static class LocalFunctionOperand extends Operand { - public final Type.TypeFunction functionType; - public LocalFunctionOperand(Type.TypeFunction functionType) { + public final EZType.EZTypeFunction functionType; + public LocalFunctionOperand(EZType.EZTypeFunction functionType) { this.functionType = functionType; } @Override diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Register.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Register.java index 9f87393..09190a1 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Register.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Register.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.compiler; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; /** * Represents a Virtual Register in the abstract machine. @@ -44,7 +44,7 @@ public class Register { /** * The type of the register */ - public final Type type; + public final EZType type; /** * The location of this register relative to the base * of the executing function. Multiple registers may share the same @@ -52,10 +52,10 @@ public class Register { */ protected int frameSlot; - public Register(int id, String name, Type type) { + public Register(int id, String name, EZType type) { this(id,name,type,id); // Initially frame slot is set to the unique ID } - protected Register(int id, String name, Type type, int frameSlot) { + protected Register(int id, String name, EZType type, int frameSlot) { this.id = id; this.name = name; this.type = type; diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java index 88469af..9a0a77a 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/RegisterPool.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.compiler; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import java.util.ArrayList; @@ -18,20 +18,20 @@ public class RegisterPool { public Register getReg(int regNumber) { return registers.get(regNumber); } - public Register newReg(String baseName, Type type) { + public Register newReg(String baseName, EZType type) { var id = registers.size(); var reg = new Register(id, baseName, type); registers.add(reg); return reg; } - public Register newTempReg(Type type) { + public Register newTempReg(EZType type) { var id = registers.size(); var name = "%t"+id; var reg = new Register(id, name, type); registers.add(reg); return reg; } - public Register newTempReg(String baseName, Type type) { + public Register newTempReg(String baseName, EZType type) { var id = registers.size(); var name = baseName+"_"+id; var reg = new Register(id, name, type); diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java index 36edb74..ba96971 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/compiler/SparseConditionalConstantPropagation.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.exceptions.CompilerException; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import java.util.*; @@ -361,7 +361,7 @@ private boolean evalInstruction(Instruction instruction) { } else throw new IllegalStateException(); } case Instruction.Call callInst -> { - if (!(callInst.callee.returnType instanceof Type.TypeVoid)) { + if (!(callInst.callee.returnType instanceof EZType.EZTypeVoid)) { var cell = valueLattice.get(callInst.returnOperand().reg); changed = cell.setKind(V_VARYING); } diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java b/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java index 654d2df..31912dc 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java @@ -7,7 +7,7 @@ import com.compilerprogramming.ezlang.exceptions.CompilerException; import com.compilerprogramming.ezlang.exceptions.InterpreterException; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; public class Interpreter { @@ -122,7 +122,7 @@ else if (arg instanceof Operand.NullConstantOperand) { Frame newFrame = new Frame(frame, baseReg, callInst.callee); interpret(execStack, newFrame); // Copy return value in expected location - if (!(callInst.callee.returnType instanceof Type.TypeVoid)) { + if (!(callInst.callee.returnType instanceof EZType.EZTypeVoid)) { execStack.stack[base + callInst.returnOperand().frameSlot()] = execStack.stack[baseReg]; } } @@ -302,7 +302,7 @@ public Frame(Symbol.FunctionTypeSymbol functionSymbol) { this.bytecodeFunction = (CompiledFunction) functionSymbol.code(); } - Frame(Frame caller, int base, Type.TypeFunction functionType) { + Frame(Frame caller, int base, EZType.EZTypeFunction functionType) { this.caller = caller; this.base = base; this.bytecodeFunction = (CompiledFunction) functionType.code; diff --git a/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java b/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java index 6717daa..fc46c3b 100644 --- a/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java +++ b/optvm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.interpreter; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import java.util.ArrayList; @@ -15,9 +15,9 @@ static public class NullValue extends Value { public NullValue() {} } static public class ArrayValue extends Value { - public final Type.TypeArray arrayType; + public final EZType.EZTypeArray arrayType; public final ArrayList values; - public ArrayValue(Type.TypeArray arrayType, long len, Value initValue) { + public ArrayValue(EZType.EZTypeArray arrayType, long len, Value initValue) { this.arrayType = arrayType; values = new ArrayList<>(); for (long i = 0; i < len; i++) { @@ -26,9 +26,9 @@ public ArrayValue(Type.TypeArray arrayType, long len, Value initValue) { } } static public class StructValue extends Value { - public final Type.TypeStruct structType; + public final EZType.EZTypeStruct structType; public final Value[] fields; - public StructValue(Type.TypeStruct structType) { + public StructValue(EZType.EZTypeStruct structType) { this.structType = structType; this.fields = new Value[structType.numFields()]; } diff --git a/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestInterferenceGraph.java b/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestInterferenceGraph.java index 8018cba..e20846f 100644 --- a/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestInterferenceGraph.java +++ b/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestInterferenceGraph.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import org.junit.Assert; import org.junit.Test; @@ -10,7 +10,7 @@ public class TestInterferenceGraph { private CompiledFunction buildTest1() { TypeDictionary typeDictionary = new TypeDictionary(); - Type.TypeFunction functionType = new Type.TypeFunction("foo"); + EZType.EZTypeFunction functionType = new EZType.EZTypeFunction("foo"); var argSymbol = new Symbol.ParameterSymbol("a", typeDictionary.INT); functionType.addArg(argSymbol); functionType.setReturnType(typeDictionary.INT); @@ -81,7 +81,7 @@ public void test1() { */ private CompiledFunction buildTest2() { TypeDictionary typeDictionary = new TypeDictionary(); - Type.TypeFunction functionType = new Type.TypeFunction("foo"); + EZType.EZTypeFunction functionType = new EZType.EZTypeFunction("foo"); functionType.setReturnType(typeDictionary.VOID); CompiledFunction function = new CompiledFunction(functionType, typeDictionary); RegisterPool regPool = function.registerPool; @@ -159,7 +159,7 @@ public void test3() { /* Test move does not interfere with uses */ public static CompiledFunction buildTest4() { TypeDictionary typeDictionary = new TypeDictionary(); - Type.TypeFunction functionType = new Type.TypeFunction("foo"); + EZType.EZTypeFunction functionType = new EZType.EZTypeFunction("foo"); functionType.setReturnType(typeDictionary.VOID); CompiledFunction function = new CompiledFunction(functionType, typeDictionary); RegisterPool regPool = function.registerPool; diff --git a/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestLiveness.java b/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestLiveness.java index 7a324b2..94c95e6 100644 --- a/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestLiveness.java +++ b/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestLiveness.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import org.junit.Assert; import org.junit.Test; @@ -245,7 +245,7 @@ func foo(a: Int,b: Int) /* page 448 Engineering a Compiler */ static CompiledFunction buildTest3() { TypeDictionary typeDictionary = new TypeDictionary(); - Type.TypeFunction functionType = new Type.TypeFunction("foo"); + EZType.EZTypeFunction functionType = new EZType.EZTypeFunction("foo"); functionType.setReturnType(typeDictionary.INT); CompiledFunction function = new CompiledFunction(functionType, typeDictionary); RegisterPool regPool = function.registerPool; diff --git a/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java b/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java index 8897512..5e926e3 100644 --- a/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java +++ b/optvm/src/test/java/com/compilerprogramming/ezlang/compiler/TestSSATransform.java @@ -1,10 +1,9 @@ package com.compilerprogramming.ezlang.compiler; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import java.util.Arrays; @@ -628,7 +627,7 @@ func bar(arg: Int)->Int { */ static CompiledFunction buildLostCopyTest() { TypeDictionary typeDictionary = new TypeDictionary(); - Type.TypeFunction functionType = new Type.TypeFunction("foo"); + EZType.EZTypeFunction functionType = new EZType.EZTypeFunction("foo"); var argSymbol = new Symbol.ParameterSymbol("p", typeDictionary.INT); functionType.addArg(argSymbol); functionType.setReturnType(typeDictionary.INT); @@ -699,7 +698,7 @@ public void testLostCopyProblem() { */ static CompiledFunction buildSwapTest() { TypeDictionary typeDictionary = new TypeDictionary(); - Type.TypeFunction functionType = new Type.TypeFunction("foo"); + EZType.EZTypeFunction functionType = new EZType.EZTypeFunction("foo"); var argSymbol = new Symbol.ParameterSymbol("p", typeDictionary.INT); functionType.addArg(argSymbol); functionType.setReturnType(typeDictionary.VOID); diff --git a/parser/src/main/java/com/compilerprogramming/ezlang/parser/AST.java b/parser/src/main/java/com/compilerprogramming/ezlang/parser/AST.java index 162dd29..8bd6020 100644 --- a/parser/src/main/java/com/compilerprogramming/ezlang/parser/AST.java +++ b/parser/src/main/java/com/compilerprogramming/ezlang/parser/AST.java @@ -3,7 +3,7 @@ import com.compilerprogramming.ezlang.lexer.Token; import com.compilerprogramming.ezlang.types.Scope; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import java.util.ArrayList; import java.util.List; @@ -162,7 +162,7 @@ public void accept(ASTVisitor visitor) { } public abstract static class Expr extends AST { - public Type type; + public EZType type; } public abstract static class TypeExpr extends Expr { diff --git a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java index 3071909..936ab2b 100644 --- a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java +++ b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java @@ -4,7 +4,7 @@ import com.compilerprogramming.ezlang.parser.AST; import com.compilerprogramming.ezlang.types.Scope; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import java.util.ArrayList; @@ -281,7 +281,7 @@ private boolean compileExpr(AST.Expr expr) { private boolean compileCallExpr(AST.CallExpr callExpr) { compileExpr(callExpr.callee); var callee = pop(); - Type.TypeFunction calleeType = null; + EZType.EZTypeFunction calleeType = null; if (callee instanceof Operand.LocalFunctionOperand functionOperand) calleeType = functionOperand.functionType; else throw new CompilerException("Cannot call a non function type"); @@ -303,8 +303,8 @@ private boolean compileCallExpr(AST.CallExpr callExpr) { for (int i = 0; i < args.size(); i++) pop(); Operand.TempRegisterOperand ret = null; - if (callExpr.callee.type instanceof Type.TypeFunction tf && - !(tf.returnType instanceof Type.TypeVoid)) { + if (callExpr.callee.type instanceof EZType.EZTypeFunction tf && + !(tf.returnType instanceof EZType.EZTypeVoid)) { ret = createTemp(tf.returnType); assert ret.regnum-maxLocalReg == returnStackPos; } @@ -312,12 +312,12 @@ private boolean compileCallExpr(AST.CallExpr callExpr) { return false; } - private Type.TypeStruct getStructType(Type t) { - if (t instanceof Type.TypeStruct typeStruct) { + private EZType.EZTypeStruct getStructType(EZType t) { + if (t instanceof EZType.EZTypeStruct typeStruct) { return typeStruct; } - else if (t instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeStruct typeStruct) { + else if (t instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeStruct typeStruct) { return typeStruct; } else @@ -325,7 +325,7 @@ else if (t instanceof Type.TypeNullable ptr && } private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) { - Type.TypeStruct typeStruct = getStructType(fieldExpr.object.type); + EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type); int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName); if (fieldIndex < 0) throw new CompilerException("Field " + fieldExpr.fieldName + " not found"); @@ -348,7 +348,7 @@ private boolean compileArrayIndexExpr(AST.ArrayLoadExpr arrayIndexExpr) { } private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) { - Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.object.type; + EZType.EZTypeStruct structType = (EZType.EZTypeStruct) setFieldExpr.object.type; int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName); if (fieldIndex == -1) throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name); @@ -398,7 +398,7 @@ private boolean compileInitExpr(AST.InitExpr initExpr) { } private boolean compileSymbolExpr(AST.NameExpr symbolExpr) { - if (symbolExpr.type instanceof Type.TypeFunction functionType) + if (symbolExpr.type instanceof EZType.EZTypeFunction functionType) pushOperand(new Operand.LocalFunctionOperand(functionType)); else { Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) symbolExpr.symbol; @@ -506,23 +506,23 @@ private boolean compileUnaryExpr(AST.UnaryExpr unaryExpr) { } private boolean compileConstantExpr(AST.LiteralExpr constantExpr) { - if (constantExpr.type instanceof Type.TypeInteger) + if (constantExpr.type instanceof EZType.EZTypeInteger) pushConstant(constantExpr.value.num.intValue(), constantExpr.type); - else if (constantExpr.type instanceof Type.TypeNull) + else if (constantExpr.type instanceof EZType.EZTypeNull) pushNullConstant(constantExpr.type); else throw new CompilerException("Invalid constant type"); return false; } - private void pushConstant(long value, Type type) { + private void pushConstant(long value, EZType type) { pushOperand(new Operand.ConstantOperand(value, type)); } - private void pushNullConstant(Type type) { + private void pushNullConstant(EZType type) { pushOperand(new Operand.NullConstantOperand(type)); } - private Operand.TempRegisterOperand createTemp(Type type) { + private Operand.TempRegisterOperand createTemp(EZType type) { var tempRegister = new Operand.TempRegisterOperand(virtualStack.size()+maxLocalReg, type); pushOperand(tempRegister); if (maxStackSize < virtualStack.size()) @@ -530,7 +530,7 @@ private Operand.TempRegisterOperand createTemp(Type type) { return tempRegister; } - Type typeOfOperand(Operand operand) { + EZType typeOfOperand(Operand operand) { if (operand instanceof Operand.ConstantOperand constant) return constant.type; else if (operand instanceof Operand.NullConstantOperand nullConstantOperand) @@ -541,7 +541,7 @@ else if (operand instanceof Operand.RegisterOperand registerOperand) } private Operand.TempRegisterOperand createTempAndMove(Operand src) { - Type type = typeOfOperand(src); + EZType type = typeOfOperand(src); var temp = createTemp(type); code(new Instruction.Move(src, temp)); return temp; @@ -603,10 +603,10 @@ else if (indexed instanceof Operand.LoadFieldOperand loadFieldOperand) code(new Instruction.Move(value, indexed)); } - private void codeNew(Type type, AST.Expr len, AST.Expr initVal) { - if (type instanceof Type.TypeArray typeArray) + private void codeNew(EZType type, AST.Expr len, AST.Expr initVal) { + if (type instanceof EZType.EZTypeArray typeArray) codeNewArray(typeArray, len, initVal); - else if (type instanceof Type.TypeStruct typeStruct) { + else if (type instanceof EZType.EZTypeStruct typeStruct) { var temp = createTemp(type); code(new Instruction.NewStruct(typeStruct, temp)); } @@ -614,7 +614,7 @@ else if (type instanceof Type.TypeStruct typeStruct) { throw new CompilerException("Unexpected type: " + type); } - private void codeNewArray(Type.TypeArray typeArray, AST.Expr len, AST.Expr initVal) { + private void codeNewArray(EZType.EZTypeArray typeArray, AST.Expr len, AST.Expr initVal) { var temp = createTemp(typeArray); Operand lenOperand = null; Operand initValOperand = null; diff --git a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index 37eaea0..d76569b 100644 --- a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -5,7 +5,7 @@ import com.compilerprogramming.ezlang.semantic.SemaAssignTypes; import com.compilerprogramming.ezlang.semantic.SemaDefineTypes; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import java.util.BitSet; @@ -15,7 +15,7 @@ public class Compiler { private void compile(TypeDictionary typeDictionary) { for (Symbol symbol: typeDictionary.getLocalSymbols()) { if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) { - Type.TypeFunction functionType = (Type.TypeFunction) functionSymbol.type; + EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type; functionType.code = new CompiledFunction(functionSymbol, typeDictionary); } } diff --git a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java index 73b5673..2386dbd 100644 --- a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java +++ b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.compiler; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; public abstract class Instruction { @@ -57,16 +57,16 @@ public StringBuilder toStr(StringBuilder sb) { } public static class NewArray extends Instruction { - public final Type.TypeArray type; - public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand) { + public final EZType.EZTypeArray type; + public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand) { super(I_NEW_ARRAY, destOperand); this.type = type; } - public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand, Operand len) { + public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand, Operand len) { super(I_NEW_ARRAY, destOperand, len); this.type = type; } - public NewArray(Type.TypeArray type, Operand.RegisterOperand destOperand, Operand len, Operand initValue) { + public NewArray(EZType.EZTypeArray type, Operand.RegisterOperand destOperand, Operand len, Operand initValue) { super(I_NEW_ARRAY, destOperand, len, initValue); this.type = type; } @@ -88,8 +88,8 @@ public StringBuilder toStr(StringBuilder sb) { } public static class NewStruct extends Instruction { - public final Type.TypeStruct type; - public NewStruct(Type.TypeStruct type, Operand.RegisterOperand destOperand) { + public final EZType.EZTypeStruct type; + public NewStruct(EZType.EZTypeStruct type, Operand.RegisterOperand destOperand) { super(I_NEW_STRUCT, destOperand); this.type = type; } @@ -244,9 +244,9 @@ public StringBuilder toStr(StringBuilder sb) { } public static class Call extends Instruction { - public final Type.TypeFunction callee; + public final EZType.EZTypeFunction callee; public final int newbase; - public Call(int newbase, Operand.RegisterOperand returnOperand, Type.TypeFunction callee, Operand.RegisterOperand... args) { + public Call(int newbase, Operand.RegisterOperand returnOperand, EZType.EZTypeFunction callee, Operand.RegisterOperand... args) { super(I_CALL, returnOperand, args); this.callee = callee; this.newbase = newbase; diff --git a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java index 54188da..d2315f3 100644 --- a/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java +++ b/registervm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java @@ -1,14 +1,14 @@ package com.compilerprogramming.ezlang.compiler; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; public class Operand { - Type type; + EZType type; public static class ConstantOperand extends Operand { public final long value; - public ConstantOperand(long value, Type type) { + public ConstantOperand(long value, EZType type) { this.value = value; this.type = type; } @@ -19,7 +19,7 @@ public String toString() { } public static class NullConstantOperand extends Operand { - public NullConstantOperand(Type type) { + public NullConstantOperand(EZType type) { this.type = type; } @Override @@ -49,8 +49,8 @@ public String toString() { } public static class LocalFunctionOperand extends Operand { - public final Type.TypeFunction functionType; - public LocalFunctionOperand(Type.TypeFunction functionType) { + public final EZType.EZTypeFunction functionType; + public LocalFunctionOperand(EZType.EZTypeFunction functionType) { this.functionType = functionType; } @Override @@ -65,7 +65,7 @@ public String toString() { * register number from start of temp area. */ public static class TempRegisterOperand extends RegisterOperand { - public TempRegisterOperand(int regnum, Type type) { + public TempRegisterOperand(int regnum, EZType type) { super(regnum); this.type = type; } diff --git a/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java b/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java index f441455..62f7411 100644 --- a/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java +++ b/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Interpreter.java @@ -7,7 +7,7 @@ import com.compilerprogramming.ezlang.exceptions.CompilerException; import com.compilerprogramming.ezlang.exceptions.InterpreterException; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; public class Interpreter { @@ -122,7 +122,7 @@ else if (arg instanceof Operand.NullConstantOperand) { Frame newFrame = new Frame(frame, baseReg, callInst.callee); interpret(execStack, newFrame); // Copy return value in expected location - if (!(callInst.callee.returnType instanceof Type.TypeVoid)) { + if (!(callInst.callee.returnType instanceof EZType.EZTypeVoid)) { execStack.stack[base + callInst.returnOperand().frameSlot()] = execStack.stack[baseReg]; } } @@ -301,7 +301,7 @@ public Frame(Symbol.FunctionTypeSymbol functionSymbol) { this.bytecodeFunction = (CompiledFunction) functionSymbol.code(); } - Frame(Frame caller, int base, Type.TypeFunction functionType) { + Frame(Frame caller, int base, EZType.EZTypeFunction functionType) { this.caller = caller; this.base = base; this.bytecodeFunction = (CompiledFunction) functionType.code; diff --git a/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java b/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java index 6717daa..fc46c3b 100644 --- a/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java +++ b/registervm/src/main/java/com/compilerprogramming/ezlang/interpreter/Value.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.interpreter; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import java.util.ArrayList; @@ -15,9 +15,9 @@ static public class NullValue extends Value { public NullValue() {} } static public class ArrayValue extends Value { - public final Type.TypeArray arrayType; + public final EZType.EZTypeArray arrayType; public final ArrayList values; - public ArrayValue(Type.TypeArray arrayType, long len, Value initValue) { + public ArrayValue(EZType.EZTypeArray arrayType, long len, Value initValue) { this.arrayType = arrayType; values = new ArrayList<>(); for (long i = 0; i < len; i++) { @@ -26,9 +26,9 @@ public ArrayValue(Type.TypeArray arrayType, long len, Value initValue) { } } static public class StructValue extends Value { - public final Type.TypeStruct structType; + public final EZType.EZTypeStruct structType; public final Value[] fields; - public StructValue(Type.TypeStruct structType) { + public StructValue(EZType.EZTypeStruct structType) { this.structType = structType; this.fields = new Value[structType.numFields()]; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index 3d7e546..813b708 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -12,7 +12,7 @@ import com.compilerprogramming.ezlang.semantic.SemaDefineTypes; import com.compilerprogramming.ezlang.types.Scope; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import java.util.ArrayList; @@ -105,10 +105,10 @@ private void populateTypes(Map structTypes) { // First process struct types for (var symbol: typeDictionary.getLocalSymbols()) { if (symbol instanceof Symbol.TypeSymbol typeSymbol) { - if (typeSymbol.type instanceof Type.TypeStruct typeStruct) { + if (typeSymbol.type instanceof EZType.EZTypeStruct typeStruct) { createSONStructType(structTypes,typeSymbol.name, typeStruct); } - else if (typeSymbol.type instanceof Type.TypeArray typeArray) { + else if (typeSymbol.type instanceof EZType.EZTypeArray typeArray) { getSONType(structTypes, typeArray); } } @@ -130,7 +130,7 @@ private void processFunctions() { } private void createFunctionType(Map structTypes, Symbol.FunctionTypeSymbol functionSymbol) { - Type.TypeFunction functionType = (Type.TypeFunction) functionSymbol.type; + EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type; Ary params = new Ary<>(SONType.class); for (var symbol: functionType.args) { if (symbol instanceof Symbol.ParameterSymbol parameterSymbol) { @@ -143,11 +143,11 @@ private void createFunctionType(Map structTypes, Symbol.Functio structTypes.put(functionSymbol.name, tfp); } - private void createSONStructType(Map structTypes, String typeName, Type.TypeStruct typeStruct) { + private void createSONStructType(Map structTypes, String typeName, EZType.EZTypeStruct typeStruct) { Ary fs = new Ary<>(Field.class); for (int i = 0; i < typeStruct.numFields(); i++) { String name = typeStruct.getFieldName(i); - Type type = typeStruct.getField(name); // FIXME + EZType type = typeStruct.getField(name); // FIXME fs.push(new Field(name,getSONType(structTypes,type),_code.getALIAS(),false)); } // A Struct type may have been created before because of @@ -169,33 +169,33 @@ private void createSONStructType(Map structTypes, String typeNa } } - private String getSONTypeName(Type type) { - if (type instanceof Type.TypeFunction typeFunction) { + private String getSONTypeName(EZType type) { + if (type instanceof EZType.EZTypeFunction typeFunction) { return typeFunction.name; } - else if (type instanceof Type.TypeArray typeArray) { + else if (type instanceof EZType.EZTypeArray typeArray) { return "*[" + getSONTypeName(typeArray.getElementType()) + "]"; } - else if (type instanceof Type.TypeStruct typeStruct) { + else if (type instanceof EZType.EZTypeStruct typeStruct) { return "*" + typeStruct.name; } - else if (type instanceof Type.TypeInteger || - type instanceof Type.TypeVoid) { + else if (type instanceof EZType.EZTypeInteger || + type instanceof EZType.EZTypeVoid) { return SONTypeInteger.BOT.str(); } - else if (type instanceof Type.TypeNull) { + else if (type instanceof EZType.EZTypeNull) { return SONTypeNil.NIL.str(); } - else if (type instanceof Type.TypeNullable typeNullable) { + else if (type instanceof EZType.EZTypeNullable typeNullable) { return getSONTypeName(typeNullable.baseType)+"?"; } else throw new CompilerException("Not yet implemented " + type.name()); } - private SONType getSONType(Map structTypes, Type type) { + private SONType getSONType(Map structTypes, EZType type) { SONType t = structTypes.get(type.name()); if (t != null) return t; - if (type instanceof Type.TypeStruct) { + if (type instanceof EZType.EZTypeStruct) { // For struct types in EeZee language a reference // to T means *T in SoN // Create SON struct type @@ -206,7 +206,7 @@ private SONType getSONType(Map structTypes, Type type) { structTypes.put(type.name(), ptr); return ptr; } - else if (type instanceof Type.TypeArray typeArray) { + else if (type instanceof EZType.EZTypeArray typeArray) { // A reference to array in EeZee means // *array in SoN SONType elementType = getSONType(structTypes,typeArray.getElementType()); @@ -215,7 +215,7 @@ else if (type instanceof Type.TypeArray typeArray) { structTypes.put(typeArray.name(), ptr); // Array type name is not same as ptr str() return ptr; } - else if (type instanceof Type.TypeNullable typeNullable) { + else if (type instanceof EZType.EZTypeNullable typeNullable) { SONType baseType = getSONType(structTypes,typeNullable.baseType); SONTypeMemPtr ptr = null; if (baseType instanceof SONTypeMemPtr ptr1) { @@ -229,7 +229,7 @@ else if (type instanceof Type.TypeNullable typeNullable) { structTypes.put(typeNullable.name(), ptr); return ptr; } - else if (type instanceof Type.TypeVoid) { + else if (type instanceof EZType.EZTypeVoid) { return structTypes.get("Int"); // Only allowed in return types } throw new CompilerException("Count not find type " + type.name()); @@ -551,12 +551,12 @@ private Node newStruct( SONTypeStruct obj, Node size) { } private Node compileNewExpr(AST.NewExpr newExpr) { - Type type = newExpr.type; - if (type instanceof Type.TypeArray typeArray) { + EZType type = newExpr.type; + if (type instanceof EZType.EZTypeArray typeArray) { SONTypeMemPtr tarray = (SONTypeMemPtr) TYPES.get(typeArray.name()); return newArray(tarray._obj,newExpr.len==null?ZERO:compileExpr(newExpr.len)); } - else if (type instanceof Type.TypeStruct typeStruct) { + else if (type instanceof EZType.EZTypeStruct typeStruct) { SONTypeMemPtr tptr = (SONTypeMemPtr) TYPES.get(typeStruct.name()); return newStruct(tptr._obj,con(tptr._obj.offset(tptr._obj._fields.length))); } @@ -625,15 +625,15 @@ private Node compileBinaryExpr(AST.BinaryExpr binaryExpr) { } private Node compileConstantExpr(AST.LiteralExpr constantExpr) { - if (constantExpr.type instanceof Type.TypeInteger) + if (constantExpr.type instanceof EZType.EZTypeInteger) return con(constantExpr.value.num.intValue()); - else if (constantExpr.type instanceof Type.TypeNull) + else if (constantExpr.type instanceof EZType.EZTypeNull) return NIL; else throw new CompilerException("Invalid constant type"); } private Node compileSymbolExpr(AST.NameExpr symbolExpr) { - if (symbolExpr.type instanceof Type.TypeFunction functionType) + if (symbolExpr.type instanceof EZType.EZTypeFunction functionType) return con(TYPES.get(functionType.name)); else { Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) symbolExpr.symbol; diff --git a/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaAssignTypes.java b/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaAssignTypes.java index 743ac68..77d3066 100644 --- a/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaAssignTypes.java +++ b/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaAssignTypes.java @@ -5,7 +5,7 @@ import com.compilerprogramming.ezlang.parser.AST; import com.compilerprogramming.ezlang.parser.ASTVisitor; import com.compilerprogramming.ezlang.types.Scope; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; /** @@ -71,17 +71,17 @@ public ASTVisitor visit(AST.BinaryExpr binaryExpr, boolean enter) { return this; validType(binaryExpr.expr1.type, true); validType(binaryExpr.expr2.type, true); - if (binaryExpr.expr1.type instanceof Type.TypeInteger && - binaryExpr.expr2.type instanceof Type.TypeInteger) { + if (binaryExpr.expr1.type instanceof EZType.EZTypeInteger && + binaryExpr.expr2.type instanceof EZType.EZTypeInteger) { // booleans are int too binaryExpr.type = typeDictionary.INT; } - else if (((binaryExpr.expr1.type instanceof Type.TypeNull && - binaryExpr.expr2.type instanceof Type.TypeNullable) || - (binaryExpr.expr1.type instanceof Type.TypeNullable && - binaryExpr.expr2.type instanceof Type.TypeNull) || - (binaryExpr.expr1.type instanceof Type.TypeNull && - binaryExpr.expr2.type instanceof Type.TypeNull)) && + else if (((binaryExpr.expr1.type instanceof EZType.EZTypeNull && + binaryExpr.expr2.type instanceof EZType.EZTypeNullable) || + (binaryExpr.expr1.type instanceof EZType.EZTypeNullable && + binaryExpr.expr2.type instanceof EZType.EZTypeNull) || + (binaryExpr.expr1.type instanceof EZType.EZTypeNull && + binaryExpr.expr2.type instanceof EZType.EZTypeNull)) && (binaryExpr.op.str.equals("==") || binaryExpr.op.str.equals("!="))) { binaryExpr.type = typeDictionary.INT; } @@ -98,7 +98,7 @@ public ASTVisitor visit(AST.UnaryExpr unaryExpr, boolean enter) { return this; } validType(unaryExpr.expr.type, false); - if (unaryExpr.expr.type instanceof Type.TypeInteger ti) { + if (unaryExpr.expr.type instanceof EZType.EZTypeInteger ti) { unaryExpr.type = unaryExpr.expr.type; } else { @@ -114,12 +114,12 @@ public ASTVisitor visit(AST.GetFieldExpr fieldExpr, boolean enter) { if (fieldExpr.type != null) return this; validType(fieldExpr.object.type, false); - Type.TypeStruct structType = null; - if (fieldExpr.object.type instanceof Type.TypeStruct ts) { + EZType.EZTypeStruct structType = null; + if (fieldExpr.object.type instanceof EZType.EZTypeStruct ts) { structType = ts; } - else if (fieldExpr.object.type instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeStruct ts) { + else if (fieldExpr.object.type instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeStruct ts) { structType = ts; } else @@ -138,15 +138,15 @@ public ASTVisitor visit(AST.SetFieldExpr fieldExpr, boolean enter) { if (fieldExpr.type != null) return this; validType(fieldExpr.object.type, true); - Type.TypeStruct structType = null; - if (fieldExpr.object.type instanceof Type.TypeStruct ts) { + EZType.EZTypeStruct structType = null; + if (fieldExpr.object.type instanceof EZType.EZTypeStruct ts) { structType = ts; } - else if (fieldExpr.object.type instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeStruct ts) { + else if (fieldExpr.object.type instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeStruct ts) { structType = ts; } - else if (fieldExpr.object.type instanceof Type.TypeArray typeArray) { + else if (fieldExpr.object.type instanceof EZType.EZTypeArray typeArray) { if (fieldExpr.fieldName.equals("len")) checkAssignmentCompatible(typeDictionary.INT,fieldExpr.value.type); else if (fieldExpr.fieldName.equals("value")) @@ -174,7 +174,7 @@ public ASTVisitor visit(AST.CallExpr callExpr, boolean enter) { if (callExpr.type != null) return this; validType(callExpr.callee.type, false); - if (callExpr.callee.type instanceof Type.TypeFunction f) { + if (callExpr.callee.type instanceof EZType.EZTypeFunction f) { callExpr.type = f.returnType; } else @@ -232,17 +232,17 @@ public ASTVisitor visit(AST.ArrayLoadExpr arrayIndexExpr, boolean enter) { if (arrayIndexExpr.type != null) return this; validType(arrayIndexExpr.array.type, false); - Type.TypeArray arrayType = null; - if (arrayIndexExpr.array.type instanceof Type.TypeArray ta) { + EZType.EZTypeArray arrayType = null; + if (arrayIndexExpr.array.type instanceof EZType.EZTypeArray ta) { arrayType = ta; } - else if (arrayIndexExpr.array.type instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeArray ta) { + else if (arrayIndexExpr.array.type instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeArray ta) { arrayType = ta; } else throw new CompilerException("Unexpected array type " + arrayIndexExpr.array.type); - if (!(arrayIndexExpr.expr.type instanceof Type.TypeInteger)) + if (!(arrayIndexExpr.expr.type instanceof EZType.EZTypeInteger)) throw new CompilerException("Array index must be integer type"); arrayIndexExpr.type = arrayType.getElementType(); validType(arrayIndexExpr.type, false); @@ -256,17 +256,17 @@ public ASTVisitor visit(AST.ArrayStoreExpr arrayIndexExpr, boolean enter) { if (arrayIndexExpr.type != null) return this; validType(arrayIndexExpr.array.type, false); - Type.TypeArray arrayType = null; - if (arrayIndexExpr.array.type instanceof Type.TypeArray ta) { + EZType.EZTypeArray arrayType = null; + if (arrayIndexExpr.array.type instanceof EZType.EZTypeArray ta) { arrayType = ta; } - else if (arrayIndexExpr.array.type instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeArray ta) { + else if (arrayIndexExpr.array.type instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeArray ta) { arrayType = ta; } else throw new CompilerException("Unexpected array type " + arrayIndexExpr.array.type); - if (!(arrayIndexExpr.expr.type instanceof Type.TypeInteger)) + if (!(arrayIndexExpr.expr.type instanceof EZType.EZTypeInteger)) throw new CompilerException("Array index must be integer type"); arrayIndexExpr.type = arrayType.getElementType(); validType(arrayIndexExpr.type, false); @@ -285,15 +285,15 @@ public ASTVisitor visit(AST.NewExpr newExpr, boolean enter) { if (newExpr.typeExpr.type == null) throw new CompilerException("Unresolved type in new expression"); validType(newExpr.typeExpr.type, false); - if (newExpr.typeExpr.type instanceof Type.TypeNullable) + if (newExpr.typeExpr.type instanceof EZType.EZTypeNullable) throw new CompilerException("new cannot be used to create a Nullable type"); - if (newExpr.typeExpr.type instanceof Type.TypeStruct typeStruct) { + if (newExpr.typeExpr.type instanceof EZType.EZTypeStruct typeStruct) { newExpr.type = newExpr.typeExpr.type; } - else if (newExpr.typeExpr.type instanceof Type.TypeArray arrayType) { + else if (newExpr.typeExpr.type instanceof EZType.EZTypeArray arrayType) { newExpr.type = newExpr.typeExpr.type; if (newExpr.len != null) { - if (!(newExpr.len.type instanceof Type.TypeInteger)) + if (!(newExpr.len.type instanceof EZType.EZTypeInteger)) throw new CompilerException("Array len must be integer type"); if (newExpr.initValue != null) { if (!arrayType.getElementType().isAssignable(newExpr.initValue.type)) @@ -315,9 +315,9 @@ public ASTVisitor visit(AST.InitExpr initExpr, boolean enter) { if (initExpr.type != null) return this; validType(initExpr.newExpr.type, false); - if (initExpr.newExpr.type instanceof Type.TypeNullable) + if (initExpr.newExpr.type instanceof EZType.EZTypeNullable) throw new CompilerException("new cannot be used to create a Nullable type"); - if (initExpr.newExpr.type instanceof Type.TypeStruct typeStruct) { + if (initExpr.newExpr.type instanceof EZType.EZTypeStruct typeStruct) { for (AST.Expr expr: initExpr.initExprList) { if (expr instanceof AST.SetFieldExpr setFieldExpr) { var fieldType = typeStruct.getField(setFieldExpr.fieldName); @@ -325,7 +325,7 @@ public ASTVisitor visit(AST.InitExpr initExpr, boolean enter) { } } } - else if (initExpr.newExpr.type instanceof Type.TypeArray arrayType) { + else if (initExpr.newExpr.type instanceof EZType.EZTypeArray arrayType) { if (initExpr.initExprList.size() > 0) initExpr.initExprList.removeIf(e->e instanceof AST.InitFieldExpr); for (AST.Expr expr: initExpr.initExprList) { @@ -368,12 +368,12 @@ public ASTVisitor visit(AST.ContinueStmt continueStmt, boolean enter) { public ASTVisitor visit(AST.ReturnStmt returnStmt, boolean enter) { if (enter) return this; - Type.TypeFunction functionType = (Type.TypeFunction) currentFuncDecl.symbol.type; + EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) currentFuncDecl.symbol.type; if (returnStmt.expr != null) { validType(returnStmt.expr.type, true); checkAssignmentCompatible(functionType.returnType, returnStmt.expr.type); } - else if (!(functionType.returnType instanceof Type.TypeVoid)) { + else if (!(functionType.returnType instanceof EZType.EZTypeVoid)) { throw new CompilerException("A return value of type " + functionType.returnType + " is expected"); } return this; @@ -436,7 +436,7 @@ public void analyze(AST.Program program) { program.accept(this); } - private void validType(Type t, boolean allowNull) { + private void validType(EZType t, boolean allowNull) { if (t == null) throw new CompilerException("Undefined type"); if (t == typeDictionary.UNKNOWN) @@ -445,7 +445,7 @@ private void validType(Type t, boolean allowNull) { throw new CompilerException("Null type not allowed"); } - private void checkAssignmentCompatible(Type var, Type value) { + private void checkAssignmentCompatible(EZType var, EZType value) { if (!var.isAssignable(value)) throw new CompilerException("Value of type " + value + " cannot be assigned to type " + var); } diff --git a/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaDefineTypes.java b/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaDefineTypes.java index e5d3498..3610660 100644 --- a/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaDefineTypes.java +++ b/semantic/src/main/java/com/compilerprogramming/ezlang/semantic/SemaDefineTypes.java @@ -5,7 +5,7 @@ import com.compilerprogramming.ezlang.parser.ASTVisitor; import com.compilerprogramming.ezlang.types.Scope; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; /** @@ -46,7 +46,7 @@ public ASTVisitor visit(AST.FuncDecl funcDecl, boolean enter) { // Install a symbol for the function, // type is not fully formed at this stage // as parameters nad return values must be added - Symbol funcSymbol = new Symbol.FunctionTypeSymbol(funcDecl.name, new Type.TypeFunction(funcDecl.name), funcDecl); + Symbol funcSymbol = new Symbol.FunctionTypeSymbol(funcDecl.name, new EZType.EZTypeFunction(funcDecl.name), funcDecl); typeDictionary.install(funcDecl.name, funcSymbol); // Set up the function decl so that when we visit the parameters // and return type we know where to add @@ -65,7 +65,7 @@ public ASTVisitor visit(AST.StructDecl structDecl, boolean enter) { if (enter) { Symbol structSymbol = typeDictionary.lookup(structDecl.name); if (structSymbol != null) { - if (structSymbol.type instanceof Type.TypeStruct lookupStructType) { + if (structSymbol.type instanceof EZType.EZTypeStruct lookupStructType) { if (!lookupStructType.pending) throw new CompilerException("Struct type " + structDecl.name + " is already declared"); } @@ -73,7 +73,7 @@ public ASTVisitor visit(AST.StructDecl structDecl, boolean enter) { throw new CompilerException("Symbol " + structDecl.name + " is already declared"); } else { - Type.TypeStruct structType = new Type.TypeStruct(structDecl.name); + EZType.EZTypeStruct structType = new EZType.EZTypeStruct(structDecl.name); structSymbol = new Symbol.TypeSymbol(structDecl.name, structType); typeDictionary.install(structDecl.name, structSymbol); } @@ -107,7 +107,7 @@ else if (varDecl.varType == AST.VarType.FUNCTION_PARAMETER && currentFuncDecl != else { if (varDecl.varType == AST.VarType.STRUCT_FIELD && currentStructDecl != null) { - Type.TypeStruct type = (Type.TypeStruct) currentStructDecl.symbol.type; + EZType.EZTypeStruct type = (EZType.EZTypeStruct) currentStructDecl.symbol.type; type.addField(varDecl.name, varDecl.typeExpr.type); } else if (varDecl.varType == AST.VarType.FUNCTION_PARAMETER @@ -115,7 +115,7 @@ else if (varDecl.varType == AST.VarType.FUNCTION_PARAMETER && currentScope == currentFuncDecl.scope) { if (currentScope.localLookup(varDecl.name) != null) throw new CompilerException("Parameter " + varDecl.name + " is already declared"); - Type.TypeFunction type = (Type.TypeFunction) currentFuncDecl.symbol.type; + EZType.EZTypeFunction type = (EZType.EZTypeFunction) currentFuncDecl.symbol.type; varDecl.symbol = currentScope.install(varDecl.name, new Symbol.ParameterSymbol(varDecl.name, varDecl.typeExpr.type)); type.addArg(varDecl.symbol); } @@ -153,26 +153,26 @@ public ASTVisitor visit(AST.CallExpr callExpr, boolean enter) { return this; } - Type getNullableSimpleType(AST.NullableSimpleTypeExpr simpleTypeExpr) { + EZType getNullableSimpleType(AST.NullableSimpleTypeExpr simpleTypeExpr) { String baseTypeName = simpleTypeExpr.baseTypeName(); Symbol typeSymbol = typeDictionary.lookup(baseTypeName); - Type baseType; + EZType baseType; if (typeSymbol == null) - baseType = typeDictionary.intern(new Type.TypeStruct(baseTypeName)); + baseType = typeDictionary.intern(new EZType.EZTypeStruct(baseTypeName)); else baseType = typeSymbol.type; if (baseType.isPrimitive()) throw new CompilerException("Cannot make Nullable instance of primitive type"); - return typeDictionary.intern(new Type.TypeNullable(baseType)); + return typeDictionary.intern(new EZType.EZTypeNullable(baseType)); } - Type getSimpleType(AST.SimpleTypeExpr simpleTypeExpr) { + EZType getSimpleType(AST.SimpleTypeExpr simpleTypeExpr) { if (simpleTypeExpr instanceof AST.NullableSimpleTypeExpr nullableSimpleTypeExpr) return getNullableSimpleType(nullableSimpleTypeExpr); String typeName = simpleTypeExpr.name(); Symbol typeSymbol = typeDictionary.lookup(typeName); if (typeSymbol == null) { - return typeDictionary.intern(new Type.TypeStruct(typeName)); + return typeDictionary.intern(new EZType.EZTypeStruct(typeName)); } return typeSymbol.type; } @@ -193,7 +193,7 @@ public ASTVisitor visit(AST.NullableSimpleTypeExpr simpleTypeExpr, boolean enter return this; } - Type getArrayType(AST.ArrayTypeExpr arrayTypeExpr) { + EZType getArrayType(AST.ArrayTypeExpr arrayTypeExpr) { if (arrayTypeExpr instanceof AST.NullableArrayTypeExpr nullableArrayTypeExpr) return getNullableArrayType(nullableArrayTypeExpr); var elemTypeExpr = arrayTypeExpr.elementType; @@ -201,7 +201,7 @@ Type getArrayType(AST.ArrayTypeExpr arrayTypeExpr) { return typeDictionary.makeArrayType(elemType, false); } - Type getNullableArrayType(AST.NullableArrayTypeExpr arrayTypeExpr) { + EZType getNullableArrayType(AST.NullableArrayTypeExpr arrayTypeExpr) { var elemTypeExpr = arrayTypeExpr.elementType; var elemType = getSimpleType(elemTypeExpr); return typeDictionary.makeArrayType(elemType, true); @@ -229,7 +229,7 @@ public ASTVisitor visit(AST.ReturnTypeExpr returnTypeExpr, boolean enter) { // We override the visitor and visit the return type here because // we need to associate the return type to the function's return type // The visitor mechanism doesn't allow us to associate values between two steps - Type.TypeFunction type = (Type.TypeFunction) currentFuncDecl.symbol.type; + EZType.EZTypeFunction type = (EZType.EZTypeFunction) currentFuncDecl.symbol.type; if (returnTypeExpr.returnType != null) { returnTypeExpr.returnType.accept(this); returnTypeExpr.type = returnTypeExpr.returnType.type; diff --git a/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java b/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java index 4d384c1..d19f547 100644 --- a/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java +++ b/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java @@ -4,7 +4,7 @@ import com.compilerprogramming.ezlang.parser.AST; import com.compilerprogramming.ezlang.types.Scope; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; public class CompiledFunction { @@ -258,12 +258,12 @@ private boolean compileCallExpr(AST.CallExpr callExpr) { return false; } - private Type.TypeStruct getStructType(Type t) { - if (t instanceof Type.TypeStruct typeStruct) { + private EZType.EZTypeStruct getStructType(EZType t) { + if (t instanceof EZType.EZTypeStruct typeStruct) { return typeStruct; } - else if (t instanceof Type.TypeNullable ptr && - ptr.baseType instanceof Type.TypeStruct typeStruct) { + else if (t instanceof EZType.EZTypeNullable ptr && + ptr.baseType instanceof EZType.EZTypeStruct typeStruct) { return typeStruct; } else @@ -271,7 +271,7 @@ else if (t instanceof Type.TypeNullable ptr && } private boolean compileFieldExpr(AST.GetFieldExpr fieldExpr) { - Type.TypeStruct typeStruct = getStructType(fieldExpr.object.type); + EZType.EZTypeStruct typeStruct = getStructType(fieldExpr.object.type); int fieldIndex = typeStruct.getFieldIndex(fieldExpr.fieldName); if (fieldIndex < 0) throw new CompilerException("Field " + fieldExpr.fieldName + " not found"); @@ -291,7 +291,7 @@ private boolean compileArrayIndexExpr(AST.ArrayLoadExpr arrayIndexExpr) { } private boolean compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) { - Type.TypeStruct structType = (Type.TypeStruct) setFieldExpr.object.type; + EZType.EZTypeStruct structType = (EZType.EZTypeStruct) setFieldExpr.object.type; int fieldIndex = structType.getFieldIndex(setFieldExpr.fieldName); if (fieldIndex == -1) throw new CompilerException("Field " + setFieldExpr.fieldName + " not found in struct " + structType.name); @@ -334,7 +334,7 @@ private boolean compileInitExpr(AST.InitExpr initExpr) { } private boolean compileSymbolExpr(AST.NameExpr symbolExpr) { - if (symbolExpr.type instanceof Type.TypeFunction functionType) + if (symbolExpr.type instanceof EZType.EZTypeFunction functionType) code(new Instruction.LoadFunction(functionType)); else { Symbol.VarSymbol varSymbol = (Symbol.VarSymbol) symbolExpr.symbol; diff --git a/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index 5c0b365..a4f5c4d 100644 --- a/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -5,7 +5,7 @@ import com.compilerprogramming.ezlang.semantic.SemaAssignTypes; import com.compilerprogramming.ezlang.semantic.SemaDefineTypes; import com.compilerprogramming.ezlang.types.Symbol; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; import com.compilerprogramming.ezlang.types.TypeDictionary; import java.util.BitSet; @@ -15,7 +15,7 @@ public class Compiler { private void compile(TypeDictionary typeDictionary) { for (Symbol symbol: typeDictionary.getLocalSymbols()) { if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) { - Type.TypeFunction functionType = (Type.TypeFunction) functionSymbol.type; + EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type; functionType.code = new CompiledFunction(functionSymbol); } } diff --git a/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java b/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java index 7fd42cd..fc0237d 100644 --- a/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java +++ b/stackvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler; -import com.compilerprogramming.ezlang.types.Type; +import com.compilerprogramming.ezlang.types.EZType; public class Instruction { @@ -98,8 +98,8 @@ public UnaryOp(int opcode) { } public static class LoadFunction extends Instruction { - public Type.TypeFunction functionType; - public LoadFunction(Type.TypeFunction typeFunction) { + public EZType.EZTypeFunction functionType; + public LoadFunction(EZType.EZTypeFunction typeFunction) { super(LOAD_FUNC); this.functionType = typeFunction; } @@ -122,8 +122,8 @@ public StringBuilder toStr(StringBuilder sb) { } public static class New extends Instruction { - public final Type type; - public New(Type type) { + public final EZType type; + public New(EZType type) { super(NEW); this.type = type; } diff --git a/types/src/main/java/com/compilerprogramming/ezlang/types/Type.java b/types/src/main/java/com/compilerprogramming/ezlang/types/EZType.java similarity index 74% rename from types/src/main/java/com/compilerprogramming/ezlang/types/Type.java rename to types/src/main/java/com/compilerprogramming/ezlang/types/EZType.java index 1c8514c..22f98ce 100644 --- a/types/src/main/java/com/compilerprogramming/ezlang/types/Type.java +++ b/types/src/main/java/com/compilerprogramming/ezlang/types/EZType.java @@ -10,7 +10,7 @@ * Currently, we support Int, Struct, and Array of Int/Struct. * Arrays and Structs are reference types. */ -public abstract class Type { +public abstract class EZType { // Type classes static final byte TVOID = 0; @@ -25,7 +25,7 @@ public abstract class Type { public final byte tclass; // type class public final String name; // type name, always unique - protected Type(byte tclass, String name) { + protected EZType(byte tclass, String name) { this.tclass = tclass; this.name = name; } @@ -37,7 +37,7 @@ protected Type(byte tclass, String name) { public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Type type = (Type) o; + EZType type = (EZType) o; return tclass == type.tclass && Objects.equals(name, type.name); } @@ -52,16 +52,16 @@ public String toString() { } public String name() { return name; } - public boolean isAssignable(Type other) { - if (other == null || other instanceof TypeVoid || other instanceof TypeUnknown) + public boolean isAssignable(EZType other) { + if (other == null || other instanceof EZTypeVoid || other instanceof EZTypeUnknown) return false; if (this == other || equals(other)) return true; - if (this instanceof TypeNullable nullable) { - if (other instanceof TypeNull) + if (this instanceof EZTypeNullable nullable) { + if (other instanceof EZTypeNull) return true; return nullable.baseType.isAssignable(other); } - else if (other instanceof TypeNullable nullable) { + else if (other instanceof EZTypeNullable nullable) { // At compile time we allow nullable value to be // assigned to base type, but null check must be inserted // Optimizer may remove null check @@ -74,25 +74,25 @@ else if (other instanceof TypeNullable nullable) { * Represents no type - useful for defining functions * that do not return a value */ - public static class TypeVoid extends Type { - public TypeVoid() { + public static class EZTypeVoid extends EZType { + public EZTypeVoid() { super(TVOID, "$Void"); } } - public static class TypeUnknown extends Type { - public TypeUnknown() { + public static class EZTypeUnknown extends EZType { + public EZTypeUnknown() { super(TUNKNOWN, "$Unknown"); } } - public static class TypeNull extends Type { - public TypeNull() { super(TNULL, "$Null"); } + public static class EZTypeNull extends EZType { + public EZTypeNull() { super(TNULL, "$Null"); } } - public static class TypeInteger extends Type { + public static class EZTypeInteger extends EZType { - public TypeInteger() { + public EZTypeInteger() { super (TINT, "Int"); } @Override @@ -101,15 +101,15 @@ public boolean isPrimitive() { } } - public static class TypeStruct extends Type { + public static class EZTypeStruct extends EZType { ArrayList fieldNames = new ArrayList<>(); - ArrayList fieldTypes = new ArrayList<>(); + ArrayList fieldTypes = new ArrayList<>(); public boolean pending = true; - public TypeStruct(String name) { + public EZTypeStruct(String name) { super(TSTRUCT, name); } - public void addField(String name, Type type) { + public void addField(String name, EZType type) { if (!pending) throw new CompilerException("Cannot add field to an already defined struct"); if (fieldNames.contains(name)) @@ -124,13 +124,13 @@ public String describe() { sb.append("struct ").append(name()).append("{"); for (int i = 0; i < fieldNames.size(); i++) { String fieldName = fieldNames.get(i); - Type fieldType = fieldTypes.get(i); + EZType fieldType = fieldTypes.get(i); sb.append(fieldName).append(": ").append(fieldType.name()).append(";"); } sb.append("}"); return sb.toString(); } - public Type getField(String name) { + public EZType getField(String name) { int index = fieldNames.indexOf(name); if (index < 0) return null; @@ -144,40 +144,40 @@ public int getFieldIndex(String name) { public void complete() { pending = false; } } - public static class TypeArray extends Type { - Type elementType; + public static class EZTypeArray extends EZType { + EZType elementType; - public TypeArray(Type baseType) { + public EZTypeArray(EZType baseType) { super(TARRAY, "[" + baseType.name() + "]"); this.elementType = baseType; - if (baseType instanceof TypeArray) + if (baseType instanceof EZTypeArray) throw new CompilerException("Array of array type not supported"); } - public Type getElementType() { + public EZType getElementType() { return elementType; } } // This is really a dedicated Union type for T|Null. - public static class TypeNullable extends Type { - public final Type baseType; - public TypeNullable(Type baseType) { + public static class EZTypeNullable extends EZType { + public final EZType baseType; + public EZTypeNullable(EZType baseType) { super(TNULLABLE, baseType.name()+"?"); this.baseType = baseType; } } - public static class TypeFunction extends Type { + public static class EZTypeFunction extends EZType { public final List args = new ArrayList<>(); - public Type returnType; + public EZType returnType; public Object code; - public TypeFunction(String name) { + public EZTypeFunction(String name) { super(TFUNC, name); } public void addArg(Symbol arg) { args.add(arg); } - public void setReturnType(Type returnType) { + public void setReturnType(EZType returnType) { this.returnType = returnType; } public String describe() { @@ -191,7 +191,7 @@ public String describe() { sb.append(arg.name).append(": ").append(arg.type.name()); } sb.append(")"); - if (!(returnType instanceof Type.TypeVoid)) { + if (!(returnType instanceof EZTypeVoid)) { sb.append("->").append(returnType.name()); } return sb.toString(); diff --git a/types/src/main/java/com/compilerprogramming/ezlang/types/Symbol.java b/types/src/main/java/com/compilerprogramming/ezlang/types/Symbol.java index c15a596..bd5d776 100644 --- a/types/src/main/java/com/compilerprogramming/ezlang/types/Symbol.java +++ b/types/src/main/java/com/compilerprogramming/ezlang/types/Symbol.java @@ -1,34 +1,32 @@ package com.compilerprogramming.ezlang.types; -import java.util.Objects; - /** * A symbol is something that has a name and a type. */ public abstract class Symbol { public final String name; - public Type type; + public EZType type; - protected Symbol(String name, Type type) { + protected Symbol(String name, EZType type) { this.name = name; this.type = type; } public static class TypeSymbol extends Symbol { - public TypeSymbol(String name, Type type) { + public TypeSymbol(String name, EZType type) { super(name, type); } } public static class FunctionTypeSymbol extends Symbol { public final Object functionDecl; - public FunctionTypeSymbol(String name, Type.TypeFunction type, Object functionDecl) { + public FunctionTypeSymbol(String name, EZType.EZTypeFunction type, Object functionDecl) { super(name, type); this.functionDecl = functionDecl; } public Object code() { - Type.TypeFunction function = (Type.TypeFunction) type; + EZType.EZTypeFunction function = (EZType.EZTypeFunction) type; return function.code; } } @@ -36,13 +34,13 @@ public Object code() { public static class VarSymbol extends Symbol { // Values assigned by bytecode compiler public int regNumber; - public VarSymbol(String name, Type type) { + public VarSymbol(String name, EZType type) { super(name, type); } } public static class ParameterSymbol extends VarSymbol { - public ParameterSymbol(String name, Type type) { + public ParameterSymbol(String name, EZType type) { super(name, type); } } diff --git a/types/src/main/java/com/compilerprogramming/ezlang/types/TypeDictionary.java b/types/src/main/java/com/compilerprogramming/ezlang/types/TypeDictionary.java index 9fc233b..18dcf83 100644 --- a/types/src/main/java/com/compilerprogramming/ezlang/types/TypeDictionary.java +++ b/types/src/main/java/com/compilerprogramming/ezlang/types/TypeDictionary.java @@ -3,56 +3,56 @@ import com.compilerprogramming.ezlang.exceptions.CompilerException; public class TypeDictionary extends Scope { - public final Type.TypeUnknown UNKNOWN; - public final Type.TypeInteger INT; - public final Type.TypeNull NULL; - public final Type.TypeVoid VOID; + public final EZType.EZTypeUnknown UNKNOWN; + public final EZType.EZTypeInteger INT; + public final EZType.EZTypeNull NULL; + public final EZType.EZTypeVoid VOID; public TypeDictionary() { super(null); - INT = (Type.TypeInteger) intern(new Type.TypeInteger()); - UNKNOWN = (Type.TypeUnknown) intern(new Type.TypeUnknown()); - NULL = (Type.TypeNull) intern(new Type.TypeNull()); - VOID = (Type.TypeVoid) intern(new Type.TypeVoid()); + INT = (EZType.EZTypeInteger) intern(new EZType.EZTypeInteger()); + UNKNOWN = (EZType.EZTypeUnknown) intern(new EZType.EZTypeUnknown()); + NULL = (EZType.EZTypeNull) intern(new EZType.EZTypeNull()); + VOID = (EZType.EZTypeVoid) intern(new EZType.EZTypeVoid()); } - public Type makeArrayType(Type elementType, boolean isNullable) { + public EZType makeArrayType(EZType elementType, boolean isNullable) { switch (elementType) { - case Type.TypeInteger ti -> { - var arrayType = intern(new Type.TypeArray(ti)); - return isNullable ? intern(new Type.TypeNullable(arrayType)) : arrayType; + case EZType.EZTypeInteger ti -> { + var arrayType = intern(new EZType.EZTypeArray(ti)); + return isNullable ? intern(new EZType.EZTypeNullable(arrayType)) : arrayType; } - case Type.TypeStruct ts -> { - var arrayType = intern(new Type.TypeArray(ts)); - return isNullable ? intern(new Type.TypeNullable(arrayType)) : arrayType; + case EZType.EZTypeStruct ts -> { + var arrayType = intern(new EZType.EZTypeArray(ts)); + return isNullable ? intern(new EZType.EZTypeNullable(arrayType)) : arrayType; } - case Type.TypeNullable nullable when nullable.baseType instanceof Type.TypeStruct -> { - var arrayType = intern(new Type.TypeArray(elementType)); - return isNullable ? intern(new Type.TypeNullable(arrayType)) : arrayType; + case EZType.EZTypeNullable nullable when nullable.baseType instanceof EZType.EZTypeStruct -> { + var arrayType = intern(new EZType.EZTypeArray(elementType)); + return isNullable ? intern(new EZType.EZTypeNullable(arrayType)) : arrayType; } case null, default -> throw new CompilerException("Unsupported array element type: " + elementType); } } - public Type intern(Type type) { + public EZType intern(EZType type) { Symbol symbol = lookup(type.name()); if (symbol != null) return symbol.type; return install(type.name(), new Symbol.TypeSymbol(type.name(), type)).type; } - public Type merge(Type t1, Type t2) { - if (t1 instanceof Type.TypeNull && t2 instanceof Type.TypeStruct) { - return intern(new Type.TypeNullable(t2)); + public EZType merge(EZType t1, EZType t2) { + if (t1 instanceof EZType.EZTypeNull && t2 instanceof EZType.EZTypeStruct) { + return intern(new EZType.EZTypeNullable(t2)); } - else if (t2 instanceof Type.TypeNull && t1 instanceof Type.TypeStruct) { - return intern(new Type.TypeNullable(t1)); + else if (t2 instanceof EZType.EZTypeNull && t1 instanceof EZType.EZTypeStruct) { + return intern(new EZType.EZTypeNullable(t1)); } - else if (t1 instanceof Type.TypeArray && t2 instanceof Type.TypeNull) { - return intern(new Type.TypeNullable(t1)); + else if (t1 instanceof EZType.EZTypeArray && t2 instanceof EZType.EZTypeNull) { + return intern(new EZType.EZTypeNullable(t1)); } - else if (t2 instanceof Type.TypeArray && t1 instanceof Type.TypeNull) { - return intern(new Type.TypeNullable(t2)); + else if (t2 instanceof EZType.EZTypeArray && t1 instanceof EZType.EZTypeNull) { + return intern(new EZType.EZTypeNullable(t2)); } - else if (t1 instanceof Type.TypeUnknown) + else if (t1 instanceof EZType.EZTypeUnknown) return t2; - else if (t2 instanceof Type.TypeUnknown) + else if (t2 instanceof EZType.EZTypeUnknown) return t1; else if (!t1.equals(t2)) throw new CompilerException("Unsupported merge type: " + t1 + " and " + t2); diff --git a/types/src/test/java/com/compilerprogramming/ezlang/types/TestTypes.java b/types/src/test/java/com/compilerprogramming/ezlang/types/TestTypes.java index 7cb6dc7..3354854 100644 --- a/types/src/test/java/com/compilerprogramming/ezlang/types/TestTypes.java +++ b/types/src/test/java/com/compilerprogramming/ezlang/types/TestTypes.java @@ -1,22 +1,17 @@ package com.compilerprogramming.ezlang.types; -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; - public class TestTypes { - Type buildStruct1(TypeDictionary typeDictionary) { - Type.TypeStruct s = new Type.TypeStruct("S1"); + EZType buildStruct1(TypeDictionary typeDictionary) { + EZType.EZTypeStruct s = new EZType.EZTypeStruct("S1"); s.addField("a", typeDictionary.INT); s.addField("b", typeDictionary.INT); s.complete(); return typeDictionary.intern(s); } - Type buildStruct2(TypeDictionary typeDictionary) { - Type.TypeStruct s = new Type.TypeStruct("S2"); + EZType buildStruct2(TypeDictionary typeDictionary) { + EZType.EZTypeStruct s = new EZType.EZTypeStruct("S2"); s.addField("a", typeDictionary.INT); s.addField("b", typeDictionary.INT); s.complete(); From 9f52d795c119dd76cb960e308c024fde24f6b238 Mon Sep 17 00:00:00 2001 From: dibyendumajumdar Date: Sun, 27 Jul 2025 17:48:08 +0100 Subject: [PATCH 2/9] Rename SONType to Type - and all sub types too so that it is easy to merge code from Simple --- .../ezlang/compiler/Compiler.java | 138 +++++++++--------- .../ezlang/compiler/Var.java | 18 +-- .../ezlang/compiler/codegen/BuildLRG.java | 5 +- .../ezlang/compiler/codegen/CodeGen.java | 26 ++-- .../ezlang/compiler/codegen/ElfFile.java | 3 +- .../ezlang/compiler/codegen/Encoding.java | 23 ++- .../compiler/codegen/GlobalCodeMotion.java | 4 +- .../ezlang/compiler/codegen/Machine.java | 8 +- .../ezlang/compiler/nodes/AddFNode.java | 16 +- .../ezlang/compiler/nodes/AddNode.java | 27 ++-- .../ezlang/compiler/nodes/AndNode.java | 26 ++-- .../ezlang/compiler/nodes/BoolNode.java | 32 ++-- .../ezlang/compiler/nodes/CFGNode.java | 4 +- .../ezlang/compiler/nodes/CProjNode.java | 16 +- .../ezlang/compiler/nodes/CallEndNode.java | 20 +-- .../ezlang/compiler/nodes/CallNode.java | 19 ++- .../ezlang/compiler/nodes/CalleeSaveNode.java | 7 +- .../ezlang/compiler/nodes/CastNode.java | 8 +- .../ezlang/compiler/nodes/ConstantNode.java | 20 +-- .../ezlang/compiler/nodes/CtrlNode.java | 4 +- .../ezlang/compiler/nodes/DivFNode.java | 16 +- .../ezlang/compiler/nodes/DivNode.java | 22 +-- .../ezlang/compiler/nodes/FRefNode.java | 4 +- .../ezlang/compiler/nodes/FunNode.java | 22 +-- .../ezlang/compiler/nodes/IfNode.java | 17 +-- .../ezlang/compiler/nodes/LoadNode.java | 20 +-- .../ezlang/compiler/nodes/LogicalNode.java | 6 +- .../ezlang/compiler/nodes/LoopNode.java | 14 +- .../compiler/nodes/MachConcreteNode.java | 4 +- .../ezlang/compiler/nodes/MemMergeNode.java | 6 +- .../ezlang/compiler/nodes/MemOpNode.java | 20 +-- .../ezlang/compiler/nodes/MinusFNode.java | 12 +- .../ezlang/compiler/nodes/MinusNode.java | 16 +- .../ezlang/compiler/nodes/MulFNode.java | 18 +-- .../ezlang/compiler/nodes/MulNode.java | 32 ++-- .../ezlang/compiler/nodes/NeverNode.java | 7 +- .../ezlang/compiler/nodes/NewNode.java | 36 ++--- .../ezlang/compiler/nodes/Node.java | 28 ++-- .../ezlang/compiler/nodes/NotNode.java | 14 +- .../ezlang/compiler/nodes/OrNode.java | 25 ++-- .../ezlang/compiler/nodes/ParmNode.java | 4 +- .../ezlang/compiler/nodes/PhiNode.java | 36 ++--- .../ezlang/compiler/nodes/ProjNode.java | 14 +- .../ezlang/compiler/nodes/ReadOnlyNode.java | 12 +- .../ezlang/compiler/nodes/RegionNode.java | 10 +- .../ezlang/compiler/nodes/ReturnNode.java | 30 ++-- .../ezlang/compiler/nodes/RoundF32Node.java | 14 +- .../ezlang/compiler/nodes/SarNode.java | 28 ++-- .../ezlang/compiler/nodes/ScopeNode.java | 10 +- .../ezlang/compiler/nodes/ShlNode.java | 26 ++-- .../ezlang/compiler/nodes/ShrNode.java | 27 ++-- .../ezlang/compiler/nodes/SplitNode.java | 4 +- .../ezlang/compiler/nodes/StartNode.java | 9 +- .../ezlang/compiler/nodes/StopNode.java | 6 +- .../ezlang/compiler/nodes/StoreNode.java | 26 ++-- .../ezlang/compiler/nodes/StructNode.java | 12 +- .../ezlang/compiler/nodes/SubFNode.java | 16 +- .../ezlang/compiler/nodes/SubNode.java | 22 +-- .../ezlang/compiler/nodes/ToFloatNode.java | 18 +-- .../ezlang/compiler/nodes/XCtrlNode.java | 4 +- .../ezlang/compiler/nodes/XorNode.java | 24 +-- .../compiler/nodes/cpus/arm/CallARM.java | 8 +- .../compiler/nodes/cpus/arm/FloatARM.java | 5 +- .../compiler/nodes/cpus/arm/IntARM.java | 6 +- .../compiler/nodes/cpus/arm/LoadARM.java | 6 +- .../compiler/nodes/cpus/arm/MemOpARM.java | 4 +- .../compiler/nodes/cpus/arm/NegARM.java | 1 - .../compiler/nodes/cpus/arm/StoreARM.java | 6 +- .../compiler/nodes/cpus/arm/SubIARM.java | 1 - .../compiler/nodes/cpus/arm/TFPARM.java | 2 - .../compiler/nodes/cpus/arm/UJmpARM.java | 4 +- .../ezlang/compiler/nodes/cpus/arm/arm.java | 60 ++++---- .../compiler/nodes/cpus/riscv/AUIPC.java | 4 +- .../compiler/nodes/cpus/riscv/CallRISC.java | 8 +- .../compiler/nodes/cpus/riscv/FltRISC.java | 2 - .../compiler/nodes/cpus/riscv/ImmRISC.java | 4 +- .../compiler/nodes/cpus/riscv/IntRISC.java | 6 +- .../ezlang/compiler/nodes/cpus/riscv/LUI.java | 12 +- .../compiler/nodes/cpus/riscv/MemOpRISC.java | 34 ++--- .../compiler/nodes/cpus/riscv/TFPRISC.java | 4 +- .../compiler/nodes/cpus/riscv/UJmpRISC.java | 4 +- .../compiler/nodes/cpus/riscv/riscv.java | 60 ++++---- .../nodes/cpus/x86_64_v2/CallX86.java | 8 +- .../nodes/cpus/x86_64_v2/CmpMemX86.java | 10 +- .../compiler/nodes/cpus/x86_64_v2/ImmX86.java | 1 - .../compiler/nodes/cpus/x86_64_v2/IntX86.java | 14 +- .../nodes/cpus/x86_64_v2/LoadX86.java | 31 ++-- .../nodes/cpus/x86_64_v2/MemOpX86.java | 4 +- .../compiler/nodes/cpus/x86_64_v2/NewX86.java | 1 - .../nodes/cpus/x86_64_v2/SplitX86.java | 10 +- .../nodes/cpus/x86_64_v2/StoreX86.java | 7 +- .../compiler/nodes/cpus/x86_64_v2/TFPX86.java | 12 +- .../nodes/cpus/x86_64_v2/UJmpX86.java | 4 +- .../nodes/cpus/x86_64_v2/x86_64_v2.java | 80 +++++----- .../ezlang/compiler/print/ASMPrinter.java | 12 +- .../ezlang/compiler/sontypes/Field.java | 22 +-- .../sontypes/{SONType.java => Type.java} | 80 +++++----- .../{SONTypeFloat.java => TypeFloat.java} | 40 ++--- .../{SONTypeFunPtr.java => TypeFunPtr.java} | 51 +++---- .../{SONTypeInteger.java => TypeInteger.java} | 54 +++---- .../{SONTypeMem.java => TypeMem.java} | 30 ++-- .../{SONTypeMemPtr.java => TypeMemPtr.java} | 48 +++--- .../{SONTypeNil.java => TypeNil.java} | 28 ++-- .../{SONTypePtr.java => TypePtr.java} | 32 ++-- .../{SONTypeRPC.java => TypeRPC.java} | 28 ++-- .../{SONTypeStruct.java => TypeStruct.java} | 44 +++--- .../{SONTypeTuple.java => TypeTuple.java} | 60 ++++---- 107 files changed, 1020 insertions(+), 1047 deletions(-) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONType.java => Type.java} (79%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeFloat.java => TypeFloat.java} (63%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeFunPtr.java => TypeFunPtr.java} (62%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeInteger.java => TypeInteger.java} (67%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeMem.java => TypeMem.java} (59%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeMemPtr.java => TypeMemPtr.java} (60%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeNil.java => TypeNil.java} (61%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypePtr.java => TypePtr.java} (51%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeRPC.java => TypeRPC.java} (74%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeStruct.java => TypeStruct.java} (84%) rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/{SONTypeTuple.java => TypeTuple.java} (50%) diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index 813b708..87b9624 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -57,22 +57,22 @@ 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 TYPES = new HashMap<>(); + public static HashMap TYPES = new HashMap<>(); private ArrayList ctorStack = new ArrayList<>(); - public Compiler(CodeGen codeGen, SONTypeInteger arg) { + public Compiler(CodeGen codeGen, TypeInteger arg) { this._code = codeGen; } public void parse() { this.typeDictionary = createAST(_code._src); - Map types = new HashMap<>(); + Map types = new HashMap<>(); populateDefaultTypes(types); populateTypes(types); TYPES.putAll(types); - ZERO = con(SONTypeInteger.ZERO).keep(); - NIL = con(SONType.NIL).keep(); + ZERO = con(TypeInteger.ZERO).keep(); + NIL = con(Type.NIL).keep(); XCTRL= new XCtrlNode().peephole().keep(); processFunctions(); } @@ -89,19 +89,19 @@ public TypeDictionary createAST(String src) { } - private void populateDefaultTypes(Map types) { + private void populateDefaultTypes(Map types) { // Pre-create int, [int] and *[int] types - types.put(typeDictionary.INT.name(), SONTypeInteger.BOT); - var intArrayType = SONTypeStruct.makeAry(SONTypeInteger.U32, _code.getALIAS(), SONTypeInteger.BOT, _code.getALIAS()); - var ptrIntArrayType = SONTypeMemPtr.make(intArrayType); + types.put(typeDictionary.INT.name(), TypeInteger.BOT); + var intArrayType = TypeStruct.makeAry(TypeInteger.U32, _code.getALIAS(), TypeInteger.BOT, _code.getALIAS()); + var ptrIntArrayType = TypeMemPtr.make(intArrayType); types.put("[" + typeDictionary.INT.name() + "]", ptrIntArrayType); // Also get the types created by default - for (SONType t: SONType.gather()) { + for (Type t: Type.gather()) { types.put(t.str(), t); } } - private void populateTypes(Map structTypes) { + private void populateTypes(Map structTypes) { // First process struct types for (var symbol: typeDictionary.getLocalSymbols()) { if (symbol instanceof Symbol.TypeSymbol typeSymbol) { @@ -129,21 +129,21 @@ private void processFunctions() { } } - private void createFunctionType(Map structTypes, Symbol.FunctionTypeSymbol functionSymbol) { + private void createFunctionType(Map structTypes, Symbol.FunctionTypeSymbol functionSymbol) { EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type; - Ary params = new Ary<>(SONType.class); + Ary params = new Ary<>(Type.class); for (var symbol: functionType.args) { if (symbol instanceof Symbol.ParameterSymbol parameterSymbol) { - SONType paramType = getSONType(structTypes, parameterSymbol.type); + Type paramType = getSONType(structTypes, parameterSymbol.type); params.push(paramType); } } var retType = getSONType(structTypes, functionType.returnType); - SONTypeFunPtr tfp = _code.makeFun2(new SONTypeTuple(params.asAry()),retType); + TypeFunPtr tfp = _code.makeFun2(new TypeTuple(params.asAry()),retType); structTypes.put(functionSymbol.name, tfp); } - private void createSONStructType(Map structTypes, String typeName, EZType.EZTypeStruct typeStruct) { + private void createSONStructType(Map structTypes, String typeName, EZType.EZTypeStruct typeStruct) { Ary fs = new Ary<>(Field.class); for (int i = 0; i < typeStruct.numFields(); i++) { String name = typeStruct.getFieldName(i); @@ -152,10 +152,10 @@ private void createSONStructType(Map structTypes, String typeNa } // A Struct type may have been created before because of // reference from itself; in which case we need to update that - SONType fref = structTypes.get(typeName); + Type fref = structTypes.get(typeName); if (fref != null) { - if (fref instanceof SONTypeMemPtr ptr && - ptr._obj instanceof SONTypeStruct ts) { + if (fref instanceof TypeMemPtr ptr && + ptr._obj instanceof TypeStruct ts) { assert ts._fields.length == 0; // Add the fields to the existing type ts._fields = fs.asAry(); @@ -163,8 +163,8 @@ private void createSONStructType(Map structTypes, String typeNa else throw new CompilerException("Expected struct type " + typeName + " but got " + fref); } else { - var ts = SONTypeStruct.make(typeName, fs.asAry()); - var ptr = SONTypeMemPtr.make((byte)2,ts); + var ts = TypeStruct.make(typeName, fs.asAry()); + var ptr = TypeMemPtr.make((byte)2,ts); structTypes.put(typeName,ptr); } } @@ -181,10 +181,10 @@ else if (type instanceof EZType.EZTypeStruct typeStruct) { } else if (type instanceof EZType.EZTypeInteger || type instanceof EZType.EZTypeVoid) { - return SONTypeInteger.BOT.str(); + return TypeInteger.BOT.str(); } else if (type instanceof EZType.EZTypeNull) { - return SONTypeNil.NIL.str(); + return TypeNil.NIL.str(); } else if (type instanceof EZType.EZTypeNullable typeNullable) { return getSONTypeName(typeNullable.baseType)+"?"; @@ -192,16 +192,16 @@ else if (type instanceof EZType.EZTypeNullable typeNullable) { else throw new CompilerException("Not yet implemented " + type.name()); } - private SONType getSONType(Map structTypes, EZType type) { - SONType t = structTypes.get(type.name()); + private Type getSONType(Map structTypes, EZType type) { + Type t = structTypes.get(type.name()); if (t != null) return t; if (type instanceof EZType.EZTypeStruct) { // For struct types in EeZee language a reference // to T means *T in SoN // Create SON struct type - SONTypeStruct ts = SONTypeStruct.make(type.name, new Field[0]); + TypeStruct ts = TypeStruct.make(type.name, new Field[0]); // Now create *T - SONTypeMemPtr ptr = SONTypeMemPtr.make((byte)2,ts); + TypeMemPtr ptr = TypeMemPtr.make((byte)2,ts); // EeZee T maps to SoN *T structTypes.put(type.name(), ptr); return ptr; @@ -209,23 +209,23 @@ private SONType getSONType(Map structTypes, EZType type) { else if (type instanceof EZType.EZTypeArray typeArray) { // A reference to array in EeZee means // *array in SoN - SONType elementType = getSONType(structTypes,typeArray.getElementType()); - SONTypeStruct ts = SONTypeStruct.makeArray(SONTypeInteger.U32, _code.getALIAS(), elementType, _code.getALIAS()); - SONTypeMemPtr ptr = SONTypeMemPtr.make((byte)2,ts); + Type elementType = getSONType(structTypes,typeArray.getElementType()); + TypeStruct ts = TypeStruct.makeArray(TypeInteger.U32, _code.getALIAS(), elementType, _code.getALIAS()); + TypeMemPtr ptr = TypeMemPtr.make((byte)2,ts); structTypes.put(typeArray.name(), ptr); // Array type name is not same as ptr str() return ptr; } else if (type instanceof EZType.EZTypeNullable typeNullable) { - SONType baseType = getSONType(structTypes,typeNullable.baseType); - SONTypeMemPtr ptr = null; - if (baseType instanceof SONTypeMemPtr ptr1) { + Type baseType = getSONType(structTypes,typeNullable.baseType); + TypeMemPtr ptr = null; + if (baseType instanceof TypeMemPtr ptr1) { if (ptr1.nullable()) ptr = ptr1; else - ptr = SONTypeMemPtr.make((byte)3,ptr1._obj); + ptr = TypeMemPtr.make((byte)3,ptr1._obj); } else - ptr = SONTypeMemPtr.make((byte)2,(SONTypeStruct) baseType); + ptr = TypeMemPtr.make((byte)2,(TypeStruct) baseType); structTypes.put(typeNullable.name(), ptr); return ptr; } @@ -244,7 +244,7 @@ private void defineScopedVars(Scope scope, ScopeNode scopeNode, FunNode fun) { for (Symbol symbol: scope.getLocalSymbols()) { if (symbol instanceof Symbol.VarSymbol varSymbol) { varSymbol.regNumber = REGNUM++; - SONType 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; @@ -259,13 +259,13 @@ private void defineScopedVars(Scope scope, ScopeNode scopeNode, FunNode fun) { private void generateFunction(Symbol.FunctionTypeSymbol functionTypeSymbol) { _scope = new ScopeNode(); - _scope.define(ScopeNode.CTRL, SONType.CONTROL , false, null); - _scope.define(ScopeNode.MEM0, SONTypeMem.BOT , false, null); + _scope.define(ScopeNode.CTRL, Type.CONTROL , false, null); + _scope.define(ScopeNode.MEM0, TypeMem.BOT , false, null); ctrl(XCTRL); _scope.mem(new MemMergeNode(false)); - var funType = (SONTypeFunPtr) 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 }" @@ -295,7 +295,7 @@ Node showGraph() { /** * Parses a function body, assuming the header is parsed. */ - private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSymbol,SONTypeFunPtr sig) { + private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSymbol, TypeFunPtr sig) { // Stack parser state on the local Java stack, and unstack it later Node oldctrl = ctrl().keep(); Node oldmem = _scope.mem().keep(); @@ -310,13 +310,13 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy // the exact single function). _code.link(fun); - Node rpc = new ParmNode("$rpc",0,SONTypeRPC.BOT,fun,con(SONTypeRPC.BOT)).peephole(); + Node rpc = new ParmNode("$rpc",0, TypeRPC.BOT,fun,con(TypeRPC.BOT)).peephole(); // Build a multi-exit return point for all function returns RegionNode r = new RegionNode(null,null).init(); assert r.inProgress(); - PhiNode rmem = new PhiNode(ScopeNode.MEM0,SONTypeMem.BOT,r,null).init(); - PhiNode rrez = new PhiNode(ScopeNode.ARG0,SONType.BOTTOM,r,null).init(); + PhiNode rmem = new PhiNode(ScopeNode.MEM0, TypeMem.BOT,r,null).init(); + PhiNode rrez = new PhiNode(ScopeNode.ARG0, Type.BOTTOM,r,null).init(); ReturnNode ret = new ReturnNode(r, rmem, rrez, rpc, fun).init(); fun.setRet(ret); assert ret.inProgress(); @@ -330,7 +330,7 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy // Private mem alias tracking per function MemMergeNode mem = new MemMergeNode(true); mem.addDef(null); // Alias#0 - mem.addDef(new ParmNode(ScopeNode.MEM0,1,SONTypeMem.BOT,fun,con(SONTypeMem.BOT)).peephole()); // All aliases + mem.addDef(new ParmNode(ScopeNode.MEM0,1, TypeMem.BOT,fun,con(TypeMem.BOT)).peephole()); // All aliases _scope.mem(mem); // All args, "as-if" called externally AST.FuncDecl funcDecl = (AST.FuncDecl) functionTypeSymbol.functionDecl; @@ -341,7 +341,7 @@ private ReturnNode generateFunctionBody(Symbol.FunctionTypeSymbol functionTypeSy Node last = compileStatement(funcDecl.block); // Last expression is the return - if( ctrl()._type==SONType.CONTROL ) + if( ctrl()._type== Type.CONTROL ) fun.addReturn(ctrl(), _scope.mem().merge(), last); // Pop off the inProgress node on the multi-exit Region merge @@ -411,15 +411,15 @@ private Node compileExpr(AST.Expr expr) { private Node compileFieldExpr(AST.GetFieldExpr getFieldExpr) { Node objPtr = compileExpr(getFieldExpr.object).keep(); // Sanity check expr for being a reference - if( !(objPtr._type instanceof SONTypeMemPtr ptr) ) { + if( !(objPtr._type instanceof TypeMemPtr ptr) ) { throw new CompilerException("Unexpected type " + objPtr._type.str()); } String name = getFieldExpr.fieldName; - SONTypeStruct base = ptr._obj; + TypeStruct base = ptr._obj; int fidx = base.find(name); if( fidx == -1 ) throw error("Accessing unknown field '" + name + "' from '" + ptr.str() + "'"); Field f = base._fields[fidx]; // Field from field index - SONType tf = f._type; + Type tf = f._type; // Field offset; fixed for structs Node off = con(base.offset(fidx)).keep(); Node load = new LoadNode(name, f._alias, tf, memAlias(f._alias), objPtr, off); @@ -439,16 +439,16 @@ private Node compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) { else objPtr = compileExpr(setFieldExpr.object); // Sanity check expr for being a reference - if( !(objPtr._type instanceof SONTypeMemPtr ptr) ) { + if( !(objPtr._type instanceof TypeMemPtr ptr) ) { throw new CompilerException("Unexpected type " + objPtr._type.str()); } Node val = compileExpr(setFieldExpr.value).keep(); String name = setFieldExpr.fieldName; - SONTypeStruct base = ptr._obj; + TypeStruct base = ptr._obj; int fidx = base.find(name); if( fidx == -1 ) throw error("Accessing unknown field '" + name + "' from '" + ptr.str() + "'"); Field f = base._fields[fidx]; // Field from field index - SONType tf = f._type; + Type tf = f._type; Node mem = memAlias(f._alias); Node st = new StoreNode(f._fname,f._alias,tf,mem,objPtr,con(base.offset(fidx)),val.unkeep(),true).peephole(); memAlias(f._alias,st); @@ -458,17 +458,17 @@ private Node compileSetFieldExpr(AST.SetFieldExpr setFieldExpr) { private Node compileArrayIndexExpr(AST.ArrayLoadExpr arrayLoadExpr) { Node objPtr = compileExpr(arrayLoadExpr.array).keep(); // Sanity check expr for being a reference - if( !(objPtr._type instanceof SONTypeMemPtr ptr) ) { + if( !(objPtr._type instanceof TypeMemPtr ptr) ) { throw new CompilerException("Unexpected type " + objPtr._type.str()); } String name = "[]"; - SONTypeStruct base = ptr._obj; + TypeStruct base = ptr._obj; Node index = compileExpr(arrayLoadExpr.expr).keep(); Node off = peep(new AddNode(con(base.aryBase()),peep(new ShlNode(index.unkeep(),con(base.aryScale()))))).keep(); int fidx = base.find(name); if( fidx == -1 ) throw error("Accessing unknown field '" + name + "' from '" + ptr.str() + "'"); Field f = base._fields[fidx]; // Field from field index - SONType tf = f._type; + Type tf = f._type; Node load = new LoadNode(name, f._alias, tf, memAlias(f._alias), objPtr, off); // Arrays include control, as a proxy for a safety range check // Structs don't need this; they only need a NPE check which is @@ -487,18 +487,18 @@ private Node compileArrayStoreExpr(AST.ArrayStoreExpr arrayStoreExpr) { else objPtr = compileExpr(arrayStoreExpr.array); // Sanity check expr for being a reference - if( !(objPtr._type instanceof SONTypeMemPtr ptr) ) { + if( !(objPtr._type instanceof TypeMemPtr ptr) ) { throw new CompilerException("Unexpected type " + objPtr._type.str()); } String name = "[]"; - SONTypeStruct base = ptr._obj; + TypeStruct base = ptr._obj; Node index = compileExpr(arrayStoreExpr.expr).keep(); Node off = peep(new AddNode(con(base.aryBase()),peep(new ShlNode(index.unkeep(),con(base.aryScale()))))).keep(); Node val = compileExpr(arrayStoreExpr.value).keep(); int fidx = base.find(name); if( fidx == -1 ) throw error("Accessing unknown field '" + name + "' from '" + ptr.str() + "'"); Field f = base._fields[fidx]; // Field from field index - SONType tf = f._type; + Type tf = f._type; Node mem = memAlias(f._alias); Node st = new StoreNode(f._fname,f._alias,tf,mem,objPtr,off.unkeep(),val.unkeep(),true).peephole(); memAlias(f._alias,st); @@ -509,7 +509,7 @@ private Node compileInitExpr(AST.InitExpr initExpr) { Node objPtr = compileExpr(initExpr.newExpr).keep(); ctorStack.add(objPtr); // Sanity check expr for being a reference - if( !(objPtr._type instanceof SONTypeMemPtr ptr) ) { + if( !(objPtr._type instanceof TypeMemPtr ptr) ) { throw new CompilerException("Unexpected type " + objPtr._type.str()); } if (initExpr.initExprList != null && !initExpr.initExprList.isEmpty()) { @@ -521,7 +521,7 @@ private Node compileInitExpr(AST.InitExpr initExpr) { return objPtr.unkeep(); } - private Node newArray(SONTypeStruct ary, Node len) { + private Node newArray(TypeStruct ary, Node len) { int base = ary.aryBase (); int scale= ary.aryScale(); Node size = peep(new AddNode(con(base),peep(new ShlNode(len.keep(),con(scale))))); @@ -531,7 +531,7 @@ private Node newArray(SONTypeStruct ary, Node len) { * Return a NewNode initialized memory. * @param obj is the declared type, with GLB fields */ - private Node newStruct( SONTypeStruct obj, Node size) { + private Node newStruct(TypeStruct obj, Node size) { Field[] fs = obj._fields; if( fs==null ) throw error("Unknown struct type '" + obj._name + "'"); @@ -544,7 +544,7 @@ private Node newStruct( SONTypeStruct obj, Node size) { for( int i = 0; i < len; i++ ) ns[2+i] = memAlias(fs[i]._alias); // FIXME make - Node nnn = new NewNode(SONTypeMemPtr.make(obj), ns).peephole().keep(); + Node nnn = new NewNode(TypeMemPtr.make(obj), ns).peephole().keep(); for( int i = 0; i < len; i++ ) memAlias(fs[i]._alias, new ProjNode(nnn,i+2,memName(fs[i]._alias)).peephole()); return new ProjNode(nnn.unkeep(),1,obj._name).peephole(); @@ -553,11 +553,11 @@ private Node newStruct( SONTypeStruct obj, Node size) { private Node compileNewExpr(AST.NewExpr newExpr) { EZType type = newExpr.type; if (type instanceof EZType.EZTypeArray typeArray) { - SONTypeMemPtr tarray = (SONTypeMemPtr) 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) { - SONTypeMemPtr tptr = (SONTypeMemPtr) TYPES.get(typeStruct.name()); + TypeMemPtr tptr = (TypeMemPtr) TYPES.get(typeStruct.name()); return newStruct(tptr._obj,con(tptr._obj.offset(tptr._obj._fields.length))); } else @@ -653,9 +653,9 @@ private Node compileSymbolExpr(AST.NameExpr symbolExpr) { */ private Node compileCallExpr(AST.CallExpr callExpr) { Node expr = compileExpr(callExpr.callee); - if( expr._type == SONType.NIL ) + if( expr._type == Type.NIL ) throw error("Calling a null function pointer"); - if( !(expr instanceof FRefNode) && !expr._type.isa(SONTypeFunPtr.BOT) ) + if( !(expr instanceof FRefNode) && !expr._type.isa(TypeFunPtr.BOT) ) throw error("Expected a function but got "+expr._type.glb().str()); expr.keep(); // Keep while parsing args @@ -674,11 +674,11 @@ private Node compileCallExpr(AST.CallExpr callExpr) { for( Node arg : args ) arg.unkeep(); // Dead into the call? Skip all the node gen - if( ctrl()._type == SONType.XCONTROL ) { + if( ctrl()._type == Type.XCONTROL ) { for( Node arg : args ) if( arg.isUnused() ) arg.kill(); - return con(SONType.TOP); + return con(Type.TOP); } // Into the call @@ -962,8 +962,8 @@ private Node compileBlock(AST.BlockStmt block) { return last; } - public static Node con(long con ) { return con==0 ? ZERO : con(SONTypeInteger.constant(con)); } - public static ConstantNode con( SONType t ) { return (ConstantNode)new ConstantNode(t).peephole(); } + public static Node con(long con ) { return con==0 ? ZERO : con(TypeInteger.constant(con)); } + public static ConstantNode con( Type t ) { return (ConstantNode)new ConstantNode(t).peephole(); } public Node peep( Node n ) { // Peephole, then improve with lexically scoped guards return _scope.upcastGuard(n.peephole()); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java index 6016939..29327fa 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java @@ -10,36 +10,36 @@ public class Var { public final String _name; // Declared name public int _idx; // index in containing scope - private SONType _type; // Declared type + private Type _type; // Declared type public boolean _final; // Final field public boolean _fref; // Forward ref - public Var(int idx, String name, SONType type, boolean xfinal) { + public Var(int idx, String name, Type type, boolean xfinal) { this(idx,name,type,xfinal,false); } - public Var(int idx, String name, SONType type, boolean xfinal, boolean fref) { + public Var(int idx, String name, Type type, boolean xfinal, boolean fref) { _idx = idx; _name = name; _type = type; _final = xfinal; _fref = fref; } - public SONType type() { + public Type type() { if( !_type.isFRef() ) return _type; // Update self to no longer use the forward ref type - SONType def = Compiler.TYPES.get(((SONTypeMemPtr)_type)._obj._name); + Type def = Compiler.TYPES.get(((TypeMemPtr)_type)._obj._name); return (_type=_type.meet(def)); } - public SONType lazyGLB() { - SONType t = type(); - return t instanceof SONTypeMemPtr ? t : t.glb(); + public Type lazyGLB() { + Type t = type(); + return t instanceof TypeMemPtr ? t : t.glb(); } // Forward reference variables (not types) must be BOTTOM and // distinct from inferred variables public boolean isFRef() { return _fref; } - public void defFRef(SONType type, boolean xfinal) { + public void defFRef(Type type, boolean xfinal) { assert isFRef() && xfinal; _type = type; _final = true; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/BuildLRG.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/BuildLRG.java index 1b4f17b..195506b 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/BuildLRG.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/BuildLRG.java @@ -1,8 +1,7 @@ package com.compilerprogramming.ezlang.compiler.codegen; -import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeMem; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeMem; abstract public class BuildLRG { // Compute live ranges in a single forwards pass. Every def is a new live @@ -17,7 +16,7 @@ abstract public class BuildLRG { public static boolean run(int round, RegAlloc alloc) { for( Node bb : alloc._code._cfg ) for( Node n : bb.outs() ) { - if( n instanceof PhiNode phi && !(phi._type instanceof SONTypeMem) ) { + if( n instanceof PhiNode phi && !(phi._type instanceof TypeMem) ) { // All Phi inputs end up with the same LRG. // Pass 1: find any pre-existing LRG, to avoid make-then-Union a LRG LRG lrg = alloc.lrg(phi); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java index fa890bd..b5666e6 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java @@ -36,11 +36,11 @@ public enum Phase { // Compilation source code public final String _src; // Compile-time known initial argument type - public final SONTypeInteger _arg; + public final TypeInteger _arg; // --------------------------- - public CodeGen( String src ) { this(src, SONTypeInteger.BOT, 123L ); } - public CodeGen( String src, SONTypeInteger arg, long workListSeed ) { + public CodeGen( String src ) { this(src, TypeInteger.BOT, 123L ); } + public CodeGen(String src, TypeInteger arg, long workListSeed ) { CODE = this; _phase = null; _callingConv = null; @@ -122,39 +122,39 @@ public int iDepthFrom(int idepth) { // Compute "function indices": FIDX. // Each new request at the same signature gets a new FIDX. - private final HashMap FIDXS = new HashMap<>(); - public SONTypeFunPtr makeFun( SONTypeTuple sig, SONType ret ) { + private final HashMap FIDXS = new HashMap<>(); + public TypeFunPtr makeFun(TypeTuple sig, Type ret ) { Integer i = FIDXS.get(sig); int fidx = i==null ? 0 : i; FIDXS.put(sig,fidx+1); // Track count per sig assert fidx<64; // TODO: need a larger FIDX space - return SONTypeFunPtr.make((byte)2,sig,ret, 1L< _linker = new HashMap<>(); + private final HashMap _linker = new HashMap<>(); // Parser object public final Compiler P; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java index 490b921..54bd967 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java @@ -9,7 +9,6 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.HashMap; -import java.util.Map; public class ElfFile { @@ -213,7 +212,7 @@ private void pushSection(Section s) { } /* creates function and stores where it starts*/ - private final HashMap _funcs = new HashMap<>(); + private final HashMap _funcs = new HashMap<>(); private void encodeFunctions(SymbolSection symbols, DataSection text) { for( int i=0; i<_code._cfg._len; i++ ) { if( !(_code._cfg.at(i) instanceof FunNode fun) ) continue; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java index 1fc124e..b37e8fe 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java @@ -1,7 +1,6 @@ package com.compilerprogramming.ezlang.compiler.codegen; import com.compilerprogramming.ezlang.compiler.Ary; -import com.compilerprogramming.ezlang.compiler.SB; import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.nodes.*; import com.compilerprogramming.ezlang.compiler.sontypes.*; @@ -47,12 +46,12 @@ public static class BAOS extends ByteArrayOutputStream { // Big Constant relocation info. public static class Relo { public final Node _op; - public final SONType _t; // Constant type + public final Type _t; // Constant type public final byte _off; // Offset from start of opcode public final byte _elf; // ELF relocation type, e.g. 2/PC32 public int _target; // Where constant is finally placede public int _opStart; // Opcode start - Relo( Node op, SONType t, byte off, byte elf ) { + Relo(Node op, Type t, byte off, byte elf ) { _op=op; _t=t; _off=off; _elf=elf; } } @@ -99,12 +98,12 @@ static void padN(int n, BAOS bits) { } // Convenience for writing log-N - static void addN( int log, SONType t, BAOS bits ) { - long x = t instanceof SONTypeInteger ti + static void addN(int log, Type t, BAOS bits ) { + long x = t instanceof TypeInteger ti ? ti.value() : log==3 - ? Double.doubleToRawLongBits( ((SONTypeFloat)t).value()) - : Float.floatToRawIntBits((float)((SONTypeFloat)t).value()); + ? Double.doubleToRawLongBits( ((TypeFloat)t).value()) + : Float.floatToRawIntBits((float)((TypeFloat)t).value()); addN(log,x,bits); } static void addN( int log, long x, BAOS bits ) { @@ -129,7 +128,7 @@ public Encoding relo( CallNode call ) { return this; } public Encoding relo( ConstantNode con ) { - SONTypeFunPtr tfp = (SONTypeFunPtr)con._con; + TypeFunPtr tfp = (TypeFunPtr)con._con; _internals.put(con,_code.link(tfp)); return this; } @@ -146,7 +145,7 @@ public Encoding external( Node call, String extern ) { // Store t as a 32/64 bit constant in the code space; generate RIP-relative // addressing to load it - public void largeConstant( Node relo, SONType t, int off, int elf ) { + public void largeConstant(Node relo, Type t, int off, int elf ) { assert t.isConstant(); assert (byte)off == off; assert (byte)elf == elf; @@ -423,7 +422,7 @@ void patchLocalRelocations() { void writeConstantPool( BAOS bits, boolean patch ) { padN(16,bits); - HashMap targets = new HashMap<>(); + HashMap targets = new HashMap<>(); // By log size for( int log = 3; log >= 0; log-- ) { @@ -436,8 +435,8 @@ void writeConstantPool( BAOS bits, boolean patch ) { if( target==null ) { targets.put(relo._t,target = bits.size()); // Put constant into code space. - if( relo._t instanceof SONTypeTuple tt ) // Constant tuples put all entries - for( SONType tx : tt._types ) + if( relo._t instanceof TypeTuple tt ) // Constant tuples put all entries + for( Type tx : tt._types ) addN(log,tx,bits); else addN(log,relo._t,bits); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java index 627e7e0..1611468 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java @@ -166,11 +166,11 @@ private static void breadth(Node stop, Node[] ns, CFGNode[] late) { // New makes new memory, never crushes load memory !(memuse instanceof NewNode) && // Load-use directly defines memory - (memuse._type instanceof SONTypeMem || + (memuse._type instanceof TypeMem || // Load-use directly defines memory memuse instanceof CallNode || // Load-use indirectly defines memory - (memuse._type instanceof SONTypeTuple tt && tt._types[ld._alias] instanceof SONTypeMem)) ) + (memuse._type instanceof TypeTuple tt && tt._types[ld._alias] instanceof TypeMem)) ) continue outer; // All uses done, schedule diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Machine.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Machine.java index 4115e57..3a9d9e9 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Machine.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Machine.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.codegen; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; abstract public class Machine { // Human readable machine name. Something like "x86-64" or "arm" or "risc5" @@ -21,9 +21,9 @@ abstract public class Machine { // for the function itself, or for *outgoing* calls, the maximum stack slot // given to the incoming function arguments (stack slots reserved for // incoming arguments). - public abstract RegMask callArgMask(SONTypeFunPtr tfp, int arg, int maxArgSlot); + public abstract RegMask callArgMask(TypeFunPtr tfp, int arg, int maxArgSlot); // Return register mask, based on signature (GPR vs FPR) - public abstract RegMask retMask(SONTypeFunPtr tfp); + public abstract RegMask retMask(TypeFunPtr tfp); // Return PC register public abstract int rpc(); // Return a MachNode unconditional branch @@ -92,6 +92,6 @@ abstract public class Machine { // Maximum stack slot (or 0) for the args in this TFP. This will include // shadow slots if defined in the ABI, even if all arguments are passed in // registers. - public abstract short maxArgSlot(SONTypeFunPtr tfp); + public abstract short maxArgSlot(TypeFunPtr tfp); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddFNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddFNode.java index d8ecc97..b7ba969 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddFNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddFNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; import java.util.BitSet; @@ -20,11 +20,11 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - if (in(1)._type instanceof SONTypeFloat i0 && - in(2)._type instanceof SONTypeFloat i1) { + public Type compute() { + if (in(1)._type instanceof TypeFloat i0 && + in(2)._type instanceof TypeFloat i1) { if (i0.isConstant() && i1.isConstant()) - return SONTypeFloat.constant(i0.value()+i1.value()); + return TypeFloat.constant(i0.value()+i1.value()); } return in(1)._type.meet(in(2)._type); } @@ -32,10 +32,10 @@ public SONType compute() { @Override public Node idealize() { Node lhs = in(1); - SONType t2 = in(2)._type; + Type t2 = in(2)._type; // Add of 0. - if ( t2.isConstant() && t2 instanceof SONTypeFloat i && i.value()==0 ) + if ( t2.isConstant() && t2 instanceof TypeFloat i && i.value()==0 ) return lhs; return null; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddNode.java index 060fba6..1b8af35 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AddNode.java @@ -1,6 +1,5 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.Compiler; import com.compilerprogramming.ezlang.compiler.sontypes.*; import java.util.BitSet; @@ -23,20 +22,20 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if( t1 instanceof SONTypeInteger i1 && - t2 instanceof SONTypeInteger i2 ) { + return TypeInteger.TOP; + if( t1 instanceof TypeInteger i1 && + t2 instanceof TypeInteger i2 ) { if (i1.isConstant() && i2.isConstant()) - return SONTypeInteger.constant(i1.value()+i2.value()); + return TypeInteger.constant(i1.value()+i2.value()); // Fold ranges like {0-1} + {2-3} into {2-4}. if( !overflow(i1._min,i2._min) && !overflow(i1._max,i2._max) ) - return SONTypeInteger.make(i1._min+i2._min,i1._max+i2._max); + return TypeInteger.make(i1._min+i2._min,i1._max+i2._max); } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } static boolean overflow( long x, long y ) { @@ -49,11 +48,11 @@ public Node idealize () { Node rhs = in(2); if( rhs instanceof AddNode add && add.err()!=null ) return null; - SONType t2 = rhs._type; + Type t2 = rhs._type; // Add of 0. We do not check for (0+x) because this will already // canonicalize to (x+0) - if( t2 == SONTypeInteger.ZERO ) + if( t2 == TypeInteger.ZERO ) return lhs; // Add of same to a multiply by 2 @@ -122,7 +121,7 @@ public Node idealize () { static Node phiCon(Node op, boolean rotate) { Node lhs = op.in(1); Node rhs = op.in(2); - if( rhs._type== SONTypeInteger.TOP ) return null; + if( rhs._type== TypeInteger.TOP ) return null; // LHS is either a Phi of constants, or another op with Phi of constants PhiNode lphi = pcon(lhs,op); if( rotate && lphi==null && lhs.nIns() > 2 ) { @@ -175,8 +174,8 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) { if( lo._type.isConstant() ) return false; if( hi._type.isConstant() ) return true ; - if( lo instanceof PhiNode lphi && lphi.region()._type== SONType.XCONTROL ) return false; - if( hi instanceof PhiNode hphi && hphi.region()._type== SONType.XCONTROL ) return false; + if( lo instanceof PhiNode lphi && lphi.region()._type== Type.XCONTROL ) return false; + if( hi instanceof PhiNode hphi && hphi.region()._type== Type.XCONTROL ) return false; if( lo instanceof PhiNode && lo.allCons(dep) ) return false; if( hi instanceof PhiNode && hi.allCons(dep) ) return true ; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java index f36997f..fb35217 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; public class AndNode extends LogicalNode { public AndNode(Node lhs, Node rhs) { super(lhs, rhs); } @@ -12,28 +12,28 @@ public class AndNode extends LogicalNode { @Override public String glabel() { return "&"; } @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if( t1 instanceof SONTypeInteger i0 && - t2 instanceof SONTypeInteger i1 ) { + return TypeInteger.TOP; + if( t1 instanceof TypeInteger i0 && + t2 instanceof TypeInteger i1 ) { if( i0.isConstant() && i1.isConstant() ) - return SONTypeInteger.constant(i0.value()&i1.value()); + return TypeInteger.constant(i0.value()&i1.value()); // Sharpen allowed bits if either value is narrowed long mask = i0.mask() & i1.mask(); - return mask < 0 ? SONTypeInteger.BOT : SONTypeInteger.make(0,mask); + return mask < 0 ? TypeInteger.BOT : TypeInteger.make(0,mask); } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } @Override public Node idealize() { Node lhs = in(1); Node rhs = in(2); - SONType t1 = lhs._type; - SONType t2 = rhs._type; - if( !(t1 instanceof SONTypeInteger && t2 instanceof SONTypeInteger t2i) ) + Type t1 = lhs._type; + Type t2 = rhs._type; + if( !(t1 instanceof TypeInteger && t2 instanceof TypeInteger t2i) ) return null; // Malformed, e.g. (17 & 3.14) // And of -1. We do not check for (-1&x) because this will already diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/BoolNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/BoolNode.java index 8863de8..3248345 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/BoolNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/BoolNode.java @@ -4,7 +4,7 @@ import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.sontypes.*; import java.util.BitSet; -import static com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger.*; +import static com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger.*; abstract public class BoolNode extends Node { @@ -28,26 +28,26 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONTypeInteger compute() { - SONType t1 = in(1)._type; - SONType t2 = in(2)._type; + public TypeInteger compute() { + Type t1 = in(1)._type; + Type t2 = in(2)._type; // Exactly equals? if( t1.isHigh() || t2.isHigh() ) return BOOL.dual(); if( in(1)==in(2) ) // LT fails, both EQ and LE succeed return this instanceof LT ? FALSE : TRUE; - if( t1 instanceof SONTypeInteger i1 && - t2 instanceof SONTypeInteger i2 ) + if( t1 instanceof TypeInteger i1 && + t2 instanceof TypeInteger i2 ) return doOp(i1,i2); - if( t1 instanceof SONTypeFloat f1 && - t2 instanceof SONTypeFloat f2 && + if( t1 instanceof TypeFloat f1 && + t2 instanceof TypeFloat f2 && f1.isConstant() && f2.isConstant() ) return doOp(f1.value(), f2.value()) ? TRUE : FALSE; return BOOL; } - SONTypeInteger doOp(SONTypeInteger t1, SONTypeInteger t2) { throw Utils.TODO(); } + TypeInteger doOp(TypeInteger t1, TypeInteger t2) { throw Utils.TODO(); } boolean doOp(double lhs, double rhs) { throw Utils.TODO(); } Node copyF(Node lhs, Node rhs) { return null; } public boolean isFloat() { return false; } @@ -64,10 +64,10 @@ public Node idealize() { // con==noncon becomes noncon==con if( in(1) instanceof ConstantNode || in(1)._nid > in(2)._nid ) // Equals sorts by NID otherwise: non.high == non.low becomes non.low == non.high - return in(1)._type instanceof SONTypeFloat ? new EQF(in(2),in(1)) : new EQ(in(2),in(1)); + return in(1)._type instanceof TypeFloat ? new EQF(in(2),in(1)) : new EQ(in(2),in(1)); } // Equals X==0 becomes a !X - if( (in(2)._type == ZERO || in(2)._type == SONType.NIL) ) + if( (in(2)._type == ZERO || in(2)._type == Type.NIL) ) return new NotNode(in(1)); } @@ -83,7 +83,7 @@ public Node idealize() { public static class EQ extends BoolNode { public EQ(Node lhs, Node rhs) { super(lhs,rhs); } public String op() { return "=="; } - SONTypeInteger doOp(SONTypeInteger i1, SONTypeInteger i2) { + TypeInteger doOp(TypeInteger i1, TypeInteger i2) { if( i1==i2 && i1.isConstant() ) return TRUE; if( i1._max < i2._min || i1._min > i2._max ) return FALSE; return BOOL; @@ -95,7 +95,7 @@ SONTypeInteger doOp(SONTypeInteger i1, SONTypeInteger i2) { public static class NE extends BoolNode { public NE(Node lhs, Node rhs) { super(lhs,rhs); } public String op() { return "!="; } - SONTypeInteger doOp(SONTypeInteger i1, SONTypeInteger i2) { + TypeInteger doOp(TypeInteger i1, TypeInteger i2) { if( i1==i2 && i1.isConstant() ) return TRUE; if( i1._max < i2._min || i1._min > i2._max ) return FALSE; return BOOL; @@ -108,7 +108,7 @@ public static class LT extends BoolNode { public LT(Node lhs, Node rhs) { super(lhs,rhs); } public String op() { return "<" ; } public String glabel() { return "<"; } - SONTypeInteger doOp(SONTypeInteger i1, SONTypeInteger i2) { + TypeInteger doOp(TypeInteger i1, TypeInteger i2) { if( i1._max < i2._min ) return TRUE; if( i1._min >= i2._max ) return FALSE; return BOOL; @@ -120,7 +120,7 @@ public static class LE extends BoolNode { public LE(Node lhs, Node rhs) { super(lhs,rhs); } public String op() { return "<="; } public String glabel() { return "<="; } - SONTypeInteger doOp(SONTypeInteger i1, SONTypeInteger i2) { + TypeInteger doOp(TypeInteger i1, TypeInteger i2) { if( i1._max <= i2._min ) return TRUE; if( i1._min > i2._max ) return FALSE; return BOOL; @@ -138,7 +138,7 @@ public static class ULT extends BoolNode { public ULT(Node lhs, Node rhs) { super(lhs,rhs); } public String op() { return "u<" ; } public String glabel() { return "u<"; } - SONTypeInteger doOp(SONTypeInteger i1, SONTypeInteger i2) { + TypeInteger doOp(TypeInteger i1, TypeInteger i2) { if( Long.compareUnsigned(i1._max,i2._min) < 0 ) return TRUE; if( Long.compareUnsigned(i1._min,i2._max) >= 0 ) return FALSE; return BOOL; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CFGNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CFGNode.java index cda5c80..afd1622 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CFGNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CFGNode.java @@ -140,8 +140,8 @@ int _bltWalk( int pre, FunNode fun, StopNode stop, BitSet post ) { for( Node use : _outputs ) { if( !(use instanceof CFGNode usecfg) ) continue; if( skip(usecfg) ) continue; - if( usecfg._type == SONType.XCONTROL || // Do not walk dead control - usecfg._type == SONTypeTuple.IF_NEITHER ) // Nor dead IFs + if( usecfg._type == Type.XCONTROL || // Do not walk dead control + usecfg._type == TypeTuple.IF_NEITHER ) // Nor dead IFs continue; // Child visited but not post-visited? if( !post.get(usecfg._nid) ) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CProjNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CProjNode.java index 9c18492..1aec013 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CProjNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CProjNode.java @@ -1,8 +1,8 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeTuple; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeTuple; import java.util.BitSet; public class CProjNode extends CFGNode { @@ -29,17 +29,17 @@ public CProjNode(Node ctrl, int idx, String label) { public CFGNode ctrl() { return cfg(0); } @Override - public SONType compute() { - SONType t = ctrl()._type; - return t instanceof SONTypeTuple tt ? tt._types[_idx] : SONType.BOTTOM; + public Type compute() { + Type t = ctrl()._type; + return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM; } @Override public Node idealize() { - if( ctrl()._type instanceof SONTypeTuple tt ) { - if( tt._types[_idx]== SONType.XCONTROL ) + if( ctrl()._type instanceof TypeTuple tt ) { + if( tt._types[_idx]== Type.XCONTROL ) return Compiler.XCTRL; // We are dead - if( ctrl() instanceof IfNode && tt._types[1-_idx]== SONType.XCONTROL ) // Only true for IfNodes + if( ctrl() instanceof IfNode && tt._types[1-_idx]== Type.XCONTROL ) // Only true for IfNodes return ctrl().in(0); // We become our input control } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallEndNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallEndNode.java index f2db2a9..79a1c03 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallEndNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallEndNode.java @@ -16,9 +16,9 @@ public class CallEndNode extends CFGNode implements MultiNode { // When set true, this Call/CallEnd/Fun/Return is being trivially inlined private boolean _folding; - public final SONTypeRPC _rpc; + public final TypeRPC _rpc; - public CallEndNode(CallNode call) { super(new Node[]{call}); _rpc = SONTypeRPC.constant(_nid); } + public CallEndNode(CallNode call) { super(new Node[]{call}); _rpc = TypeRPC.constant(_nid); } public CallEndNode(CallEndNode cend) { super(cend); _rpc = cend._rpc; } @Override public String label() { return "CallEnd"; } @@ -37,21 +37,21 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { + public Type compute() { if( !(in(0) instanceof CallNode call) ) - return SONTypeTuple.RET.dual(); - SONType ret = SONType.BOTTOM; - SONTypeMem mem = SONTypeMem.BOT; - if( addDep(call.fptr())._type instanceof SONTypeFunPtr tfp ) { + return TypeTuple.RET.dual(); + Type ret = Type.BOTTOM; + TypeMem mem = TypeMem.BOT; + if( addDep(call.fptr())._type instanceof TypeFunPtr tfp ) { ret = tfp.ret(); // Here, if I can figure out I've found *all* callers, then I can meet // across the linked returns and join with the function return type. if( tfp.isConstant() && nIns()>1 ) { assert nIns()==2; // Linked exactly once for a constant - ret = ((SONTypeTuple)in(1)._type).ret(); // Return type + ret = ((TypeTuple)in(1)._type).ret(); // Return type } } - return SONTypeTuple.make(call._type, SONTypeMem.BOT,ret); + return TypeTuple.make(call._type, TypeMem.BOT,ret); } @Override @@ -64,7 +64,7 @@ public Node idealize() { if( fptr.nOuts() == 1 && // Only user is this call fptr instanceof ConstantNode && // We have an immediate call // Function is being called, and its not-null - fptr._type instanceof SONTypeFunPtr tfp && tfp.notNull() && + fptr._type instanceof TypeFunPtr tfp && tfp.notNull() && // Arguments are correct call.err()==null ) { ReturnNode ret = (ReturnNode)in(1); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallNode.java index 5f88fd4..78c0632 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallNode.java @@ -1,11 +1,10 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; -import com.compilerprogramming.ezlang.compiler.IterPeeps; import com.compilerprogramming.ezlang.compiler.Compiler; import com.compilerprogramming.ezlang.compiler.Utils; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; import com.compilerprogramming.ezlang.exceptions.CompilerException; import java.util.BitSet; @@ -31,7 +30,7 @@ public class CallNode extends CFGNode { return sb.append(")"); } public String name() { - if( fptr()._type instanceof SONTypeFunPtr tfp && tfp.isConstant() ) + if( fptr()._type instanceof TypeFunPtr tfp && tfp.isConstant() ) return CodeGen.CODE.link(tfp)._name; return null; } @@ -47,7 +46,7 @@ public String name() { // args from input 2 to last; last is function input public Node fptr() { return _inputs.last(); } // Error if not a TFP - public SONTypeFunPtr tfp() { return (SONTypeFunPtr)fptr()._type; } + public TypeFunPtr tfp() { return (TypeFunPtr)fptr()._type; } // Call is to an externally supplied code public boolean external() { return false; } @@ -78,7 +77,7 @@ private CallEndNode _cend() { @Override - public SONType compute() { + public Type compute() { return ctrl()._type; } @@ -91,15 +90,15 @@ public Node idealize() { // point to this Call, and all his Parms point to the call arguments; // also the CallEnd points to the Return. Node progress = null; - if( fptr()._type instanceof SONTypeFunPtr tfp && tfp.nargs() == nargs() ) { + if( fptr()._type instanceof TypeFunPtr tfp && tfp.nargs() == nargs() ) { // If fidxs is negative, then infinite unknown functions long fidxs = tfp.fidxs(); if( fidxs > 0 ) { // Wipe out the return which matching in the linker table // Walk the (63 max) bits and link - for( ; fidxs!=0; fidxs = SONTypeFunPtr.nextFIDX(fidxs) ) { + for( ; fidxs!=0; fidxs = TypeFunPtr.nextFIDX(fidxs) ) { int fidx = Long.numberOfTrailingZeros(fidxs); - SONTypeFunPtr tfp0 = tfp.makeFrom(fidx); + TypeFunPtr tfp0 = tfp.makeFrom(fidx); FunNode fun = CodeGen.CODE.link(tfp0); if( fun!=null && !fun._folding && !linked(fun) ) progress = link(fun); @@ -149,7 +148,7 @@ public void unlink_all() { @Override public CompilerException err() { - if( !(fptr()._type instanceof SONTypeFunPtr tfp) ) + if( !(fptr()._type instanceof TypeFunPtr tfp) ) throw Utils.TODO(); if( !tfp.notNull() ) return Compiler.error( "Might be null calling "+tfp); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CalleeSaveNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CalleeSaveNode.java index a003903..bf6461b 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CalleeSaveNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CalleeSaveNode.java @@ -1,11 +1,8 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.codegen.Encoding; import com.compilerprogramming.ezlang.compiler.codegen.RegMask; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import java.util.BitSet; -import java.io.ByteArrayOutputStream; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; public class CalleeSaveNode extends ProjNode implements MachNode { final RegMask _mask; @@ -21,6 +18,6 @@ public CalleeSaveNode(FunNode fun, int reg, String label) { @Override public RegMask outregmap() { return _mask; } @Override public void encoding( Encoding enc ) { } - @Override public SONType compute() { return SONType.BOTTOM; } + @Override public Type compute() { return Type.BOTTOM; } @Override public Node idealize() { return null; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CastNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CastNode.java index 58de87f..7645f39 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CastNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CastNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.Utils; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import com.compilerprogramming.ezlang.exceptions.CompilerException; import java.util.BitSet; @@ -9,8 +9,8 @@ // Upcast (join) the input to a t. Used after guard test to lift an input. // Can also be used to make a type-assertion if ctrl is null. public class CastNode extends Node { - public SONType _t; - public CastNode(SONType t, Node ctrl, Node in) { + public Type _t; + public CastNode(Type t, Node ctrl, Node in) { super(ctrl, in); _t = t; setType(compute()); @@ -36,7 +36,7 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { + public Type compute() { return in(1)._type.join(_t); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ConstantNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ConstantNode.java index 4eb1798..407ab01 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ConstantNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ConstantNode.java @@ -2,8 +2,8 @@ import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; import com.compilerprogramming.ezlang.compiler.SB; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; import java.util.BitSet; /** @@ -20,17 +20,17 @@ */ public class ConstantNode extends Node { - public final SONType _con; - public ConstantNode( SONType type ) { + public final Type _con; + public ConstantNode( Type type ) { super(new Node[]{CodeGen.CODE._start}); _con = _type = type; } - public ConstantNode( Node con, SONType t ) { super(con); _con = t; } + public ConstantNode( Node con, Type t ) { super(con); _con = t; } public ConstantNode( ConstantNode con ) { this(con,con._type); } - public static Node make( SONType type ) { - if( type== SONType. CONTROL ) return new CtrlNode(); - if( type== SONType.XCONTROL ) return new XCtrlNode(); + public static Node make( Type type ) { + if( type== Type. CONTROL ) return new CtrlNode(); + if( type== Type.XCONTROL ) return new XCtrlNode(); return new ConstantNode(type); } @@ -42,7 +42,7 @@ public static Node make( SONType type ) { @Override public StringBuilder _print1(StringBuilder sb, BitSet visited) { - if( _con instanceof SONTypeFunPtr tfp && tfp.isConstant() ) { + if( _con instanceof TypeFunPtr tfp && tfp.isConstant() ) { FunNode fun = CodeGen.CODE.link(tfp); if( fun!=null && fun._name != null ) return sb.append("{ ").append(fun._name).append("}"); @@ -53,7 +53,7 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { @Override public boolean isConst() { return true; } @Override - public SONType compute() { return _con; } + public Type compute() { return _con; } @Override public Node idealize() { return null; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CtrlNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CtrlNode.java index 6450cdd..6131eef 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CtrlNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CtrlNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import java.util.BitSet; public class CtrlNode extends CFGNode { @@ -9,6 +9,6 @@ public class CtrlNode extends CFGNode { @Override public String label() { return "Ctrl"; } @Override public StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append("Cctrl"); } @Override public boolean isConst() { return true; } - @Override public SONType compute() { return SONType.CONTROL; } + @Override public Type compute() { return Type.CONTROL; } @Override public Node idealize() { return null; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivFNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivFNode.java index 0813fa8..bbfcccf 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivFNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivFNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; import java.util.BitSet; @@ -20,11 +20,11 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - if (in(1)._type instanceof SONTypeFloat i0 && - in(2)._type instanceof SONTypeFloat i1) { + public Type compute() { + if (in(1)._type instanceof TypeFloat i0 && + in(2)._type instanceof TypeFloat i1) { if (i0.isConstant() && i1.isConstant()) - return SONTypeFloat.constant(i0.value()/i1.value()); + return TypeFloat.constant(i0.value()/i1.value()); } return in(1)._type.meet(in(2)._type); } @@ -32,8 +32,8 @@ public SONType compute() { @Override public Node idealize() { // Div of constant - if( in(2)._type instanceof SONTypeFloat f && f.isConstant() ) - return new MulFNode(in(1),new ConstantNode(SONTypeFloat.constant(1.0/f.value())).peephole()); + if( in(2)._type instanceof TypeFloat f && f.isConstant() ) + return new MulFNode(in(1),new ConstantNode(TypeFloat.constant(1.0/f.value())).peephole()); return null; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivNode.java index 9c51b6e..34d6eb2 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/DivNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; import java.util.BitSet; public class DivNode extends Node { @@ -19,24 +19,24 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if( t1 instanceof SONTypeInteger i1 && - t2 instanceof SONTypeInteger i2 ) { + return TypeInteger.TOP; + if( t1 instanceof TypeInteger i1 && + t2 instanceof TypeInteger i2 ) { if (i1.isConstant() && i2.isConstant()) return i2.value() == 0 - ? SONTypeInteger.ZERO - : SONTypeInteger.constant(i1.value()/i2.value()); + ? TypeInteger.ZERO + : TypeInteger.constant(i1.value()/i2.value()); } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } @Override public Node idealize() { // Div of 1. - if( in(2)._type == SONTypeInteger.TRUE ) + if( in(2)._type == TypeInteger.TRUE ) return in(1); return null; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FRefNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FRefNode.java index 3b4a282..0b48d15 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FRefNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FRefNode.java @@ -2,7 +2,7 @@ import com.compilerprogramming.ezlang.compiler.Compiler; import com.compilerprogramming.ezlang.compiler.Var; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import com.compilerprogramming.ezlang.exceptions.CompilerException; import java.util.BitSet; @@ -13,7 +13,7 @@ * peepholes to the Def. */ public class FRefNode extends ConstantNode { - public static final SONType FREF_TYPE = SONType.BOTTOM; + public static final Type FREF_TYPE = Type.BOTTOM; public final Var _n; public FRefNode( Var n ) { super(FREF_TYPE); _n = n; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FunNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FunNode.java index 7e12aaf..f7de008 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FunNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/FunNode.java @@ -4,9 +4,9 @@ import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; import com.compilerprogramming.ezlang.compiler.codegen.Encoding; import com.compilerprogramming.ezlang.compiler.codegen.RegMask; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeTuple; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeTuple; import java.util.BitSet; import static com.compilerprogramming.ezlang.compiler.codegen.CodeGen.CODE; @@ -16,19 +16,19 @@ public class FunNode extends RegionNode { // When set true, this Call/CallEnd/Fun/Return is being trivially inlined boolean _folding; - private SONTypeFunPtr _sig; // Initial signature + private TypeFunPtr _sig; // Initial signature private ReturnNode _ret; // Return pointer public String _name; // Debug name - public FunNode(SONTypeFunPtr sig, Node... nodes ) { super(nodes); _sig = sig; } + public FunNode(TypeFunPtr sig, Node... nodes ) { super(nodes); _sig = sig; } public FunNode( FunNode fun ) { super( fun ); if( fun!=null ) { _sig = fun.sig(); _name = fun._name; } else { - _sig = SONTypeFunPtr.BOT; + _sig = TypeFunPtr.BOT; _name = ""; } } @@ -60,8 +60,8 @@ public ParmNode rpc() { public ReturnNode ret() { assert _ret!=null; return _ret; } // Signature can improve over time - public SONTypeFunPtr sig() { return _sig; } - public void setSig( SONTypeFunPtr sig ) { + public TypeFunPtr sig() { return _sig; } + public void setSig( TypeFunPtr sig ) { assert sig.isa(_sig); if( _sig != sig ) { CODE.add(this); @@ -70,9 +70,9 @@ public void setSig( SONTypeFunPtr sig ) { } @Override - public SONType compute() { + public Type compute() { // Only dead if no callers after SCCP - return SONType.CONTROL; + return Type.CONTROL; } @Override @@ -87,7 +87,7 @@ public Node idealize() { } // Upgrade inferred or user-written return type to actual - if( _ret!=null && _ret._type instanceof SONTypeTuple tt && tt.ret() != _sig.ret() ) + if( _ret!=null && _ret._type instanceof TypeTuple tt && tt.ret() != _sig.ret() ) // FIXME Dibyendu //throw Utils.TODO(); return null; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/IfNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/IfNode.java index 862c8b6..5cc6a2e 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/IfNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/IfNode.java @@ -2,7 +2,6 @@ import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; -import com.compilerprogramming.ezlang.compiler.IterPeeps; import com.compilerprogramming.ezlang.compiler.sontypes.*; import java.util.BitSet; @@ -32,25 +31,25 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { @Override public CFGNode uctrl() { return null; } @Override - public SONType compute() { + public Type compute() { // If the If node is not reachable then neither is any following Proj - if (ctrl()._type != SONType.CONTROL && ctrl()._type != SONType.BOTTOM ) - return SONTypeTuple.IF_NEITHER; + if (ctrl()._type != Type.CONTROL && ctrl()._type != Type.BOTTOM ) + return TypeTuple.IF_NEITHER; Node pred = pred(); - SONType t = pred._type; + Type t = pred._type; // High types mean NEITHER side is reachable. // Wait until the type falls to decide which way to go. if( t.isHigh() ) - return SONTypeTuple.IF_NEITHER; + return TypeTuple.IF_NEITHER; // If constant is 0 then false branch is reachable // Else true branch is reachable if( t.isConstant() ) - return (t== SONType.NIL || t== SONTypeInteger.ZERO || (t instanceof SONTypeFunPtr tfp && tfp._fidxs==0) ) ? SONTypeTuple.IF_FALSE : SONTypeTuple.IF_TRUE; + return (t== Type.NIL || t== TypeInteger.ZERO || (t instanceof TypeFunPtr tfp && tfp._fidxs==0) ) ? TypeTuple.IF_FALSE : TypeTuple.IF_TRUE; // If adding a zero makes a difference, the predicate must not have a zero/null if( !t.makeZero().isa(t) ) - return SONTypeTuple.IF_TRUE; + return TypeTuple.IF_TRUE; - return SONTypeTuple.IF_BOTH; + return TypeTuple.IF_BOTH; } @Override diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoadNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoadNode.java index 4607e8e..28842c2 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoadNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoadNode.java @@ -18,7 +18,7 @@ public class LoadNode extends MemOpNode { * @param ptr The ptr to the struct base from where we load a field * @param off The offset inside the struct base */ - public LoadNode(String name, int alias, SONType glb, Node mem, Node ptr, Node off) { + public LoadNode(String name, int alias, Type glb, Node mem, Node ptr, Node off) { super(name, alias, true, glb, mem, ptr, off); } @@ -31,10 +31,10 @@ public LoadNode(String name, int alias, SONType glb, Node mem, Node ptr, Node of public StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(".").append(_name); } @Override - public SONType compute() { - if( mem()._type instanceof SONTypeMem mem ) { + public Type compute() { + if( mem()._type instanceof TypeMem mem ) { // Update declared forward ref to the actual - if( _declaredType.isFRef() && mem._t instanceof SONTypeMemPtr tmp && !tmp.isFRef() ) + if( _declaredType.isFRef() && mem._t instanceof TypeMemPtr tmp && !tmp.isFRef() ) _declaredType = tmp; // No lifting if ptr might null-check if( err()==null ) @@ -118,7 +118,7 @@ public Node idealize() { // if( pred ) ptr.x = e0; val = pred ? e0 // else ptr.x = e1; : e1; // val = ptr.x; ptr.x = val; - if( mem() instanceof PhiNode memphi && memphi.region()._type == SONType.CONTROL && memphi.nIns()== 3 && + if( mem() instanceof PhiNode memphi && memphi.region()._type == Type.CONTROL && memphi.nIns()== 3 && // Offset can be hoisted off() instanceof ConstantNode && // Pointer can be hoisted @@ -139,8 +139,8 @@ public Node idealize() { // Load a flavored zero from a New private Node zero(NewNode nnn) { - SONTypeStruct ts = nnn._ptr._obj; - SONType zero = ts._fields[ts.findAlias(_alias)]._type.makeZero(); + TypeStruct ts = nnn._ptr._obj; + Type zero = ts._fields[ts.findAlias(_alias)]._type.makeZero(); return castRO(new ConstantNode(zero).peephole()); } @@ -179,7 +179,7 @@ private static boolean hoistPtr(Node ptr, PhiNode memphi ) { private boolean profit(PhiNode phi, int idx) { Node px = phi.in(idx); if( px==null ) return false; - if( px._type instanceof SONTypeMem mem && mem._t.isHighOrConst() ) return true; + if( px._type instanceof TypeMem mem && mem._t.isHighOrConst() ) return true; if( px instanceof StoreNode st1 && ptr()==st1.ptr() && off()==st1.off() ) return true; addDep(px); return false; @@ -195,13 +195,13 @@ private Node castRO(Node rez) { // When a load bypasses a store, the store might truncate bits - and the // load will need to zero/sign-extend. private Node extend(Node val) { - if( !(_declaredType instanceof SONTypeInteger ti) ) return val; + if( !(_declaredType instanceof TypeInteger ti) ) return val; if( ti._min==0 ) // Unsigned return new AndNode(val,con(ti._max)); // Signed extension int shift = Long.numberOfLeadingZeros(ti._max)-1; Node shf = con(shift); - if( shf._type== SONTypeInteger.ZERO ) + if( shf._type== TypeInteger.ZERO ) return val; Node shl = new ShlNode(val,shf.keep()).peephole(); return new SarNode(shl,shf.unkeep()); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LogicalNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LogicalNode.java index 1b93813..e7c3450 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LogicalNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LogicalNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; import com.compilerprogramming.ezlang.exceptions.CompilerException; import java.util.BitSet; @@ -21,8 +21,8 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { @Override public CompilerException err() { if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null; - if( !(in(1)._type instanceof SONTypeInteger) ) return Compiler.error("Cannot '"+op()+"' " + in(1)._type); - if( !(in(2)._type instanceof SONTypeInteger) ) return Compiler.error("Cannot '"+op()+"' " + in(2)._type); + if( !(in(1)._type instanceof TypeInteger) ) return Compiler.error("Cannot '"+op()+"' " + in(1)._type); + if( !(in(2)._type instanceof TypeInteger) ) return Compiler.error("Cannot '"+op()+"' " + in(2)._type); return null; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoopNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoopNode.java index a912ba8..87c1a03 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoopNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LoopNode.java @@ -1,8 +1,8 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeMem; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeMem; public class LoopNode extends RegionNode { public LoopNode( Node entry ) { super(null,entry,null); } @@ -15,8 +15,8 @@ public class LoopNode extends RegionNode { public String label() { return "Loop"; } @Override - public SONType compute() { - if( inProgress() ) return SONType.CONTROL; + public Type compute() { + if( inProgress() ) return Type.CONTROL; return entry()._type; } @@ -53,15 +53,15 @@ public StopNode forceExit( FunNode fun, StopNode stop ) { // Now fold control into the exit. Might have 1 valid exit, or an // XCtrl or a bunch of prior NeverNode exits. - Node top = new ConstantNode(SONType.TOP).peephole(); + Node top = new ConstantNode(Type.TOP).peephole(); Node memout = new MemMergeNode(false); memout.addDef(f); // placeholder for control for( Node u : _outputs ) - if( u instanceof PhiNode phi && phi._type.isa(SONTypeMem.BOT) ) + if( u instanceof PhiNode phi && phi._type.isa(TypeMem.BOT) ) memout.addDef(phi); Node ctrl = ret.ctrl(), mem = ret.mem(), expr = ret.expr(); - if( ctrl!=null && ctrl._type != SONType.XCONTROL ) { + if( ctrl!=null && ctrl._type != Type.XCONTROL ) { // Perfect aligned exit? if( !(ctrl instanceof RegionNode r && mem instanceof PhiNode pmem && pmem.region()==r && diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MachConcreteNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MachConcreteNode.java index 82aa6e0..f83f429 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MachConcreteNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MachConcreteNode.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import com.compilerprogramming.ezlang.compiler.Utils; import java.util.BitSet; @@ -13,7 +13,7 @@ public abstract class MachConcreteNode extends Node implements MachNode { public MachConcreteNode(Node[]nodes) { super(nodes); } @Override public String label() { return op(); } - @Override public SONType compute () { throw Utils.TODO(); } + @Override public Type compute () { throw Utils.TODO(); } @Override public Node idealize() { throw Utils.TODO(); } @Override public StringBuilder _print1(StringBuilder sb, BitSet visited) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemMergeNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemMergeNode.java index 6a36e74..8de4ab8 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemMergeNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemMergeNode.java @@ -18,7 +18,7 @@ public class MemMergeNode extends Node { */ public final boolean _inProgress; - public MemMergeNode( boolean inProgress) { _type = SONTypeMem.BOT; _inProgress = inProgress; } + public MemMergeNode( boolean inProgress) { _type = TypeMem.BOT; _inProgress = inProgress; } public MemMergeNode(MemMergeNode mem) { super(mem); _inProgress = false; } @@ -59,7 +59,7 @@ public Node merge() { } - @Override public SONType compute() { return SONTypeMem.BOT; } + @Override public Type compute() { return TypeMem.BOT; } @Override public Node idealize() { if( inProgress() ) return null; @@ -106,7 +106,7 @@ Node _mem( int alias, Node st ) { // Set real Phi in the loop head // The phi takes its one input (no backedge yet) from a recursive // lookup, which might have insert a Phi in every loop nest. - : loopmem.alias(alias, new PhiNode(Compiler.memName(alias), SONTypeMem.BOT,loop.ctrl(),loopmem._mem(alias,null),null).peephole() ); + : loopmem.alias(alias, new PhiNode(Compiler.memName(alias), TypeMem.BOT,loop.ctrl(),loopmem._mem(alias,null),null).peephole() ); alias(alias,old); } // Memory projections are made lazily; expand as needed diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemOpNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemOpNode.java index 0bf695d..87c1c2e 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemOpNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MemOpNode.java @@ -2,8 +2,8 @@ import com.compilerprogramming.ezlang.compiler.Compiler; import com.compilerprogramming.ezlang.compiler.Utils; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeMemPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeMemPtr; import com.compilerprogramming.ezlang.exceptions.CompilerException; import java.lang.StringBuilder; @@ -37,18 +37,18 @@ public abstract class MemOpNode extends Node { // Declared type; not final because it might be a forward-reference // which will be lazily improved when the reference is declared. - public SONType _declaredType; + public Type _declaredType; // A debug name, no semantic meaning public final String _name; - public MemOpNode(String name, int alias, boolean isLoad, SONType glb, Node mem, Node ptr, Node off) { + public MemOpNode(String name, int alias, boolean isLoad, Type glb, Node mem, Node ptr, Node off) { super(null, mem, ptr, off); _name = name; _alias = alias; _declaredType = glb; _isLoad = isLoad; } - public MemOpNode(String name, int alias, boolean isLoad, SONType glb, Node mem, Node ptr, Node off, Node value) { + public MemOpNode(String name, int alias, boolean isLoad, Type glb, Node mem, Node ptr, Node off, Node value) { this(name, alias, isLoad, glb, mem, ptr, off); addDef(value); } @@ -57,7 +57,7 @@ public MemOpNode( Node mach, MemOpNode mop ) { _name = mop==null ? null : mop._name; _alias = mop==null ? 0 : mop._alias; _isLoad= mop==null ? true : mop._isLoad; - _declaredType = mop==null ? SONType.BOTTOM : mop._declaredType; + _declaredType = mop==null ? Type.BOTTOM : mop._declaredType; if( mop==null ) throw Utils.TODO("Load or not"); } @@ -69,7 +69,7 @@ public MemOpNode( boolean isLoad ) { _alias = 0; _isLoad = isLoad; - _declaredType = SONType.BOTTOM; + _declaredType = Type.BOTTOM; } // @@ -95,12 +95,12 @@ public boolean eq(Node n) { @Override public CompilerException err() { - SONType ptr = ptr()._type; + Type ptr = ptr()._type; // Already an error, but better error messages come from elsewhere - if( ptr == SONType.BOTTOM ) return null; + if( ptr == Type.BOTTOM ) return null; if( ptr.isHigh() ) return null; // Assume it will fall to not-null // Better be a not-nil TMP - if( ptr instanceof SONTypeMemPtr tmp && tmp.notNull() ) + if( ptr instanceof TypeMemPtr tmp && tmp.notNull() ) return null; return Compiler.error( "Might be null accessing '" + _name + "'"); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusFNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusFNode.java index aa93f64..6a6a3f0 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusFNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusFNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; import java.util.BitSet; @@ -19,10 +19,10 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - if (in(1)._type instanceof SONTypeFloat i0) - return i0.isConstant() ? SONTypeFloat.constant(-i0.value()) : i0; - return SONTypeFloat.F64; + public Type compute() { + if (in(1)._type instanceof TypeFloat i0) + return i0.isConstant() ? TypeFloat.constant(-i0.value()) : i0; + return TypeFloat.F64; } @Override diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusNode.java index 6bb92e1..8b72814 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MinusNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; import java.util.BitSet; @@ -19,12 +19,12 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - if( in(1)._type.isHigh() ) return SONTypeInteger.TOP; - if( in(1)._type instanceof SONTypeInteger i0 && - !(i0== SONTypeInteger.BOT || i0._min == Long.MIN_VALUE || i0._max == Long.MIN_VALUE) ) - return SONTypeInteger.make(-i0._max,-i0._min); - return SONTypeInteger.BOT; + public Type compute() { + if( in(1)._type.isHigh() ) return TypeInteger.TOP; + if( in(1)._type instanceof TypeInteger i0 && + !(i0== TypeInteger.BOT || i0._min == Long.MIN_VALUE || i0._max == Long.MIN_VALUE) ) + return TypeInteger.make(-i0._max,-i0._min); + return TypeInteger.BOT; } @Override diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulFNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulFNode.java index d0f0f37..5890f10 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulFNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulFNode.java @@ -1,7 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; import java.util.BitSet; @@ -20,11 +20,11 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - if (in(1)._type instanceof SONTypeFloat i0 && - in(2)._type instanceof SONTypeFloat i1) { + public Type compute() { + if (in(1)._type instanceof TypeFloat i0 && + in(2)._type instanceof TypeFloat i1) { if (i0.isConstant() && i1.isConstant()) - return SONTypeFloat.constant(i0.value()*i1.value()); + return TypeFloat.constant(i0.value()*i1.value()); } return in(1)._type.meet(in(2)._type); } @@ -33,12 +33,12 @@ public SONType compute() { public Node idealize() { Node lhs = in(1); Node rhs = in(2); - SONType t1 = lhs._type; - SONType t2 = rhs._type; + Type t1 = lhs._type; + Type t2 = rhs._type; // Mul of 1. We do not check for (1*x) because this will already // canonicalize to (x*1) - if ( t2.isConstant() && t2 instanceof SONTypeFloat i && i.value()==1 ) + if ( t2.isConstant() && t2 instanceof TypeFloat i && i.value()==1 ) return lhs; // Move constants to RHS: con*arg becomes arg*con diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulNode.java index e35b172..81f378c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/MulNode.java @@ -1,8 +1,8 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; import com.compilerprogramming.ezlang.exceptions.CompilerException; import java.util.BitSet; @@ -22,33 +22,33 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if( t1 instanceof SONTypeInteger i1 && - t2 instanceof SONTypeInteger i2 ) { - if( i1== SONTypeInteger.ZERO || i2== SONTypeInteger.ZERO) - return SONTypeInteger.ZERO; + return TypeInteger.TOP; + if( t1 instanceof TypeInteger i1 && + t2 instanceof TypeInteger i2 ) { + if( i1== TypeInteger.ZERO || i2== TypeInteger.ZERO) + return TypeInteger.ZERO; if (i1.isConstant() && i2.isConstant()) - return SONTypeInteger.constant(i1.value()*i2.value()); + return TypeInteger.constant(i1.value()*i2.value()); } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } @Override public Node idealize() { Node lhs = in(1); Node rhs = in(2); - SONType t1 = lhs._type; - SONType t2 = rhs._type; + Type t1 = lhs._type; + Type t2 = rhs._type; // Move constants to RHS: con*arg becomes arg*con if ( t1.isConstant() && !t2.isConstant() ) return swap12(); // Multiply by constant - if ( t2.isConstant() && t2 instanceof SONTypeInteger i ) { + if ( t2.isConstant() && t2 instanceof TypeInteger i ) { // Mul of 1. We do not check for (1*x) because this will already // canonicalize to (x*1) long c = i.value(); @@ -82,8 +82,8 @@ public Node idealize() { @Override Node copyF() { return new MulFNode(null,null); } @Override public CompilerException err() { if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null; - if( !(in(1)._type instanceof SONTypeInteger) ) return Compiler.error("Cannot '"+label()+"' " + in(1)._type.glb()); - if( !(in(2)._type instanceof SONTypeInteger) ) return Compiler.error("Cannot '"+label()+"' " + in(2)._type.glb()); + if( !(in(1)._type instanceof TypeInteger) ) return Compiler.error("Cannot '"+label()+"' " + in(1)._type.glb()); + if( !(in(2)._type instanceof TypeInteger) ) return Compiler.error("Cannot '"+label()+"' " + in(2)._type.glb()); return null; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NeverNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NeverNode.java index 5e7f4e7..4d392ef 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NeverNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NeverNode.java @@ -1,8 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeTuple; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeTuple; import java.util.BitSet; @@ -15,7 +14,7 @@ public class NeverNode extends IfNode { @Override public StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append("Never"); } - @Override public SONType compute() { return SONTypeTuple.IF_BOTH; } + @Override public Type compute() { return TypeTuple.IF_BOTH; } @Override public Node idealize() { return null; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NewNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NewNode.java index 1d7ca33..daac5e8 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NewNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/NewNode.java @@ -16,21 +16,21 @@ */ public class NewNode extends Node implements MultiNode { - public final SONTypeMemPtr _ptr; + public final TypeMemPtr _ptr; public final int _len; - public NewNode(SONTypeMemPtr ptr, Node... nodes) { + public NewNode(TypeMemPtr ptr, Node... nodes) { super(nodes); assert !ptr.nullable(); _ptr = ptr; _len = ptr._obj._fields.length; // Control in slot 0 - assert nodes[0]._type== SONType.CONTROL || nodes[0]._type == SONType.XCONTROL; + assert nodes[0]._type== Type.CONTROL || nodes[0]._type == Type.XCONTROL; // Malloc-length in slot 1 - assert nodes[1]._type instanceof SONTypeInteger || nodes[1]._type== SONType.NIL; + assert nodes[1]._type instanceof TypeInteger || nodes[1]._type== Type.NIL; for( int i=0; i<_len; i++ ) // Memory slices for all fields. - assert nodes[2+i]._type.isa(SONTypeMem.BOT); + assert nodes[2+i]._type.isa(TypeMem.BOT); } public NewNode(NewNode nnn) { super(nnn); _ptr = nnn._ptr; _len = nnn._len; } @@ -61,19 +61,19 @@ int findAlias(int alias) { public Node size() { return in(1); } @Override - public SONTypeTuple compute() { + public TypeTuple compute() { Field[] fs = _ptr._obj._fields; - SONType[] ts = new SONType[fs.length+2]; - ts[0] = SONType.CONTROL; + Type[] ts = new Type[fs.length+2]; + ts[0] = Type.CONTROL; ts[1] = _ptr; for( int i=0; i(n==null ? new Node[0] : n._inputs.asAry()); _outputs = new Ary<>(Node.class); - _type = n==null ? SONType.BOTTOM : n._type; + _type = n==null ? Type.BOTTOM : n._type; _deps = null; _hash = 0; } @@ -386,9 +386,9 @@ public final Node peephole( ) { /** * Try to peephole at this node and return a better replacement Node if - * possible. We compute a {@link SONType} and then check and replace: + * possible. We compute a {@link Type} and then check and replace: *
    - *
  • if the Type {@link SONType#isConstant}, we replace with a {@link ConstantNode}
  • + *
  • if the Type {@link Type#isConstant}, we replace with a {@link ConstantNode}
  • *
  • in a future chapter we will look for a * Common Subexpression * to eliminate.
  • @@ -405,7 +405,7 @@ public final Node peephole( ) { public final Node peepholeOpt( ) { CODE.iterCnt(); // Compute initial or improved Type - SONType old = setType(compute()); + Type old = setType(compute()); // Replace constant computations from non-constants with a constant // node. If peeps are disabled, still allow high Phis to collapse; @@ -473,12 +473,12 @@ private Node deadCodeElim(Node m) { * infinitely recurse until stack overflow. Instead, compute typically * computes a new type from the {@link #_type} field of its inputs. */ - public abstract SONType compute(); + public abstract Type compute(); // Set the type. Assert monotonic progress. // If changing, add users to worklist. - public SONType setType(SONType type) { - SONType old = _type; + public Type setType(Type type) { + Type old = _type; assert old==null || type.isa(old); // Since _type not set, can just re-run this in assert in the debugger if( old == type ) return old; _type = type; // Set _type late for easier assert debugging @@ -634,13 +634,13 @@ public final Node widen() { Node flt = copyF(); if( flt==null ) return this; for( int i=1; i 0 || i._max < 0) ) return SONTypeInteger.FALSE; - return SONTypeInteger.BOOL; + public TypeInteger compute() { + Type t0 = in(1)._type; + if( t0.isHigh() ) return TypeInteger.BOOL.dual(); + if( t0 == Type.NIL || t0 == TypeInteger.ZERO ) return TypeInteger.TRUE; + if( t0 instanceof TypeNil tn && tn.notNull() ) return TypeInteger.FALSE; + if( t0 instanceof TypeInteger i && (i._min > 0 || i._max < 0) ) return TypeInteger.FALSE; + return TypeInteger.BOOL; } @Override diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/OrNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/OrNode.java index fc76f23..444e2ca 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/OrNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/OrNode.java @@ -1,8 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; public class OrNode extends LogicalNode { public OrNode(Node lhs, Node rhs) { super(lhs, rhs); } @@ -13,28 +12,28 @@ public class OrNode extends LogicalNode { @Override public String glabel() { return "|"; } @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if( t1 instanceof SONTypeInteger i0 && - t2 instanceof SONTypeInteger i1 ) { + return TypeInteger.TOP; + if( t1 instanceof TypeInteger i0 && + t2 instanceof TypeInteger i1 ) { if( i0.isConstant() && i1.isConstant() ) - return SONTypeInteger.constant(i0.value()|i1.value()); + return TypeInteger.constant(i0.value()|i1.value()); } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } @Override public Node idealize() { Node lhs = in(1); Node rhs = in(2); - SONType t1 = lhs._type; - SONType t2 = rhs._type; + Type t1 = lhs._type; + Type t2 = rhs._type; // Or of 0. We do not check for (0|x) because this will already // canonicalize to (x|0) - if( t2.isConstant() && t2 instanceof SONTypeInteger i && i.value()==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 ) return lhs; // Move constants to RHS: con*arg becomes arg*con diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ParmNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ParmNode.java index b755949..054d3a6 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ParmNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ParmNode.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import java.util.BitSet; public class ParmNode extends PhiNode { @@ -8,7 +8,7 @@ public class ParmNode extends PhiNode { // Argument indices are mapped one-to-one on CallNode inputs public final int _idx; // Argument index - public ParmNode(String label, int idx, SONType declaredType, Node... inputs) { + public ParmNode(String label, int idx, Type declaredType, Node... inputs) { super(label,declaredType,inputs); _idx = idx; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/PhiNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/PhiNode.java index 9ac7bb8..86f7818 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/PhiNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/PhiNode.java @@ -11,10 +11,10 @@ public class PhiNode extends Node { // The Phi type we compute must stay within the domain of the Phi. // Example Int stays Int, Ptr stays Ptr, Control stays Control, Mem stays Mem. - final SONType _declaredType; + final Type _declaredType; - public PhiNode(String label, SONType declaredType, Node... inputs) { super(inputs); _label = label; assert declaredType!=null; _declaredType = declaredType; } - public PhiNode(PhiNode phi, String label, SONType declaredType) { super(phi); _label = label; _type = _declaredType = declaredType; } + public PhiNode(String label, Type declaredType, Node... inputs) { super(inputs); _label = label; assert declaredType!=null; _declaredType = declaredType; } + public PhiNode(PhiNode phi, String label, Type declaredType) { super(phi); _label = label; _type = _declaredType = declaredType; } public PhiNode(PhiNode phi) { super(phi); _label = phi._label; _declaredType = phi._declaredType; } public PhiNode(RegionNode r, Node sample) { @@ -45,21 +45,21 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } public CFGNode region() { return (CFGNode)in(0); } - @Override public boolean isMem() { return _declaredType instanceof SONTypeMem; } + @Override public boolean isMem() { return _declaredType instanceof TypeMem; } @Override public boolean isPinned() { return true; } @Override - public SONType compute() { + public Type compute() { if( !(region() instanceof RegionNode r) ) - return region()._type== SONType.XCONTROL ? (_type instanceof SONTypeMem ? SONTypeMem.TOP : SONType.TOP) : _type; + return region()._type== Type.XCONTROL ? (_type instanceof TypeMem ? TypeMem.TOP : Type.TOP) : _type; // During parsing Phis have to be computed type pessimistically. if( r.inProgress() ) return _declaredType; // Set type to local top of the starting type - SONType t = _declaredType.glb().dual();//Type.TOP; + Type t = _declaredType.glb().dual();//Type.TOP; for (int i = 1; i < nIns(); i++) // If the region's control input is live, add this as a dependency // to the control because we can be peeped should it become dead. - if( addDep(r.in(i))._type != SONType.XCONTROL ) + if( addDep(r.in(i))._type != Type.XCONTROL ) t = t.meet(in(i)._type); return t; } @@ -78,7 +78,7 @@ public Node idealize() { // No bother if region is going to fold dead paths soon for( int i=1; i>i1.value()); + return TypeInteger.constant(i0.value()>>i1.value()); if( i1.isConstant() ) { int log = (int)i1.value(); - return SONTypeInteger.make(-1L<<(63-log),(1L<<(63-log))-1); + return TypeInteger.make(-1L<<(63-log),(1L<<(63-log))-1); } } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } @Override public Node idealize() { Node lhs = in(1); Node rhs = in(2); - SONType t2 = rhs._type; + Type t2 = rhs._type; // Sar of 0. - if( t2.isConstant() && t2 instanceof SONTypeInteger i && (i.value()&63)==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 ) return lhs; // TODO: x >> 3 >> (y ? 1 : 2) ==> x >> (y ? 4 : 5) diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java index c3109cc..bf3b53e 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java @@ -144,7 +144,7 @@ int find( String name ) { /** * Create a new variable name in the current scope */ - public boolean define(String name, SONType declaredType, boolean xfinal, Node init) { + public boolean define(String name, Type declaredType, boolean xfinal, Node init) { assert _lexSize.isEmpty() || name.charAt(0)!='$' ; // Later scopes do not define memory if( _lexSize._len > 0 ) for( int i=_vars.size()-1; i>=_lexSize.last(); i-- ) { @@ -355,20 +355,20 @@ public void addGuards( Node ctrl, Node pred, boolean invert ) { pred = pred instanceof NotNode not ? not.in(1) : CodeGen.CODE.add(new NotNode(pred).peephole()); // This is a zero/null test. // Compute the positive test type. - SONType tnz = pred._type.nonZero(); + Type tnz = pred._type.nonZero(); if( tnz!=null ) _addGuard(tnz,ctrl,pred); // Compute the negative test type. if( pred instanceof NotNode not ) { Node npred = not.in(1); - SONType tzero = npred._type.makeZero(); + Type tzero = npred._type.makeZero(); _addGuard(tzero,ctrl,npred); } } - private void _addGuard(SONType guard, Node ctrl, Node pred) { - SONType tcast = guard.join(pred._type); + private void _addGuard(Type guard, Node ctrl, Node pred) { + Type tcast = guard.join(pred._type); if( tcast != pred._type && !tcast.isHigh() ) { Node cast = new CastNode(tcast,ctrl,pred.keep()).peephole().keep(); _guards.add(pred); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShlNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShlNode.java index f8d59b5..8b9a66c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShlNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShlNode.java @@ -1,8 +1,8 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; public class ShlNode extends LogicalNode { public ShlNode(Node lhs, Node rhs) { super(lhs, rhs); } @@ -13,18 +13,18 @@ public class ShlNode extends LogicalNode { @Override public String glabel() { return "<<"; } @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if (t1 instanceof SONTypeInteger i0 && - t2 instanceof SONTypeInteger i1 ) { - if( i0 == SONTypeInteger.ZERO ) - return SONTypeInteger.ZERO; + return TypeInteger.TOP; + if (t1 instanceof TypeInteger i0 && + t2 instanceof TypeInteger i1 ) { + if( i0 == TypeInteger.ZERO ) + return TypeInteger.ZERO; if( i0.isConstant() && i1.isConstant() ) - return SONTypeInteger.constant(i0.value()< (x << i) + (c << i) - if( lhs instanceof AddNode add && addDep(add).in(2)._type instanceof SONTypeInteger c && c.isConstant() ) { + if( lhs instanceof AddNode add && addDep(add).in(2)._type instanceof TypeInteger c && c.isConstant() ) { long sum = c.value() << shl.value(); if( Integer.MIN_VALUE <= sum && sum <= Integer.MAX_VALUE ) return new AddNode(new ShlNode(add.in(1),rhs).peephole(), Compiler.con(sum) ); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java index c4b2a9b..f50ac65 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java @@ -1,8 +1,7 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.Compiler; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; public class ShrNode extends LogicalNode { public ShrNode(Node lhs, Node rhs) { super(lhs, rhs); } @@ -13,28 +12,28 @@ public class ShrNode extends LogicalNode { @Override public String glabel() { return ">>>"; } @Override - public SONType compute() { - SONType t1 = in(1)._type, t2 = in(2)._type; + public Type compute() { + Type t1 = in(1)._type, t2 = in(2)._type; if( t1.isHigh() || t2.isHigh() ) - return SONTypeInteger.TOP; - if (t1 instanceof SONTypeInteger i0 && - t2 instanceof SONTypeInteger i1 ) { - if( i0 == SONTypeInteger.ZERO ) - return SONTypeInteger.ZERO; + return TypeInteger.TOP; + if (t1 instanceof TypeInteger i0 && + t2 instanceof TypeInteger i1 ) { + if( i0 == TypeInteger.ZERO ) + return TypeInteger.ZERO; if( i0.isConstant() && i1.isConstant() ) - return SONTypeInteger.constant(i0.value()>>>i1.value()); + return TypeInteger.constant(i0.value()>>>i1.value()); } - return SONTypeInteger.BOT; + return TypeInteger.BOT; } @Override public Node idealize() { Node lhs = in(1); Node rhs = in(2); - SONType t2 = rhs._type; + Type t2 = rhs._type; // Shr of 0. - if( t2.isConstant() && t2 instanceof SONTypeInteger i && (i.value()&63)==0 ) + if( t2.isConstant() && t2 instanceof TypeInteger i && (i.value()&63)==0 ) return lhs; // TODO: x >>> 3 >>> (y ? 1 : 2) ==> x >>> (y ? 4 : 5) diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SplitNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SplitNode.java index 0040146..f233e5f 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SplitNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SplitNode.java @@ -2,7 +2,7 @@ import com.compilerprogramming.ezlang.compiler.SB; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import java.util.BitSet; public abstract class SplitNode extends MachConcreteNode { @@ -16,7 +16,7 @@ public abstract class SplitNode extends MachConcreteNode { else in(1)._print0(sb,visited); return sb.append(")"); } - @Override public SONType compute() { return in(0)._type; } + @Override public Type compute() { return in(0)._type; } @Override public Node idealize() { return null; } @Override public void asm(CodeGen code, SB sb) { sb.p(code.reg(this)).p(" = ").p(code.reg(in(1))); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java index 5908301..ba18e35 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java @@ -1,7 +1,6 @@ package com.compilerprogramming.ezlang.compiler.nodes; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; -import com.compilerprogramming.ezlang.compiler.Compiler; import com.compilerprogramming.ezlang.compiler.sontypes.*; import java.util.BitSet; @@ -17,9 +16,9 @@ */ public class StartNode extends LoopNode implements MultiNode { - final SONType _arg; + final Type _arg; - public StartNode(SONType arg) { super((Node)null); _arg = arg; _type = compute(); } + public StartNode(Type arg) { super((Node)null); _arg = arg; _type = compute(); } public StartNode(StartNode start) { super(start); _arg = start==null ? null : start._arg; } @Override public String label() { return "Start"; } @@ -44,8 +43,8 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } - @Override public SONTypeTuple compute() { - return SONTypeTuple.make(SONType.CONTROL, SONTypeMem.TOP,_arg); + @Override public TypeTuple compute() { + return TypeTuple.make(Type.CONTROL, TypeMem.TOP,_arg); } @Override public Node idealize() { return null; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java index f0fbdfa..5f19131 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java @@ -1,6 +1,6 @@ package com.compilerprogramming.ezlang.compiler.nodes; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import java.util.BitSet; public class StopNode extends CFGNode { @@ -40,8 +40,8 @@ public ReturnNode ret() { } @Override - public SONType compute() { - return SONType.BOTTOM; + public Type compute() { + return Type.BOTTOM; } @Override diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StoreNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StoreNode.java index a37af4f..fc3d9a9 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StoreNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StoreNode.java @@ -21,7 +21,7 @@ public class StoreNode extends MemOpNode { * @param off The offset inside the struct base * @param value Value to be stored */ - public StoreNode(String name, int alias, SONType glb, Node mem, Node ptr, Node off, Node value, boolean init) { + public StoreNode(String name, int alias, Type glb, Node mem, Node ptr, Node off, Node value, boolean init) { super(name, alias, false, glb, mem, ptr, off, value); _init = init; } @@ -40,20 +40,20 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { } @Override - public SONType compute() { - SONType val = val()._type; - SONTypeMem mem = (SONTypeMem)mem()._type; // Invariant - if( mem == SONTypeMem.TOP ) return SONTypeMem.TOP; - SONType t = SONType.BOTTOM; // No idea on field contents + public Type compute() { + Type val = val()._type; + TypeMem mem = (TypeMem)mem()._type; // Invariant + if( mem == TypeMem.TOP ) return TypeMem.TOP; + Type t = Type.BOTTOM; // No idea on field contents // Same alias, lift val to the declared type and then meet into other fields if( mem._alias == _alias ) { // Update declared forward ref to the actual - if( _declaredType.isFRef() && val instanceof SONTypeMemPtr tmp && !tmp.isFRef() ) + if( _declaredType.isFRef() && val instanceof TypeMemPtr tmp && !tmp.isFRef() ) _declaredType = tmp; val = val.join(_declaredType); t = val.meet(mem._t); } - return SONTypeMem.make(_alias,t); + return TypeMem.make(_alias,t); } @Override @@ -64,7 +64,7 @@ public Node idealize() { if( mem() instanceof StoreNode st && ptr()==st.ptr() && // Must check same object off()==st.off() && // And same offset (could be "same alias" but this handles arrays to same index) - ptr()._type instanceof SONTypeMemPtr && // No bother if weird dead pointers + ptr()._type instanceof TypeMemPtr && // No bother if weird dead pointers // Must have exactly one use of "this" or you get weird // non-serializable memory effects in the worse case. checkOnlyUse(st) ) { @@ -77,7 +77,7 @@ public Node idealize() { if( val() instanceof AndNode and && and.in(2)._type.isConstant() ) { int log = _declaredType.log_size(); if( log<3 ) { // And-mask vs narrow store - long mask = ((SONTypeInteger)and.in(2)._type).value(); + long mask = ((TypeInteger)and.in(2)._type).value(); long bits = (1L<<(8<>55) == ti.value(); } // True if signed 12-bit immediate - private static boolean imm12(SONTypeInteger ti) { + private static boolean imm12(TypeInteger ti) { // 52 = 64-12 return ti.isConstant() && ((ti.value()<<52)>>52) == ti.value(); } @@ -265,7 +265,7 @@ private static boolean imm12(SONTypeInteger ti) { // Can we encode this in ARM's 12-bit LOGICAL immediate form? // Some combination of shifted bit-masks. - private static int imm12Logical(SONTypeInteger ti) { + private static int imm12Logical(TypeInteger ti) { if( !ti.isConstant() ) return -1; if( !ti.isConstant() ) return -1; long val = ti.value(); @@ -563,17 +563,17 @@ public static int b_calloc(int opcode, int delta) { return (opcode << 26) | delta; } - @Override public RegMask callArgMask(SONTypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } - static RegMask callInMask(SONTypeFunPtr tfp, int idx, int maxArgSlot ) { + @Override public RegMask callArgMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } + static RegMask callInMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { if( idx==0 ) return CodeGen.CODE._rpcMask; if( idx==1 ) return null; // Count floats in signature up to index int fcnt=0; for( int i=2; i new IntARM(con); - case SONTypeFloat tf -> new FloatARM(con); - case SONTypeFunPtr tfp -> new TFPARM(con); - case SONTypeMemPtr tmp -> new ConstantNode(con); - case SONTypeNil tn -> throw Utils.TODO(); + case TypeInteger ti -> new IntARM(con); + case TypeFloat tf -> new FloatARM(con); + case TypeFunPtr tfp -> new TFPARM(con); + case TypeMemPtr tmp -> new ConstantNode(con); + case TypeNil tn -> throw Utils.TODO(); // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case SONType t -> t==SONType.NIL ? new IntARM(con) : new ConstantNode(con); + case Type t -> t== Type.NIL ? new IntARM(con) : new ConstantNode(con); }; } private Node call(CallNode call){ - return call.fptr() instanceof ConstantNode con && con._con instanceof SONTypeFunPtr tfp + return call.fptr() instanceof ConstantNode con && con._con instanceof TypeFunPtr tfp ? new CallARM(call, tfp) : new CallRRARM(call); } private Node or(OrNode or) { int imm12; - return or.in(2) instanceof ConstantNode off && off._con instanceof SONTypeInteger ti && (imm12 = imm12Logical(ti)) != -1 + return or.in(2) instanceof ConstantNode off && off._con instanceof TypeInteger ti && (imm12 = imm12Logical(ti)) != -1 ? new OrIARM(or, imm12) : new OrARM(or); } private Node xor(XorNode xor) { int imm12; - return xor.in(2) instanceof ConstantNode off && off._con instanceof SONTypeInteger ti && (imm12 = imm12Logical(ti)) != -1 + return xor.in(2) instanceof ConstantNode off && off._con instanceof TypeInteger ti && (imm12 = imm12Logical(ti)) != -1 ? new XorIARM(xor, imm12) : new XorARM(xor); } private Node and(AndNode and) { int imm12; - return and.in(2) instanceof ConstantNode off && off._con instanceof SONTypeInteger ti && (imm12 = imm12Logical(ti)) != -1 + return and.in(2) instanceof ConstantNode off && off._con instanceof TypeInteger ti && (imm12 = imm12Logical(ti)) != -1 ? new AndIARM(and, imm12) : new AndARM(and); } private Node asr(SarNode asr) { - return asr.in(2) instanceof ConstantNode off && off._con instanceof SONTypeInteger ti && ti.value() >= 0 && ti.value() < 63 + return asr.in(2) instanceof ConstantNode off && off._con instanceof TypeInteger ti && ti.value() >= 0 && ti.value() < 63 ? new AsrIARM(asr, (int)ti.value()) : new AsrARM(asr); } private Node lsl(ShlNode lsl) { - return lsl.in(2) instanceof ConstantNode off && off._con instanceof SONTypeInteger ti && ti.value() >= 0 && ti.value() < 63 + return lsl.in(2) instanceof ConstantNode off && off._con instanceof TypeInteger ti && ti.value() >= 0 && ti.value() < 63 ? new LslIARM(lsl, (int)ti.value()) : new LslARM(lsl); } private Node lsr(ShrNode lsr) { - return lsr.in(2) instanceof ConstantNode off && off._con instanceof SONTypeInteger ti && ti.value() >= 0 && ti.value() < 63 + return lsr.in(2) instanceof ConstantNode off && off._con instanceof TypeInteger ti && ti.value() >= 0 && ti.value() < 63 ? new LsrIARM(lsr, (int)ti.value()) : new LsrARM(lsr); } @@ -800,8 +800,8 @@ private N address(N mop ) { Node base = mop.ptr(); // Skip/throw-away a ReadOnly, only used to typecheck if( base instanceof ReadOnlyNode read ) base = read.in(1); - assert !(base instanceof AddNode) && base._type instanceof SONTypeMemPtr; // Base ptr always, not some derived - if( mop.off() instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm9(ti) ) { + assert !(base instanceof AddNode) && base._type instanceof TypeMemPtr; // Base ptr always, not some derived + if( mop.off() instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm9(ti) ) { off = (int)ti.value(); assert off == ti.value(); // In 32-bit range } else { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/AUIPC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/AUIPC.java index 8691f78..6dd5f28 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/AUIPC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/AUIPC.java @@ -4,11 +4,11 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; // Add upper 20bits to PC. Immediate comes from the relocation info. public class AUIPC extends ConstantNode implements MachNode, RIPRelSize { - AUIPC( SONType tfp ) { super(tfp); } + AUIPC( Type tfp ) { super(tfp); } @Override public RegMask regmap(int i) { return null; } @Override public RegMask outregmap() { return riscv.WMASK; } @Override public boolean isClone() { return true; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java index 457ca77..a552e4c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java @@ -3,12 +3,12 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; public class CallRISC extends CallNode implements MachNode, RIPRelSize { - final SONTypeFunPtr _tfp; + final TypeFunPtr _tfp; final String _name; - CallRISC( CallNode call, SONTypeFunPtr tfp ) { + CallRISC( CallNode call, TypeFunPtr tfp ) { super(call); assert tfp.isConstant(); _inputs.pop(); // Pop constant target @@ -23,7 +23,7 @@ public class CallRISC extends CallNode implements MachNode, RIPRelSize { } @Override public RegMask outregmap() { return null; } @Override public String name() { return _name; } - @Override public SONTypeFunPtr tfp() { return _tfp; } + @Override public TypeFunPtr tfp() { return _tfp; } @Override public void encoding( Encoding enc ) { // Short form +/-4K: beq r0,r0,imm12 diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java index f9dd6e6..31d0f1b 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java @@ -1,11 +1,9 @@ package com.compilerprogramming.ezlang.compiler.nodes.cpus.riscv; import com.compilerprogramming.ezlang.compiler.SB; -import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; public class FltRISC extends ConstantNode implements MachNode, RIPRelSize { FltRISC(ConstantNode con) { super(con); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java index 150a8b9..814b984 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java @@ -3,7 +3,7 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; import java.util.BitSet; abstract public class ImmRISC extends MachConcreteNode implements MachNode { @@ -49,6 +49,6 @@ abstract public class ImmRISC extends MachConcreteNode implements MachNode { int imm12 = (_imm12<<20)>>20; // Sign extend 12 bits sb.p(code.reg(this)).p(" = ").p(code.reg(in(1))).p(" ").p(glabel()).p(" #").p(imm12); if( in(1) instanceof LUI lui ) - sb.p(" // #").hex4((int)(((SONTypeInteger)lui._con).value()) + imm12); + sb.p(" // #").hex4((int)(((TypeInteger)lui._con).value()) + imm12); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java index d01a11d..905800f 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java @@ -4,8 +4,8 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; // 12-bit integer constant. Larger constants are made up in the instruction // selection by adding with a LUI. @@ -18,7 +18,7 @@ public class IntRISC extends ConstantNode implements MachNode { @Override public IntRISC copy() { return new IntRISC(this); } @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); - int val = _con==SONType.NIL ? 0 : (int)(((SONTypeInteger)_con).value() & 0xFFF); + int val = _con== Type.NIL ? 0 : (int)(((TypeInteger)_con).value() & 0xFFF); // Explicit truncation of larger immediates; this will sign-extend on // load and this is handled during instruction selection. enc.add4(riscv.i_type(riscv.OP_IMM, dst, 0, riscv.ZERO, val)); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java index e63e001..a804ba8 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java @@ -4,21 +4,21 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; // Load upper 20bits. public class LUI extends ConstantNode implements MachNode { public LUI( int imm20 ) { - super(SONTypeInteger.constant(imm20)); - assert riscv.imm20Exact((SONTypeInteger)_con); + super(TypeInteger.constant(imm20)); + assert riscv.imm20Exact((TypeInteger)_con); } @Override public RegMask regmap(int i) { return null; } @Override public RegMask outregmap() { return riscv.WMASK; } @Override public boolean isClone() { return true; } - @Override public LUI copy() { return new LUI((int)((SONTypeInteger)_con).value()); } + @Override public LUI copy() { return new LUI((int)((TypeInteger)_con).value()); } @Override public String op() { return "lui"; } @Override public void encoding( Encoding enc ) { - long x = ((SONTypeInteger)_con).value(); + long x = ((TypeInteger)_con).value(); int imm20 = (int)(x>>12) & 0xFFFFF; short dst = enc.reg(this); int lui = riscv.u_type(riscv.OP_LUI, dst, imm20); @@ -27,6 +27,6 @@ public LUI( int imm20 ) { @Override public void asm(CodeGen code, SB sb) { String reg = code.reg(this); - sb.p(reg).p(" = #").hex4((int)(((SONTypeInteger)_con).value())); + sb.p(reg).p(" = #").hex4((int)(((TypeInteger)_con).value())); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java index 90b6a49..f8d1ddf 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java @@ -12,7 +12,7 @@ public abstract class MemOpRISC extends MemOpNode implements MachNode { final char _sz; MemOpRISC(MemOpNode mop, Node base, int off, Node val) { super(mop,mop); - assert base._type instanceof SONTypeMemPtr; + assert base._type instanceof TypeMemPtr; _inputs.setX(2, base ); // Base can be an Add, is no longer raw object base _inputs.setX(3, null); // Never an index _inputs.setX(4, val ); @@ -25,7 +25,7 @@ public abstract class MemOpRISC extends MemOpNode implements MachNode { @Override public StringBuilder _printMach(StringBuilder sb, BitSet visited) { return sb.append(".").append(_name); } - @Override public SONType compute() { throw Utils.TODO(); } + @Override public Type compute() { throw Utils.TODO(); } @Override public Node idealize() { throw Utils.TODO(); } // func3 is based on load/store size and extend @@ -33,26 +33,26 @@ int func3() { int func3 = -1; // no unsigned flavour for store, so both signed and unsigned trigger the same if(this instanceof StoreRISC) { - if( _declaredType == SONTypeInteger. I8 || _declaredType == SONTypeInteger.U8 || _declaredType == SONTypeInteger.BOOL) func3=0; // SB - if( _declaredType == SONTypeInteger.I16 || _declaredType == SONTypeInteger.U16 ) func3=1; // SH - if( _declaredType == SONTypeInteger.I32 || _declaredType == SONTypeInteger.U32 || _declaredType instanceof SONTypeMemPtr) func3=2; // SW - if( _declaredType == SONTypeInteger.BOT ) func3=3; // SD + if( _declaredType == TypeInteger. I8 || _declaredType == TypeInteger.U8 || _declaredType == TypeInteger.BOOL) func3=0; // SB + if( _declaredType == TypeInteger.I16 || _declaredType == TypeInteger.U16 ) func3=1; // SH + if( _declaredType == TypeInteger.I32 || _declaredType == TypeInteger.U32 || _declaredType instanceof TypeMemPtr) func3=2; // SW + if( _declaredType == TypeInteger.BOT ) func3=3; // SD if( func3 == -1 ) throw Utils.TODO(); return func3; } - if( _declaredType == SONTypeInteger. I8 ) func3=0; // LB - if( _declaredType == SONTypeInteger.I16 ) func3=1; // LH - if( _declaredType == SONTypeInteger.I32 ) func3=2; // LW - if( _declaredType == SONTypeInteger.BOT ) func3=3; // LD - if( _declaredType == SONTypeInteger. U8 ) func3=4; // LBU - if( _declaredType == SONTypeInteger.BOOL) func3=4; // LBU - if( _declaredType == SONTypeInteger.U16 ) func3=5; // LHU - if( _declaredType == SONTypeInteger.U32 ) func3=6; // LWU + if( _declaredType == TypeInteger. I8 ) func3=0; // LB + if( _declaredType == TypeInteger.I16 ) func3=1; // LH + if( _declaredType == TypeInteger.I32 ) func3=2; // LW + if( _declaredType == TypeInteger.BOT ) func3=3; // LD + if( _declaredType == TypeInteger. U8 ) func3=4; // LBU + if( _declaredType == TypeInteger.BOOL) func3=4; // LBU + if( _declaredType == TypeInteger.U16 ) func3=5; // LHU + if( _declaredType == TypeInteger.U32 ) func3=6; // LWU // float - if(_declaredType == SONTypeFloat.F32) func3 = 2; // fLW fSW - if(_declaredType == SONTypeFloat.F64) func3 = 3; // fLD fSD - if( _declaredType instanceof SONTypeMemPtr ) func3=3; // 8 byte pointers + if(_declaredType == TypeFloat.F32) func3 = 2; // fLW fSW + if(_declaredType == TypeFloat.F64) func3 = 3; // fLD fSD + if( _declaredType instanceof TypeMemPtr) func3=3; // 8 byte pointers if( func3 == -1 ) throw Utils.TODO(); return func3; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java index d04275d..fcdd904 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java @@ -4,7 +4,7 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; public class TFPRISC extends ConstantNode implements MachNode, RIPRelSize { TFPRISC(ConstantNode con) { super(con); } @@ -17,7 +17,7 @@ public class TFPRISC extends ConstantNode implements MachNode, RIPRelSize { enc.relo(this); // TODO: 1 op encoding, plus a TODO if it does not fit short dst = enc.reg(this); - SONTypeFunPtr tfp = (SONTypeFunPtr)_con; + TypeFunPtr tfp = (TypeFunPtr)_con; // auipc t0,0 int auipc = riscv.u_type(riscv.OP_AUIPC, dst, 0); // addi t1,t0 + #0 diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/UJmpRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/UJmpRISC.java index 7ddb3aa..f85809f 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/UJmpRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/UJmpRISC.java @@ -3,7 +3,7 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import java.util.BitSet; // unconditional jump @@ -13,7 +13,7 @@ public class UJmpRISC extends CFGNode implements MachNode, RIPRelSize { @Override public StringBuilder _print1( StringBuilder sb, BitSet visited ) { return sb.append("jmp "); } @Override public RegMask regmap(int i) {return null; } @Override public RegMask outregmap() { return null; } - @Override public SONType compute() { throw Utils.TODO(); } + @Override public Type compute() { throw Utils.TODO(); } @Override public Node idealize() { throw Utils.TODO(); } @Override public void encoding( Encoding enc ) { // Short form +/-4K: beq r0,r0,imm12 diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java index b051fef..854112f 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java @@ -253,17 +253,17 @@ static public int fsetop(String op) { }; } - @Override public RegMask callArgMask( SONTypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } - static RegMask callInMask( SONTypeFunPtr tfp, int idx, int maxArgSlot ) { + @Override public RegMask callArgMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } + static RegMask callInMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { if( idx==0 ) return RPC_MASK; if( idx==1 ) return null; // Count floats in signature up to index int fcnt=0; for( int i=2; i>52) == ti.value(); } // True if HIGH 20-bit signed immediate, with all zeros low. - public static boolean imm20Exact(SONTypeInteger ti) { + public static boolean imm20Exact(TypeInteger ti) { // shift left 32 to clear out the upper 32 bits. // shift right SIGNED to sign-extend upper 32 bits; then shift 12 more to clear out lower 12 bits. // shift left 12 to re-center the bits. @@ -371,13 +371,13 @@ private Node addf(AddFNode addf) { } private Node add(AddNode add) { - if( add.in(2) instanceof ConstantNode off2 && off2._con instanceof SONTypeInteger ti && imm12(ti) ) + if( add.in(2) instanceof ConstantNode off2 && off2._con instanceof TypeInteger ti && imm12(ti) ) return new AddIRISC(add, (int)ti.value(),true); return new AddRISC(add); } private Node and(AndNode and) { - if( and.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti ) { + if( and.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti ) { if( imm12(ti) ) return new AndIRISC(and, (int)ti.value()); // Could be any size low bit mask @@ -388,7 +388,7 @@ private Node and(AndNode and) { } private Node call(CallNode call) { - return call.fptr() instanceof ConstantNode con && con._con instanceof SONTypeFunPtr tfp + return call.fptr() instanceof ConstantNode con && con._con instanceof TypeFunPtr tfp ? new CallRISC(call, tfp) : new CallRRISC(call); } @@ -416,7 +416,7 @@ private Node cmp(BoolNode bool) { // we can remove the double XOR in the encodings. return switch( bool.op() ) { - case "<" -> bool.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti) + case "<" -> bool.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti) ? new SetIRISC(bool, (int)ti.value(),false) : new SetRISC(bool, false); // x <= y - flip and negate; !(y < x); `slt tmp=y,x; xori dst=tmp,#1` @@ -430,7 +430,7 @@ private Node cmp(BoolNode bool) { private Node con( ConstantNode con ) { if( !con._con.isConstant() ) return new ConstantNode( con ); // Default unknown caller inputs return switch( con._con ) { - case SONTypeInteger ti -> { + case TypeInteger ti -> { if( imm12(ti) ) yield new IntRISC(con); long x = ti.value(); if( imm20Exact(ti) ) yield new LUI((int)x); @@ -446,12 +446,12 @@ private Node con( ConstantNode con ) { throw Utils.TODO(); } // Load from constant pool - case SONTypeFloat tf -> new FltRISC(con); - case SONTypeFunPtr tfp -> new TFPRISC(con); - case SONTypeMemPtr tmp -> throw Utils.TODO(); - case SONTypeNil tn -> throw Utils.TODO(); + case TypeFloat tf -> new FltRISC(con); + case TypeFunPtr tfp -> new TFPRISC(con); + case TypeMemPtr tmp -> throw Utils.TODO(); + case TypeNil tn -> throw Utils.TODO(); // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case SONType t -> t==SONType.NIL ? new IntRISC(con) : new ConstantNode(con); + case Type t -> t== Type.NIL ? new IntRISC(con) : new ConstantNode(con); }; } @@ -468,43 +468,43 @@ private Node jmp( IfNode iff ) { } private Node or(OrNode or) { - if( or.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti)) + if( or.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti)) return new OrIRISC(or, (int)ti.value()); return new OrRISC(or); } private Node xor(XorNode xor) { - if( xor.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti)) + if( xor.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti)) return new XorIRISC(xor, (int)ti.value()); return new XorRISC(xor); } private Node sra(SarNode sar) { - if( sar.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti)) + if( sar.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti)) return new SraIRISC(sar, (int)ti.value()); return new SraRISC(sar); } private Node srl(ShrNode shr) { - if( shr.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti)) + if( shr.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti)) return new SrlIRISC(shr, (int)ti.value(),true); return new SrlRISC(shr); } private Node sll(ShlNode sll) { - if( sll.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti)) + if( sll.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti)) return new SllIRISC(sll, (int)ti.value()); return new SllRISC(sll); } private Node sub(SubNode sub) { - return sub.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti) + return sub.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti) ? new AddIRISC(sub, (int)(-ti.value()),true) : new SubRISC(sub); } private Node i2f8(ToFloatNode tfn) { - assert tfn.in(1)._type instanceof SONTypeInteger ti; + assert tfn.in(1)._type instanceof TypeInteger ti; return new I2F8RISC(tfn); } @@ -517,7 +517,7 @@ private Node ld(LoadNode ld) { } private Node st(StoreNode st) { - Node xval = st.val() instanceof ConstantNode con && con._con == SONTypeInteger.ZERO ? null : st.val(); + Node xval = st.val() instanceof ConstantNode con && con._con == TypeInteger.ZERO ? null : st.val(); return new StoreRISC(st,address(st), off, xval); } @@ -530,8 +530,8 @@ private Node address( MemOpNode mop ) { Node base = mop.ptr(); // Skip/throw-away a ReadOnly, only used to typecheck if( base instanceof ReadOnlyNode read ) base = read.in(1); - assert !(base instanceof AddNode) && base._type instanceof SONTypeMemPtr; // Base ptr always, not some derived - if( mop.off() instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm12(ti) ) { + assert !(base instanceof AddNode) && base._type instanceof TypeMemPtr; // Base ptr always, not some derived + if( mop.off() instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm12(ti) ) { off = (int)ti.value(); } else { base = new AddRISC(base,mop.off()); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java index ec9b60d..ead9f8b 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java @@ -4,12 +4,12 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.CallNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; public class CallX86 extends CallNode implements MachNode, RIPRelSize { - final SONTypeFunPtr _tfp; + final TypeFunPtr _tfp; final String _name; - CallX86( CallNode call, SONTypeFunPtr tfp ) { + CallX86( CallNode call, TypeFunPtr tfp ) { super(call); _inputs.pop(); // Pop constant target assert tfp.isConstant(); @@ -19,7 +19,7 @@ public class CallX86 extends CallNode implements MachNode, RIPRelSize { @Override public String op() { return "call"; } @Override public String label() { return op(); } @Override public String name() { return _name; } - @Override public SONTypeFunPtr tfp() { return _tfp; } + @Override public TypeFunPtr tfp() { return _tfp; } @Override public RegMask regmap(int i) { return x86_64_v2.callInMask(_tfp,i,fun()._maxArgSlot); } @Override public RegMask outregmap() { return null; } @Override public int nargs() { return nIns()-2; } // Minus control, memory, fptr diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java index d2bd42a..35c0efe 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java @@ -3,8 +3,8 @@ import com.compilerprogramming.ezlang.compiler.SB; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; public class CmpMemX86 extends MemOpX86 { final boolean _swap; // Op switched LHS, RHS @@ -45,10 +45,10 @@ public class CmpMemX86 extends MemOpX86 { } } - static void encVal(Encoding enc, SONType decl, short ptr, short idx, short src, int off, int scale, boolean _swap) { - if(decl instanceof SONTypeFloat) { + static void encVal(Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale, boolean _swap) { + if(decl instanceof TypeFloat) { src -= (short)x86_64_v2.XMM_OFFSET; - enc.add1(decl==SONTypeFloat.F32 ? 0xF3 : 0xF2).add1(0x0F).add1(0xC2); + enc.add1(decl== TypeFloat.F32 ? 0xF3 : 0xF2).add1(0x0F).add1(0xC2); } else { int log = decl.log_size(); x86_64_v2.rexF(src, ptr, idx, log == 3, enc); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java index f86e852..7b45ec3 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java @@ -3,7 +3,6 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; public abstract class ImmX86 extends MachConcreteNode implements MachNode { final int _imm; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java index 6067ac8..cc68d2a 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java @@ -3,14 +3,14 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; // Integer constants public class IntX86 extends ConstantNode implements MachNode { IntX86( ConstantNode con ) { super(con); } @Override public String op() { - return _con == SONType.NIL || _con == SONTypeInteger.ZERO ? "xor" : "ldi"; + return _con == Type.NIL || _con == TypeInteger.ZERO ? "xor" : "ldi"; } @Override public boolean isClone() { return true; } @Override public Node copy() { return new IntX86(this); } @@ -18,13 +18,13 @@ public class IntX86 extends ConstantNode implements MachNode { @Override public RegMask outregmap() { return x86_64_v2.WMASK; } // Zero-set uses XOR kills flags @Override public RegMask killmap() { - return _con == SONType.NIL || _con == SONTypeInteger.ZERO ? x86_64_v2.FLAGS_MASK : null; + return _con == Type.NIL || _con == TypeInteger.ZERO ? x86_64_v2.FLAGS_MASK : null; } @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); // Short form for zero - if( _con==SONType.NIL || _con==SONTypeInteger.ZERO ) { + if( _con== Type.NIL || _con== TypeInteger.ZERO ) { // XOR dst,dst. Can skip REX is dst is low 8, makes this a 32b // xor, which will also zero the high bits. if( dst >= 8 ) enc.add1(x86_64_v2.rex(dst, dst, 0)); @@ -37,7 +37,7 @@ public class IntX86 extends ConstantNode implements MachNode { // Conditional encoding based on 64 or 32 bits //REX.W + C7 /0 id MOV r/m64, imm32 - long imm = ((SONTypeInteger)_con).value(); + long imm = ((TypeInteger)_con).value(); if (Integer.MIN_VALUE <= imm && imm < 0) { // We need sign extension, so use imm32 into 64 bit register // REX.W + C7 /0 id MOV r/m64, imm32 @@ -68,7 +68,7 @@ public class IntX86 extends ConstantNode implements MachNode { // General form: "op\tdst=src+src" @Override public void asm(CodeGen code, SB sb) { String reg = code.reg(this); - if( _con == SONType.NIL || _con == SONTypeInteger.ZERO ) + if( _con == Type.NIL || _con == TypeInteger.ZERO ) sb.p(reg).p(",").p(reg); else _con.print(sb.p(reg).p(" = #")); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java index c35e176..c7f6624 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java @@ -5,10 +5,9 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.LoadNode; import com.compilerprogramming.ezlang.compiler.nodes.Node; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeMemPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; public class LoadX86 extends MemOpX86 { LoadX86( LoadNode ld, Node base, Node idx, int off, int scale ) { @@ -26,23 +25,23 @@ public class LoadX86 extends MemOpX86 { enc(enc, _declaredType, dst, ptr, idx, _off, _scale); } - static void enc( Encoding enc, SONType decl, short dst, short ptr, short idx, int off, int scale ) { - if( decl == SONTypeFloat.F32) enc.add1(0xF3); - if( decl == SONTypeFloat.F64) enc.add1(0xF2); + static void enc(Encoding enc, Type decl, short dst, short ptr, short idx, int off, int scale ) { + if( decl == TypeFloat.F32) enc.add1(0xF3); + if( decl == TypeFloat.F64) enc.add1(0xF2); - if( decl.isa(SONTypeFloat.F64) ) + if( decl.isa(TypeFloat.F64) ) dst -= (short)x86_64_v2.XMM_OFFSET; - x86_64_v2.rexF(dst, ptr, idx, decl != SONTypeInteger.U32 && decl != SONTypeFloat.F32 && decl != SONTypeFloat.F64, enc); + x86_64_v2.rexF(dst, ptr, idx, decl != TypeInteger.U32 && decl != TypeFloat.F32 && decl != TypeFloat.F64, enc); if( false ) ; - else if( decl == SONTypeFloat.F32 ) enc.add1(0x0F).add1(0x10); // F3 0F 10 /r MOVSS xmm1, m32 - else if( decl == SONTypeFloat.F64 ) enc.add1(0x0F).add1(0x10); // F2 0F 10 /r MOVSD xmm1, m64 - else if( decl.isa(SONTypeInteger.I8)) enc.add1(0x0F).add1(0xBE); // sign extend: REX.W + 0F BE /r MOVSX r64, r/m8 - else if( decl == SONTypeInteger.I16 ) enc.add1(0x0F).add1(0xBF); // sign extend: REX.W + 0F BF /r MOVSX r64, r/m16 - else if( decl == SONTypeInteger.I32 ) enc.add1(0x63); // sign extend: REX.W + 63 /r MOVSXD r64, r/m32 - else if( decl == SONTypeInteger.U8 ) enc.add1(0x0F).add1(0xB6); // zero extend: REX.W + 0F B6 /r MOVZX r64, r/m8 - else if( decl == SONTypeInteger.U16 ) enc.add1(0x0F).add1(0xB7); // zero extend: REX.W + 0F B7 /r MOVZX r64, r/m16 + else if( decl == TypeFloat.F32 ) enc.add1(0x0F).add1(0x10); // F3 0F 10 /r MOVSS xmm1, m32 + else if( decl == TypeFloat.F64 ) enc.add1(0x0F).add1(0x10); // F2 0F 10 /r MOVSD xmm1, m64 + else if( decl.isa(TypeInteger.I8)) enc.add1(0x0F).add1(0xBE); // sign extend: REX.W + 0F BE /r MOVSX r64, r/m8 + else if( decl == TypeInteger.I16 ) enc.add1(0x0F).add1(0xBF); // sign extend: REX.W + 0F BF /r MOVSX r64, r/m16 + else if( decl == TypeInteger.I32 ) enc.add1(0x63); // sign extend: REX.W + 63 /r MOVSXD r64, r/m32 + else if( decl == TypeInteger.U8 ) enc.add1(0x0F).add1(0xB6); // zero extend: REX.W + 0F B6 /r MOVZX r64, r/m8 + else if( decl == TypeInteger.U16 ) enc.add1(0x0F).add1(0xB7); // zero extend: REX.W + 0F B7 /r MOVZX r64, r/m16 // Covers U32, I64/BOT, TMP else if( decl.log_size()>=2 ) enc.add1(0x8B); // zero extend: 8B /r MOV r32, r/m32 else throw Utils.TODO(); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemOpX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemOpX86.java index e6e064d..ca9e4aa 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemOpX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemOpX86.java @@ -26,7 +26,7 @@ public abstract class MemOpX86 extends MemOpNode implements MachNode { final char _sz; // Handy print name MemOpX86( Node op, MemOpNode mop, Node base, Node idx, int off, int scale, int imm ) { super(op,mop); - assert base._type instanceof SONTypeMemPtr && !(base instanceof AddNode); + assert base._type instanceof TypeMemPtr && !(base instanceof AddNode); assert (idx==null && scale==0) || (idx!=null && 0<= scale && scale<=3); // Copy memory parts from e.g. the LoadNode over the opcode, e.g. an Add @@ -58,7 +58,7 @@ public abstract class MemOpX86 extends MemOpNode implements MachNode { } @Override public String label() { return op(); } - @Override public SONType compute() { throw Utils.TODO(); } + @Override public Type compute() { throw Utils.TODO(); } @Override public Node idealize() { throw Utils.TODO(); } // Register mask allowed on input i. diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/NewX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/NewX86.java index aa81518..34198db 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/NewX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/NewX86.java @@ -3,7 +3,6 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; public class NewX86 extends NewNode implements MachNode { // A pre-zeroed chunk of memory. diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SplitX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SplitX86.java index 545d0f1..278dfb2 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SplitX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SplitX86.java @@ -1,14 +1,12 @@ package com.compilerprogramming.ezlang.compiler.nodes.cpus.x86_64_v2; import com.compilerprogramming.ezlang.compiler.SB; -import com.compilerprogramming.ezlang.compiler.Utils; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.FunNode; import com.compilerprogramming.ezlang.compiler.nodes.Node; import com.compilerprogramming.ezlang.compiler.nodes.SplitNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeInteger; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; public class SplitX86 extends SplitNode { SplitX86( String kind, byte round ) { super(kind,round, new Node[2]); } @@ -59,12 +57,12 @@ public class SplitX86 extends SplitNode { x86_64_v2.indirectAdr(0, (short)-1/*index*/, (short)x86_64_v2.RSP, off, 0, enc); return; } - StoreX86.encVal(enc, srcX ? SONTypeFloat.F64 : SONTypeInteger.BOT, (short)x86_64_v2.RSP, (short)-1/*index*/, src, off, 0); + StoreX86.encVal(enc, srcX ? TypeFloat.F64 : TypeInteger.BOT, (short)x86_64_v2.RSP, (short)-1/*index*/, src, off, 0); return; } if( src >= x86_64_v2.MAX_REG ) { int off = enc._fun.computeStackOffset(enc._code,src); - LoadX86.enc(enc, dstX ? SONTypeFloat.F64 : SONTypeInteger.BOT, dst, (short)x86_64_v2.RSP, (short)-1, off, 0); + LoadX86.enc(enc, dstX ? TypeFloat.F64 : TypeInteger.BOT, dst, (short)x86_64_v2.RSP, (short)-1, off, 0); return; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java index 243f48b..aa641c0 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java @@ -2,11 +2,10 @@ import com.compilerprogramming.ezlang.compiler.SB; import com.compilerprogramming.ezlang.compiler.codegen.*; -import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.Node; import com.compilerprogramming.ezlang.compiler.nodes.StoreNode; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFloat; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; + import java.util.BitSet; public class StoreX86 extends MemOpX86 { @@ -52,7 +51,7 @@ public class StoreX86 extends MemOpX86 { } // Non-immediate encoding - static void encVal( Encoding enc, SONType decl, short ptr, short idx, short src, int off, int scale ) { + static void encVal(Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale ) { int log = decl.log_size(); // Float reg being stored if( src >= x86_64_v2.XMM_OFFSET ) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java index 4861b8d..19034fb 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java @@ -3,15 +3,13 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.nodes.cpus.riscv.riscv; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; -import com.compilerprogramming.ezlang.compiler.sontypes.SONTypeFunPtr; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; // Function constants public class TFPX86 extends ConstantNode implements MachNode, RIPRelSize { TFPX86( ConstantNode con ) { super(con); } @Override public String op() { - return _con == SONType.NIL ? "xor" : "ldx"; + return _con == Type.NIL ? "xor" : "ldx"; } @Override public boolean isClone() { return true; } @Override public Node copy() { return new TFPX86(this); } @@ -19,13 +17,13 @@ public class TFPX86 extends ConstantNode implements MachNode, RIPRelSize { @Override public RegMask outregmap() { return x86_64_v2.WMASK; } // Zero-set uses XOR kills flags @Override public RegMask killmap() { - return _con == SONType.NIL ? x86_64_v2.FLAGS_MASK : null; + return _con == Type.NIL ? x86_64_v2.FLAGS_MASK : null; } @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); // Short form for zero - if( _con==SONType.NIL ) { + if( _con== Type.NIL ) { // XOR dst,dst. Can skip REX is dst is low 8, makes this a 32b // xor, which will also zero the high bits. if( dst >= 8 ) enc.add1(x86_64_v2.rex(dst, dst, 0)); @@ -58,7 +56,7 @@ public class TFPX86 extends ConstantNode implements MachNode, RIPRelSize { // General form: "op\tdst=src+src" @Override public void asm(CodeGen code, SB sb) { String reg = code.reg(this); - if( _con == SONType.NIL ) + if( _con == Type.NIL ) sb.p(reg).p(",").p(reg); else _con.print(sb.p(reg).p(" = #")); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/UJmpX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/UJmpX86.java index d404bb5..7d05e25 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/UJmpX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/UJmpX86.java @@ -3,7 +3,7 @@ import com.compilerprogramming.ezlang.compiler.*; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.*; -import com.compilerprogramming.ezlang.compiler.sontypes.SONType; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; import java.util.BitSet; // unconditional jump @@ -16,7 +16,7 @@ public class UJmpX86 extends CFGNode implements MachNode, RIPRelSize { } @Override public RegMask regmap(int i) {return null; } @Override public RegMask outregmap() { return null; } - @Override public SONType compute() { throw Utils.TODO(); } + @Override public Type compute() { throw Utils.TODO(); } @Override public Node idealize() { throw Utils.TODO(); } @Override public void encoding( Encoding enc ) { enc.jump(this,uctrl()); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java index 547abda..ad134b5 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java @@ -185,8 +185,8 @@ public static void indirectAdr( int scale, short index, short base, int offset, // Map from function signature and argument index to register. // Used to set input registers to CallNodes, and ParmNode outputs. - @Override public RegMask callArgMask( SONTypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } - static RegMask callInMask( SONTypeFunPtr tfp, int idx, int maxArgSlot ) { + @Override public RegMask callArgMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } + static RegMask callInMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { if( idx==0 ) return RPC_MASK; if( idx==1 ) return null; return switch( CodeGen.CODE._callingConv ) { @@ -197,7 +197,7 @@ static RegMask callInMask( SONTypeFunPtr tfp, int idx, int maxArgSlot ) { } // Maximum stack args used by this signature - @Override public short maxArgSlot( SONTypeFunPtr tfp ) { + @Override public short maxArgSlot( TypeFunPtr tfp ) { return switch( CodeGen.CODE._callingConv ) { case "SystemV" -> maxArgSlotSys5 (tfp); case "Win64" -> maxArgSlotWin64(tfp); @@ -230,16 +230,16 @@ static RegMask callInMask( SONTypeFunPtr tfp, int idx, int maxArgSlot ) { R09_MASK, }; - static RegMask callWin64(SONTypeFunPtr tfp, int idx, int maxArgSlot ) { + static RegMask callWin64(TypeFunPtr tfp, int idx, int maxArgSlot ) { // idx 2,3,4,5 passed in registers, with stack slot mirrors. // idx >= 6 passed on stack, starting at slot#1 (#0 reserved for RPC). if( idx >= 6 ) return new RegMask(MAX_REG+maxArgSlot+(idx-2)); - return tfp.arg(idx-2) instanceof SONTypeFloat + return tfp.arg(idx-2) instanceof TypeFloat ? XMMS8 [idx-2] : WIN64_CALL[idx-2]; } - static short maxArgSlotWin64(SONTypeFunPtr tfp) { + static short maxArgSlotWin64(TypeFunPtr tfp) { return (short)tfp.nargs(); } @@ -271,23 +271,23 @@ static short maxArgSlotWin64(SONTypeFunPtr tfp) { R09_MASK, }; - static RegMask callSys5(SONTypeFunPtr tfp, int idx, int maxArgSlot ) { + static RegMask callSys5(TypeFunPtr tfp, int idx, int maxArgSlot ) { // First 6 integers passed in registers: rdi,rsi,rdx,rcx,r08,r09 // First 8 floats passed in registers: xmm0-xmm7 int icnt=0, fcnt=0; // Count of ints, floats for( int i=2; i new SubFX86(subf); case SubNode sub -> sub(sub); case ToFloatNode tfn -> i2f8(tfn); - case XCtrlNode x -> new ConstantNode(SONType.XCONTROL); + case XCtrlNode x -> new ConstantNode(Type.XCONTROL); case XorNode xor -> xor(xor); case LoopNode loop -> new LoopNode(loop); @@ -399,7 +399,7 @@ private Node add(AddNode add) { // Attempt a full LEA-style break down. // Returns one of AddX86, AddIX86, LeaX86, or LHS - if( rhs instanceof ConstantNode off2 && off2._con instanceof SONTypeInteger toff ) { + if( rhs instanceof ConstantNode off2 && off2._con instanceof TypeInteger toff ) { long imm = toff.value(); assert imm!=0; // Folded in peeps if( (int)imm != imm ) // Full 64bit immediate @@ -435,7 +435,7 @@ private Node _lea(Node add, Node base, Node idx, int off) { if( base instanceof ShlNode && !(idx instanceof ShlNode) ) throw Utils.TODO(); // Bug in canonicalization, should on RHS if( idx instanceof ShlNode shift && shift.in(2) instanceof ConstantNode shfcon && - shfcon._con instanceof SONTypeInteger tscale && 0 <= tscale.value() && tscale.value() <= 3 ) { + shfcon._con instanceof TypeInteger tscale && 0 <= tscale.value() && tscale.value() <= 3 ) { idx = shift.in(1); scale = ((int) tscale.value()); } @@ -447,13 +447,13 @@ private Node _lea(Node add, Node base, Node idx, int off) { private Node and(AndNode and) { - if( and.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm32(ti.value()) ) + if( and.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) return new AndIX86(and, (int)ti.value()); return new AndX86(and); } private Node call(CallNode call) { - return call.fptr() instanceof ConstantNode con && con._con instanceof SONTypeFunPtr tfp + return call.fptr() instanceof ConstantNode con && con._con instanceof TypeFunPtr tfp ? new CallX86(call, tfp) : new CallRX86(call); } @@ -487,11 +487,11 @@ private Node _cmp(BoolNode bool) { return new CmpMemX86(bool, address(ld), ld.ptr(), idx, off, scale, imm(lhs), val, true ); // Vs immediate - if( rhs instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm32(ti.value()) ) + if( rhs instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) return new CmpIX86(bool, (int)ti.value(), false); // Operand swap compare; Set and Jmp need to swap `bop` - if( lhs instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm32(ti.value()) ) + if( lhs instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) return new CmpIX86(bool, (int)ti.value(), swap=true); // x vs y @@ -501,18 +501,18 @@ private Node _cmp(BoolNode bool) { private Node con( ConstantNode con ) { if(!con._con.isConstant()) return new ConstantNode(con); // Default unknown caller inputs return switch (con._con) { - case SONTypeInteger ti -> new IntX86(con); - case SONTypeFloat tf -> new FltX86(con); - case SONTypeFunPtr tfp -> new TFPX86(con); - case SONTypeMemPtr tmp -> throw Utils.TODO(); - case SONTypeNil tn -> throw Utils.TODO(); + case TypeInteger ti -> new IntX86(con); + case TypeFloat tf -> new FltX86(con); + case TypeFunPtr tfp -> new TFPX86(con); + case TypeMemPtr tmp -> throw Utils.TODO(); + case TypeNil tn -> throw Utils.TODO(); // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case SONType t -> t == SONType.NIL ? new IntX86(con) : new ConstantNode(con); + case Type t -> t == Type.NIL ? new IntX86(con) : new ConstantNode(con); }; } private Node i2f8(ToFloatNode tfn) { - assert tfn.in(1)._type instanceof SONTypeInteger ti; + assert tfn.in(1)._type instanceof TypeInteger ti; return new I2f8X86(tfn); } @@ -523,7 +523,7 @@ private Node jmp(IfNode iff) { String op = "!="; if( iff.in(1) instanceof BoolNode bool ) op = swap ? IfNode.swap(bool.op()) : bool.op(); else if( iff.in(1)==null ) op = "=="; // Never-node cutout - else iff.setDef(1, new BoolNode.NE(iff.in(1), new ConstantNode(SONTypeInteger.ZERO))); + else iff.setDef(1, new BoolNode.NE(iff.in(1), new ConstantNode(TypeInteger.ZERO))); return new JmpX86(iff, op); } @@ -532,13 +532,13 @@ private Node ld(LoadNode ld) { } private Node mul(MulNode mul) { - if( mul.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm32(ti.value()) ) + if( mul.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) return new MulIX86(mul, (int)ti.value()); return new MulX86(mul); } private Node or(OrNode or) { - if( or.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm32(ti.value()) ) + if( or.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) return new OrIX86(or, (int)ti.value()); return new OrX86(or); } @@ -548,19 +548,19 @@ private Node prj( ProjNode prj ) { } private Node sar(SarNode sar) { - if( sar.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm8(ti.value())) + if( sar.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm8(ti.value())) return new SarIX86(sar, (int)(ti.value() & 0xff) ); return new SarX86(sar); } private Node shl(ShlNode shl) { - if( shl.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm8(ti.value())) + if( shl.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm8(ti.value())) return new ShlIX86(shl, (int)(ti.value() & 0xff) ); return new ShlX86(shl); } private Node shr( ShrNode shr ) { - if( shr.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm8(ti.value()) ) + if( shr.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm8(ti.value()) ) return new ShrIX86(shr, (int)(ti.value() & 0xff) ); return new ShrX86(shr); } @@ -586,13 +586,13 @@ private static boolean stld_match(StoreNode st, LoadNode ld ) { private Node sub (SubNode sub ){ - return sub.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti + return sub.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti ? new AddIX86(sub, (int)-ti.value()) : new SubX86(sub); } private Node xor (XorNode xor){ - if( xor.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti && imm32(ti.value()) ) + if( xor.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti && imm32(ti.value()) ) return new XorIX86(xor, (int)ti.value()); return new XorX86(xor); } @@ -608,18 +608,18 @@ private N address(N mop) { Node base = mop.ptr(); // Skip/throw-away a ReadOnly, only used to typecheck if(base instanceof ReadOnlyNode read) base = read.in(1); - assert !(base instanceof AddNode) && base._type instanceof SONTypeMemPtr; // Base ptr always, not some derived - if(mop.off() instanceof AddNode add && add.in(2) instanceof ConstantNode con && con._con instanceof SONTypeInteger ti) { + assert !(base instanceof AddNode) && base._type instanceof TypeMemPtr; // Base ptr always, not some derived + if(mop.off() instanceof AddNode add && add.in(2) instanceof ConstantNode con && con._con instanceof TypeInteger ti) { off = (int) ti.value(); assert off == ti.value(); // In 32-bit range idx = add.in(1); if(idx instanceof ShlNode shift && shift.in(2) instanceof ConstantNode shfcon && - shfcon._con instanceof SONTypeInteger tscale && 0 <= tscale.value() && tscale.value() <= 3) { + shfcon._con instanceof TypeInteger tscale && 0 <= tscale.value() && tscale.value() <= 3) { idx = shift.in(1); scale = (int) tscale.value(); } } else { - if(mop.off() instanceof ConstantNode con && con._con instanceof SONTypeInteger ti) { + if(mop.off() instanceof ConstantNode con && con._con instanceof TypeInteger ti) { off = (int) ti.value(); assert off == ti.value(); // In 32-bit range } else { @@ -631,7 +631,7 @@ private N address(N mop) { private int imm( Node xval ) { assert val == null && imm == 0; - if( xval instanceof ConstantNode con && con._con instanceof SONTypeInteger ti) { + if( xval instanceof ConstantNode con && con._con instanceof TypeInteger ti) { val = null; imm = (int) ti.value(); assert imm == ti.value(); // In 32-bit range diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java index 1a93013..20dd43d 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java @@ -28,7 +28,7 @@ public static SB print(SB sb, CodeGen code) { Encoding enc = code._encoding; if( enc!=null && !enc._bigCons.isEmpty() ) { iadr = (iadr+15)&-16; // pad to 16 - HashSet targets = new HashSet<>(); + HashSet targets = new HashSet<>(); sb.p("--- Constant Pool ------").nl(); // By log size for( int log = 3; log >= 0; log-- ) { @@ -38,8 +38,8 @@ public static SB print(SB sb, CodeGen code) { targets.add(relo._t); if( relo._t.log_size()==log ) { sb.hex2(iadr).p(" "); - if( relo._t instanceof SONTypeTuple tt ) { - for( SONType tx : tt._types ) { + if( relo._t instanceof TypeTuple tt ) { + for( Type tx : tt._types ) { switch( log ) { case 0: sb.hex1(enc.read1(iadr)); break; case 1: sb.hex2(enc.read2(iadr)); break; @@ -109,7 +109,7 @@ static int doBlock(int iadr, SB sb, CodeGen code, FunNode fun, int cfgidx) { boolean once=false; for( Node n : bb.outs() ) { if( !(n instanceof PhiNode phi) ) continue; - if( phi._type instanceof SONTypeMem || phi._type instanceof SONTypeRPC ) continue; // Nothing for the hidden ones + if( phi._type instanceof TypeMem || phi._type instanceof TypeRPC) continue; // Nothing for the hidden ones // Post-RegAlloc phi prints all on one line if( postAlloc ) { if( !once ) { once=true; sb.fix(4," ").p(" ").fix(encWidth,"").p(" "); } @@ -164,7 +164,7 @@ static int doInst( int iadr, SB sb, CodeGen code, FunNode fun, int cfgidx, Node // ProjNodes following a multi (e.g. Call or New results), // get indent slightly and just print their index & node# if( n instanceof ProjNode proj ) { - if( proj._type instanceof SONTypeMem ) return iadr; // Nothing for the hidden ones + if( proj._type instanceof TypeMem) return iadr; // Nothing for the hidden ones sb.fix(4," ").p(" ").fix(encWidth,"").p(" ").fix(opWidth,proj._label==null ? "---" : proj._label).p(" ").p(code.reg(n,fun)).nl(); return iadr; } @@ -204,7 +204,7 @@ static int doInst( int iadr, SB sb, CodeGen code, FunNode fun, int cfgidx, Node isMultiOp = isMultiOp(sb,old,len); sb.fix(argWidth-(len-old),""); // Pad out - } else if( !(n._type instanceof SONTypeMem) ) { + } else if( !(n._type instanceof TypeMem) ) { // Room for some inputs sb.fix(5, n._nid+":" ); int off = 0; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Field.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Field.java index d4cf783..70cee5e 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Field.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Field.java @@ -7,20 +7,20 @@ /** * Represents a field in a struct. This is not a Type in the type system. */ -public class Field extends SONType { +public class Field extends Type { // The pair {fieldName,type} uniquely identifies a field. // Field name public final String _fname; // Type of the field - public final SONType _type; + public final Type _type; // Unique memory alias, not sensibly part of a "type" but very convenient here. public final int _alias; // Field must be written to exactly once, no more, no less public final boolean _final; - public Field(String fname, SONType type, int alias, boolean xfinal ) { + public Field(String fname, Type type, int alias, boolean xfinal ) { super(TFLD); _fname = fname; _type = type; @@ -28,20 +28,20 @@ public Field(String fname, SONType type, int alias, boolean xfinal ) { _final = xfinal; } // Make with existing alias - public static Field make(String fname, SONType type, int alias, boolean xfinal ) { + public static Field make(String fname, Type type, int alias, boolean xfinal ) { return new Field(fname,type,alias,xfinal).intern(); } - public Field makeFrom( SONType type ) { + public Field makeFrom( Type type ) { return type == _type ? this : new Field(_fname,type,_alias,_final).intern(); } @Override public Field makeRO() { return _final ? this : make(_fname,_type.makeRO(),_alias,true); } @Override public boolean isFinal() { return _final && _type.isFinal(); } - public static final Field TEST = make("test", SONType.NIL,-2,false); - public static final Field TEST2= make("test", SONType.NIL,-2,true); - public static void gather(ArrayList ts) { ts.add(TEST); ts.add(TEST2); } + public static final Field TEST = make("test", Type.NIL,-2,false); + public static final Field TEST2= make("test", Type.NIL,-2,true); + public static void gather(ArrayList ts) { ts.add(TEST); ts.add(TEST2); } - @Override Field xmeet( SONType that ) { + @Override Field xmeet( Type that ) { Field fld = (Field)that; // Invariant assert _fname.equals(fld._fname); assert _alias==fld._alias; @@ -52,14 +52,14 @@ public Field makeFrom( SONType type ) { public Field dual() { return make(_fname,_type.dual(),_alias,!_final); } @Override public Field glb() { - SONType glb = _type.glb(); + Type glb = _type.glb(); return (glb==_type && _final) ? this : make(_fname,glb,_alias,true); } // Override in subclasses int hash() { return _fname.hashCode() ^ _type.hashCode() ^ _alias ^ (_final ? 1024 : 0); } - boolean eq(SONType t) { + boolean eq(Type t) { Field f = (Field)t; return _fname.equals(f._fname) && _type==f._type && _alias==f._alias && _final==f._final; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONType.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Type.java similarity index 79% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONType.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Type.java index 86a8f70..5c3e392 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONType.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/Type.java @@ -21,8 +21,8 @@ * simple constant folding e.g. 1+2 == 3 stuff. */ -public class SONType { - static final HashMap INTERN = new HashMap<>(); +public class Type { + static final HashMap INTERN = new HashMap<>(); // ---------------------------------------------------------- // Simple types are implemented fully here. "Simple" means: the code and @@ -50,35 +50,35 @@ public class SONType { public boolean is_simple() { return _type < TSIMPLE; } private static final String[] STRS = new String[]{"Bot","Top","Ctrl","~Ctrl","null","~nil"}; - protected SONType(byte type) { _type = type; } - - public static final SONType BOTTOM = new SONType( TBOT ).intern(); // ALL - public static final SONType TOP = new SONType( TTOP ).intern(); // ANY - public static final SONType CONTROL = new SONType( TCTRL ).intern(); // Ctrl - public static final SONType XCONTROL = new SONType( TXCTRL ).intern(); // ~Ctrl - public static final SONType NIL = new SONType( TNIL ).intern(); // low null of all flavors - public static final SONType XNIL = new SONType( TXNIL ).intern(); // high or choice null - public static SONType[] gather() { - ArrayList ts = new ArrayList<>(); + protected Type(byte type) { _type = type; } + + public static final Type BOTTOM = new Type( TBOT ).intern(); // ALL + public static final Type TOP = new Type( TTOP ).intern(); // ANY + public static final Type CONTROL = new Type( TCTRL ).intern(); // Ctrl + public static final Type XCONTROL = new Type( TXCTRL ).intern(); // ~Ctrl + public static final Type NIL = new Type( TNIL ).intern(); // low null of all flavors + public static final Type XNIL = new Type( TXNIL ).intern(); // high or choice null + public static Type[] gather() { + ArrayList ts = new ArrayList<>(); ts.add(BOTTOM); ts.add(CONTROL); ts.add(NIL); ts.add(XNIL); - SONTypeNil.gather(ts); - SONTypePtr.gather(ts); - SONTypeInteger.gather(ts); - SONTypeFloat.gather(ts); - SONTypeMemPtr.gather(ts); - SONTypeFunPtr.gather(ts); - SONTypeMem.gather(ts); + TypeNil.gather(ts); + TypePtr.gather(ts); + TypeInteger.gather(ts); + TypeFloat.gather(ts); + TypeMemPtr.gather(ts); + TypeFunPtr.gather(ts); + TypeMem.gather(ts); Field.gather(ts); - SONTypeStruct.gather(ts); - SONTypeTuple.gather(ts); - SONTypeRPC.gather(ts); + TypeStruct.gather(ts); + TypeTuple.gather(ts); + TypeRPC.gather(ts); int sz = ts.size(); for( int i = 0; i < sz; i++ ) ts.add(ts.get(i).dual()); - return ts.toArray(new SONType[ts.size()]); + return ts.toArray(new Type[ts.size()]); } // Is high or on the lattice centerline. @@ -104,7 +104,7 @@ public static SONType[] gather() { // Factory method which interns "this" @SuppressWarnings("unchecked") - public T intern() { + public T intern() { T nnn = (T)INTERN.get(this); if( nnn==null ) INTERN.put(nnn=(T)this,this); @@ -125,41 +125,41 @@ public final int hashCode() { @Override public final boolean equals( Object o ) { if( o==this ) return true; - if( !(o instanceof SONType t)) return false; + if( !(o instanceof Type t)) return false; if( _type != t._type ) return false; return eq(t); } // Overridden in subclasses; subclass can assume "this!=t" and java classes are same - boolean eq(SONType t) { return true; } + boolean eq(Type t) { return true; } // ---------------------------------------------------------- - public final SONType meet(SONType t) { + public final Type meet(Type t) { // Shortcut for the self case if( t == this ) return this; // Same-type is always safe in the subclasses if( _type==t._type ) return xmeet(t); // TypeNil vs TypeNil meet - if( this instanceof SONTypeNil ptr0 && t instanceof SONTypeNil ptr1 ) + if( this instanceof TypeNil ptr0 && t instanceof TypeNil ptr1 ) return ptr0.nmeet(ptr1); // Reverse; xmeet 2nd arg is never "is_simple" and never equal to "this". if( is_simple() ) return this.xmeet(t ); if( t.is_simple() ) return t .xmeet(this); - return SONType.BOTTOM; // Mixing 2 unrelated types + return Type.BOTTOM; // Mixing 2 unrelated types } // Compute meet right now. Overridden in subclasses. // Handle cases where 'this.is_simple()' and unequal to 't'. // Subclassed xmeet calls can assert that '!t.is_simple()'. - SONType xmeet(SONType t) { + Type xmeet(Type t) { assert is_simple(); // Should be overridden in subclass // ANY meet anything is thing; thing meet ALL is ALL if( _type==TBOT || t._type==TTOP ) return this; if( _type==TTOP || t._type==TBOT ) return t; // RHS TypeNil vs NIL/XNIL - if( _type== TNIL ) return t instanceof SONTypeNil ptr ? ptr.meet0() : (t._type==TXNIL ? SONTypePtr.PTR : BOTTOM); - if( _type== TXNIL ) return t instanceof SONTypeNil ptr ? ptr.meetX() : (t._type== TNIL ? SONTypePtr.PTR : BOTTOM); + if( _type== TNIL ) return t instanceof TypeNil ptr ? ptr.meet0() : (t._type==TXNIL ? TypePtr.PTR : BOTTOM); + if( _type== TXNIL ) return t instanceof TypeNil ptr ? ptr.meetX() : (t._type== TNIL ? TypePtr.PTR : BOTTOM); // 'this' is only {TCTRL,TXCTRL} // Other non-simple RHS things bottom out if( !t.is_simple() ) return BOTTOM; @@ -169,7 +169,7 @@ SONType xmeet(SONType t) { return _type==TCTRL || t._type==TCTRL ? CONTROL : XCONTROL; } - public SONType dual() { + public Type dual() { return switch( _type ) { case TBOT -> TOP; case TTOP -> BOTTOM; @@ -184,24 +184,24 @@ public SONType dual() { // ---------------------------------------------------------- // Our lattice is defined with a MEET and a DUAL. // JOIN is dual of meet of both duals. - public final SONType join(SONType t) { + public final Type join(Type t) { if( this==t ) return this; return dual().meet(t.dual()).dual(); } // True if this "isa" t; e.g. 17 isa TypeInteger.BOT - public boolean isa( SONType t ) { return meet(t)==t; } + public boolean isa( Type t ) { return meet(t)==t; } // True if this "isa" t up to named structures - public boolean shallowISA( SONType t ) { return isa(t); } + public boolean shallowISA( Type t ) { return isa(t); } - public SONType nonZero() { return SONTypePtr.NPTR; } + public Type nonZero() { return TypePtr.NPTR; } // Make a zero version of this type, 0 for integers and null for pointers. - public SONType makeZero() { return SONType.NIL; } + public Type makeZero() { return Type.NIL; } /** Compute greatest lower bound in the lattice */ - public SONType glb() { return SONType.BOTTOM; } + public Type glb() { return Type.BOTTOM; } // Is forward-reference public boolean isFRef() { return false; } @@ -210,7 +210,7 @@ public final SONType join(SONType t) { public boolean isFinal() { return true; } // Make all reachable struct Fields final - public SONType makeRO() { return this; } + public Type makeRO() { return this; } // ---------------------------------------------------------- diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFloat.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeFloat.java similarity index 63% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFloat.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeFloat.java index c4d2dba..60d8a32 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFloat.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeFloat.java @@ -6,12 +6,12 @@ /** * Float Type */ -public class SONTypeFloat extends SONType { +public class TypeFloat extends Type { - public final static SONTypeFloat ONE = constant(1.0); - public final static SONTypeFloat FZERO = constant(0.0); - public final static SONTypeFloat F32 = make(32, 0); - public final static SONTypeFloat F64 = make(64, 0); + public final static TypeFloat ONE = constant(1.0); + public final static TypeFloat FZERO = constant(0.0); + public final static TypeFloat F32 = make(32, 0); + public final static TypeFloat F64 = make(64, 0); // - high -64, high -32, con 0, low +32, low +64 public final byte _sz; @@ -21,18 +21,18 @@ public class SONTypeFloat extends SONType { */ public final double _con; - private SONTypeFloat(byte sz, double con) { + private TypeFloat(byte sz, double con) { super(TFLT); _sz = sz; _con = con; } - private static SONTypeFloat make(int sz, double con) { - return new SONTypeFloat((byte)sz,con).intern(); + private static TypeFloat make(int sz, double con) { + return new TypeFloat((byte)sz,con).intern(); } - public static SONTypeFloat constant(double con) { return make(0, con); } + public static TypeFloat constant(double con) { return make(0, con); } - public static void gather(ArrayList ts) { ts.add(F64); ts.add(F32); ts.add(constant(3.141592653589793)); } + public static void gather(ArrayList ts) { ts.add(F64); ts.add(F32); ts.add(constant(3.141592653589793)); } @Override public String str() { return switch( _sz ) { @@ -53,13 +53,13 @@ private static SONTypeFloat make(int sz, double con) { public double value() { return _con; } @Override - public SONTypeFloat xmeet(SONType other) { - SONTypeFloat f = (SONTypeFloat)other; + public TypeFloat xmeet(Type other) { + TypeFloat f = (TypeFloat)other; // Invariant from caller: 'this' != 'other' and same class (TypeFloat). - SONTypeFloat i = (SONTypeFloat)other; // Contract + TypeFloat i = (TypeFloat)other; // Contract // Larger size in i1, smaller in i0 - SONTypeFloat i0 = _sz < i._sz ? this : i; - SONTypeFloat i1 = _sz < i._sz ? i : this; + TypeFloat i0 = _sz < i._sz ? this : i; + TypeFloat i1 = _sz < i._sz ? i : this; if( i1._sz== 64 ) return F64; if( i0._sz==-64 ) return i1; @@ -75,18 +75,18 @@ public SONTypeFloat xmeet(SONType other) { } @Override - public SONType dual() { + public Type dual() { return isConstant() ? this : make(-_sz,0); // Constants are a self-dual } - @Override public SONType makeZero() { return FZERO; } - @Override public SONType glb() { return F64; } + @Override public Type makeZero() { return FZERO; } + @Override public Type glb() { return F64; } @Override int hash() { return (int)(Double.hashCode(_con) ^ _sz ^ (1<<17)); } @Override - public boolean eq( SONType t ) { - SONTypeFloat i = (SONTypeFloat)t; // Contract + public boolean eq( Type t ) { + TypeFloat i = (TypeFloat)t; // Contract // Allow NaN to check for equality return _sz==i._sz && Double.doubleToLongBits(_con)==Double.doubleToLongBits(i._con); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeFunPtr.java similarity index 62% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeFunPtr.java index 484c1d7..1069de5 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeFunPtr.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeFunPtr.java @@ -16,16 +16,16 @@ * Functions with different signatures cannot be mixed, and will result in a * bottom function type, which can only be null-checked. */ -public class SONTypeFunPtr extends SONTypeNil { +public class TypeFunPtr extends TypeNil { // A TypeFunPtr is Signature and a set of functions. - public final SONTypeTuple _sig; - public final SONType _ret; + public final TypeTuple _sig; + public final Type _ret; // Cheesy easy implementation for a small set; 1 bit per unique function // within the TypeTuple. Can be upgraded to a BitSet for larger classes of // functions. Negative means "these 63 concrete bits plus infinite unknown more" public final long _fidxs; // 63 unique functions per signature - public SONTypeFunPtr(byte nil, SONTypeTuple sig, SONType ret, long fidxs) { + public TypeFunPtr(byte nil, TypeTuple sig, Type ret, long fidxs) { super(TFUNPTR,nil); assert sig != null; _sig = sig; @@ -33,41 +33,42 @@ public SONTypeFunPtr(byte nil, SONTypeTuple sig, SONType ret, long fidxs) { _fidxs = fidxs; } - public static SONTypeFunPtr make(byte nil, SONTypeTuple sig, SONType ret, long fidxs ) { return new SONTypeFunPtr(nil,sig,ret,fidxs).intern(); } - public static SONTypeFunPtr make(boolean nil, SONTypeTuple sig, SONType ret ) { return make((byte)(nil ? 3 : 2),sig,ret,-1); } - @Override SONTypeFunPtr makeFrom(byte nil ) { return nil ==_nil ? this : make( nil,_sig,_ret, _fidxs); } - public SONTypeFunPtr makeFrom(SONType ret ) { return ret ==_ret ? this : make( _nil,_sig, ret, _fidxs); } - public SONTypeFunPtr makeFrom(int fidx ) { return make((byte)2, _sig,_ret,1L< ts) { /* ts.add(TEST); ts.add(TEST0); */ ts.add(BOT); ts.add(MAIN); } + public static TypeFunPtr MAIN = make((byte)3, TypeTuple.MAIN, TypeInteger.BOT,-1); + public static TypeFunPtr CALLOC= make((byte)3, TypeTuple.CALLOC, TypeMemPtr.BOT,-1); + public static void gather(ArrayList ts) { /* ts.add(TEST); ts.add(TEST0); */ ts.add(BOT); ts.add(MAIN); } @Override - SONType xmeet(SONType t) { - SONTypeFunPtr that = (SONTypeFunPtr) t; - return SONTypeFunPtr.make(xmeet0(that),(SONTypeTuple)_sig.meet(that._sig), _ret.meet(that._ret), _fidxs | that._fidxs); + Type xmeet(Type t) { + TypeFunPtr that = (TypeFunPtr) t; + return TypeFunPtr.make(xmeet0(that),(TypeTuple)_sig.meet(that._sig), _ret.meet(that._ret), _fidxs | that._fidxs); } @Override - public SONTypeFunPtr dual() { return SONTypeFunPtr.make(dual0(), _sig.dual(), _ret.dual(), ~_fidxs); } + public TypeFunPtr dual() { return TypeFunPtr.make(dual0(), _sig.dual(), _ret.dual(), ~_fidxs); } // RHS is NIL; do not deep-dual when crossing the centerline - @Override public SONType meet0() { return _nil==3 ? this : make((byte)3,_sig,_ret,_fidxs); } + @Override public Type meet0() { return _nil==3 ? this : make((byte)3,_sig,_ret,_fidxs); } - @Override public SONTypeFunPtr glb() { return make((byte)3,_sig,_ret,-1L); } + @Override public TypeFunPtr glb() { return make((byte)3,_sig,_ret,-1L); } @Override public boolean isHigh () { return _nil <= 1 || (_nil==2 && _fidxs==0); } @Override public boolean isConstant() { return (_nil==2 && Long.bitCount(_fidxs)==1) || (_nil==3 && _fidxs==0); } @Override public int log_size() { return 2; } // (1<<2)==4-byte pointers - public SONType arg(int i) { return _sig._types[i]; } + public Type arg(int i) { return _sig._types[i]; } public long fidxs() { return _fidxs; } - public SONType ret() { return _ret; } + public Type ret() { return _ret; } public int nargs() { return _sig._types.length; } public int fidx() { assert Long.bitCount(_fidxs)==1; return Long.numberOfTrailingZeros(_fidxs); } @@ -75,8 +76,8 @@ SONType xmeet(SONType t) { int hash() { return Utils.fold(_sig.hashCode() ^ _ret.hashCode() ^ _fidxs ^ super.hash()); } @Override - boolean eq(SONType t) { - SONTypeFunPtr ptr = (SONTypeFunPtr)t; // Invariant + boolean eq(Type t) { + TypeFunPtr ptr = (TypeFunPtr)t; // Invariant return _sig == ptr._sig && _ret == ptr._ret && _fidxs == ptr._fidxs && super.eq(ptr); } @@ -84,11 +85,11 @@ boolean eq(SONType t) { @Override public SB print(SB sb) { return _print(sb,false,true); } public SB print(SB sb, boolean n) { return _print(sb,false,n); } @Override public SB gprint(SB sb) { return _print(sb,true ,true); } - private static SB _print(SB sb, boolean g, SONType t) { return g ? t.gprint(sb) : t.print(sb); } + private static SB _print(SB sb, boolean g, Type t) { return g ? t.gprint(sb) : t.print(sb); } private SB _print(SB sb, boolean g, boolean n) { sb.p(x()).p("{ "); if( _sig._types!=null ) - for( SONType t : _sig._types ) + for( Type t : _sig._types ) _print(sb,g,t).p(" "); _print(sb.p(g ? "→ " : "-> "),g,_ret).p(" #"); if( isHigh() ) sb.p("~"); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeInteger.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeInteger.java similarity index 67% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeInteger.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeInteger.java index 5030b57..3026653 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeInteger.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeInteger.java @@ -6,24 +6,24 @@ /** * Integer Type */ -public class SONTypeInteger extends SONType { +public class TypeInteger extends Type { - public final static SONTypeInteger ZERO= make(0,0); - public final static SONTypeInteger FALSE=ZERO; - public final static SONTypeInteger TRUE= make(1,1); + public final static TypeInteger ZERO= make(0,0); + public final static TypeInteger FALSE=ZERO; + public final static TypeInteger TRUE= make(1,1); - public final static SONTypeInteger I1 = make(-1,0); - public final static SONTypeInteger I8 = make(-128,127); - public final static SONTypeInteger I16 = make(-32768,32767); - public final static SONTypeInteger I32 = make(-1L<<31,(1L<<31)-1); - public final static SONTypeInteger BOT = make(Long.MIN_VALUE,Long.MAX_VALUE); - public final static SONTypeInteger TOP = BOT.dual(); + public final static TypeInteger I1 = make(-1,0); + public final static TypeInteger I8 = make(-128,127); + public final static TypeInteger I16 = make(-32768,32767); + public final static TypeInteger I32 = make(-1L<<31,(1L<<31)-1); + public final static TypeInteger BOT = make(Long.MIN_VALUE,Long.MAX_VALUE); + public final static TypeInteger TOP = BOT.dual(); - public final static SONTypeInteger U1 = make(0,1); - public final static SONTypeInteger BOOL= U1; - public final static SONTypeInteger U8 = make(0,255); - public final static SONTypeInteger U16 = make(0,65535); - public final static SONTypeInteger U32 = make(0,(1L<<32)-1); + public final static TypeInteger U1 = make(0,1); + public final static TypeInteger BOOL= U1; + public final static TypeInteger U8 = make(0,255); + public final static TypeInteger U16 = make(0,65535); + public final static TypeInteger U32 = make(0,(1L<<32)-1); /** * Describes an integer *range* - everything from min to max; both min and @@ -34,18 +34,18 @@ public class SONTypeInteger extends SONType { */ public final long _min, _max; - private SONTypeInteger(long min, long max) { + private TypeInteger(long min, long max) { super(TINT); _min = min; _max = max; } // Strict non-zero contract - public static SONTypeInteger make(long lo, long hi) { return new SONTypeInteger(lo,hi).intern(); } + public static TypeInteger make(long lo, long hi) { return new TypeInteger(lo,hi).intern(); } - public static SONTypeInteger constant(long con) { return make(con, con); } + public static TypeInteger constant(long con) { return make(con, con); } - public static void gather(ArrayList ts) { ts.add(I32); ts.add(BOT); ts.add(U1); ts.add(I1); ts.add(U8); } + public static void gather(ArrayList ts) { ts.add(I32); ts.add(BOT); ts.add(U1); ts.add(I1); ts.add(U8); } @Override public String str() { if( isConstant() ) return ""+_min; @@ -102,26 +102,26 @@ public long mask() { } @Override - public SONType xmeet(SONType other) { + public Type xmeet(Type other) { // Invariant from caller: 'this' != 'other' and same class (TypeInteger) - SONTypeInteger i = (SONTypeInteger)other; // Contract + TypeInteger i = (TypeInteger)other; // Contract return make(Math.min(_min,i._min), Math.max(_max,i._max)); } - @Override public SONTypeInteger dual() { return make(_max,_min); } + @Override public TypeInteger dual() { return make(_max,_min); } - @Override public SONTypeInteger nonZero() { + @Override public TypeInteger nonZero() { if( isHigh() ) return this; if( this==ZERO ) return null; // No sane answer if( _min==0 ) return make(1,Math.max(_max,1)); // specifically good on BOOL if( _max==0 ) return make(_min,-1); return this; } - @Override public SONType makeZero() { return ZERO; } - @Override public SONTypeInteger glb() { return BOT; } + @Override public Type makeZero() { return ZERO; } + @Override public TypeInteger glb() { return BOT; } @Override int hash() { return Utils.fold(_min) * Utils.fold(_max); } - @Override public boolean eq( SONType t ) { - SONTypeInteger i = (SONTypeInteger)t; // Contract + @Override public boolean eq( Type t ) { + TypeInteger i = (TypeInteger)t; // Contract return _min==i._min && _max==i._max; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMem.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeMem.java similarity index 59% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMem.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeMem.java index aed4569..6fd87d7 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMem.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeMem.java @@ -8,52 +8,52 @@ /** * Represents a slice of memory corresponding to a set of aliases */ -public class SONTypeMem extends SONType { +public class TypeMem extends Type { // Which slice of memory? // 0 means TOP, no slice. // 0 means BOT, all memory. // N means slice#N. public final int _alias; - public final SONType _t; // Memory contents, some scalar type + public final Type _t; // Memory contents, some scalar type - private SONTypeMem(int alias, SONType t) { + private TypeMem(int alias, Type t) { super(TMEM); - assert alias!=0 || (t== SONType.TOP || t== SONType.BOTTOM); + assert alias!=0 || (t== Type.TOP || t== Type.BOTTOM); _alias = alias; _t = t; } - public static SONTypeMem make(int alias, SONType t) { return new SONTypeMem(alias,t).intern(); } - public static final SONTypeMem TOP = make(0, SONType.TOP ); - public static final SONTypeMem BOT = make(0, SONType.BOTTOM); + public static TypeMem make(int alias, Type t) { return new TypeMem(alias,t).intern(); } + public static final TypeMem TOP = make(0, Type.TOP ); + public static final TypeMem BOT = make(0, Type.BOTTOM); - public static void gather(ArrayList ts) { ts.add(make(1, SONType.NIL)); ts.add(make(1, SONTypeInteger.ZERO)); ts.add(BOT); } + public static void gather(ArrayList ts) { ts.add(make(1, Type.NIL)); ts.add(make(1, TypeInteger.ZERO)); ts.add(BOT); } @Override - SONTypeMem xmeet(SONType t) { - SONTypeMem that = (SONTypeMem) t; // Invariant: TypeMem and unequal + TypeMem xmeet(Type t) { + TypeMem that = (TypeMem) t; // Invariant: TypeMem and unequal if( this==TOP ) return that; if( that==TOP ) return this; if( this==BOT ) return BOT; if( that==BOT ) return BOT; int alias = _alias==that._alias ? _alias : 0; - SONType mt = _t.meet(that._t); + Type mt = _t.meet(that._t); return make(alias,mt); } @Override - public SONType dual() { + public Type dual() { return make(_alias,_t.dual()); } @Override public boolean isHigh() { return _t.isHigh(); } - @Override public SONType glb() { return make(_alias,_t.glb()); } + @Override public Type glb() { return make(_alias,_t.glb()); } @Override int hash() { return 9876543 + _alias + _t.hashCode(); } - @Override boolean eq(SONType t) { - SONTypeMem that = (SONTypeMem) t; // Invariant + @Override boolean eq(Type t) { + TypeMem that = (TypeMem) t; // Invariant return _alias == that._alias && _t == that._t; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeMemPtr.java similarity index 60% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeMemPtr.java index e3b197f..c213e07 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeMemPtr.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeMemPtr.java @@ -13,7 +13,7 @@ * The distinguished *$BOT ptr represents union of *void and Null. * The distinguished *$TOP ptr represents the dual of *$BOT. */ -public class SONTypeMemPtr extends SONTypeNil { +public class TypeMemPtr extends TypeNil { // A TypeMemPtr is pair (obj,nil) // where obj is a TypeStruct, possibly TypeStruct.BOT/TOP // where nil is an explicit null is allowed or not @@ -24,49 +24,49 @@ public class SONTypeMemPtr extends SONTypeNil { // (BOT ,false) - a not-nil void* (unspecified struct) // (TOP ,true ) - a nil - public final SONTypeStruct _obj; + public final TypeStruct _obj; - public SONTypeMemPtr(byte nil, SONTypeStruct obj) { + public TypeMemPtr(byte nil, TypeStruct obj) { super(TMEMPTR,nil); assert obj!=null; _obj = obj; } - public static SONTypeMemPtr make(byte nil, SONTypeStruct obj) { return new SONTypeMemPtr(nil, obj).intern(); } - public static SONTypeMemPtr makeNullable(SONTypeStruct obj) { return make((byte)3, obj); } - public static SONTypeMemPtr make(SONTypeStruct obj) { return new SONTypeMemPtr((byte)2, obj).intern(); } - public SONTypeMemPtr makeFrom(SONTypeStruct obj) { return obj==_obj ? this : make(_nil, obj); } - public SONTypeMemPtr makeNullable() { return makeFrom((byte)3); } + public static TypeMemPtr make(byte nil, TypeStruct obj) { return new TypeMemPtr(nil, obj).intern(); } + public static TypeMemPtr makeNullable(TypeStruct obj) { return make((byte)3, obj); } + public static TypeMemPtr make(TypeStruct obj) { return new TypeMemPtr((byte)2, obj).intern(); } + public TypeMemPtr makeFrom(TypeStruct obj) { return obj==_obj ? this : make(_nil, obj); } + public TypeMemPtr makeNullable() { return makeFrom((byte)3); } @Override - SONTypeMemPtr makeFrom(byte nil) { return nil==_nil ? this : make(nil,_obj); } - @Override public SONTypeMemPtr makeRO() { return makeFrom(_obj.makeRO()); } + TypeMemPtr makeFrom(byte nil) { return nil==_nil ? this : make(nil,_obj); } + @Override public TypeMemPtr makeRO() { return makeFrom(_obj.makeRO()); } @Override public boolean isFinal() { return _obj.isFinal(); } // An abstract pointer, pointing to either a Struct or an Array. // Can also be null or not, so 4 choices {TOP,BOT} x {nil,not} - public static final SONTypeMemPtr BOT = make((byte)3, SONTypeStruct.BOT); - public static final SONTypeMemPtr TOP = BOT.dual(); - public static final SONTypeMemPtr NOTBOT = make((byte)2, SONTypeStruct.BOT); + public static final TypeMemPtr BOT = make((byte)3, TypeStruct.BOT); + public static final TypeMemPtr TOP = BOT.dual(); + public static final TypeMemPtr NOTBOT = make((byte)2, TypeStruct.BOT); //public static SONTypeMemPtr TEST= make((byte)2, SONTypeStruct.TEST); - public static void gather(ArrayList ts) { ts.add(NOTBOT); ts.add(BOT); /* ts.add(TEST); */ } + public static void gather(ArrayList ts) { ts.add(NOTBOT); ts.add(BOT); /* ts.add(TEST); */ } @Override - public SONTypeNil xmeet(SONType t) { - SONTypeMemPtr that = (SONTypeMemPtr) t; - return SONTypeMemPtr.make(xmeet0(that), (SONTypeStruct)_obj.meet(that._obj)); + public TypeNil xmeet(Type t) { + TypeMemPtr that = (TypeMemPtr) t; + return TypeMemPtr.make(xmeet0(that), (TypeStruct)_obj.meet(that._obj)); } @Override - public SONTypeMemPtr dual() { return SONTypeMemPtr.make( dual0(), _obj.dual()); } + public TypeMemPtr dual() { return TypeMemPtr.make( dual0(), _obj.dual()); } // RHS is NIL; do not deep-dual when crossing the centerline @Override - SONType meet0() { return _nil==3 ? this : make((byte)3,_obj); } + Type meet0() { return _nil==3 ? this : make((byte)3,_obj); } // True if this "isa" t up to named structures - @Override public boolean shallowISA( SONType t ) { - if( !(t instanceof SONTypeMemPtr that) ) return false; + @Override public boolean shallowISA( Type t ) { + if( !(t instanceof TypeMemPtr that) ) return false; if( this==that ) return true; if( xmeet0(that)!=that._nil ) return false; if( _obj==that._obj ) return true; @@ -75,7 +75,7 @@ public SONTypeNil xmeet(SONType t) { throw Utils.TODO(); // return _obj.shallowISA(that._obj); } - @Override public SONTypeMemPtr glb() { return make((byte)3,_obj.glb()); } + @Override public TypeMemPtr glb() { return make((byte)3,_obj.glb()); } // Is forward-reference @Override public boolean isFRef() { return _obj.isFRef(); } @@ -83,8 +83,8 @@ public SONTypeNil xmeet(SONType t) { @Override int hash() { return _obj.hashCode() ^ super.hash(); } - @Override boolean eq(SONType t) { - SONTypeMemPtr ptr = (SONTypeMemPtr)t; // Invariant + @Override boolean eq(Type t) { + TypeMemPtr ptr = (TypeMemPtr)t; // Invariant return _obj == ptr._obj && super.eq(ptr); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeNil.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeNil.java similarity index 61% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeNil.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeNil.java index 3193c0c..7ee19bb 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeNil.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeNil.java @@ -5,48 +5,48 @@ /** * Nil-able Scalar types */ -public abstract class SONTypeNil extends SONType { +public abstract class TypeNil extends Type { // 0 = high-subclass choice nil // 1 = high-subclass no nil // 2 = low -subclass no nil // 3 = low -subclass also nil final byte _nil; - SONTypeNil(byte t, byte nil ) { super(t); _nil = nil; } + TypeNil(byte t, byte nil ) { super(t); _nil = nil; } - public static void gather(ArrayList ts) { } + public static void gather(ArrayList ts) { } - abstract SONTypeNil makeFrom(byte nil); + abstract TypeNil makeFrom(byte nil); - byte xmeet0(SONTypeNil that) { return (byte)Math.max(_nil,that._nil); } + byte xmeet0(TypeNil that) { return (byte)Math.max(_nil,that._nil); } byte dual0() { return (byte)(3-_nil); } // RHS is NIL - abstract SONType meet0(); + abstract Type meet0(); // RHS is XNIL - SONType meetX() { - return _nil==0 ? XNIL : (_nil<=2 ? SONTypePtr.NPTR : SONTypePtr.PTR); + Type meetX() { + return _nil==0 ? XNIL : (_nil<=2 ? TypePtr.NPTR : TypePtr.PTR); } - SONType nmeet(SONTypeNil tn) { + Type nmeet(TypeNil tn) { // Invariants: both are TypeNil subclasses and unequal classes. // If this is TypePtr, we went to TypePtr.nmeet and not here. // If that is TypePtr, this is not (invariant); reverse and go again. - if( tn instanceof SONTypePtr ts ) return ts.nmeet(this); + if( tn instanceof TypePtr ts ) return ts.nmeet(this); // Two mismatched TypeNil, no Scalar. if( _nil==0 && tn._nil==0 ) return XNIL; - if( _nil<=2 && tn._nil<=2 ) return SONTypePtr.NPTR; - return SONTypePtr.PTR; + if( _nil<=2 && tn._nil<=2 ) return TypePtr.NPTR; + return TypePtr.PTR; } @Override public boolean isHigh () { return _nil <= 1; } @Override public boolean isConstant () { return false; } @Override public boolean isHighOrConst() { return isHigh() || isConstant(); } - @Override public SONType glb() { return SONType.NIL; } + @Override public Type glb() { return Type.NIL; } public boolean notNull() { return _nil==1 || _nil==2; } public boolean nullable() { return _nil==3; } @@ -56,5 +56,5 @@ SONType nmeet(SONTypeNil tn) { int hash() { return _nil<<17; } - boolean eq(SONTypeNil ptr) { return _nil == ptr._nil; } + boolean eq(TypeNil ptr) { return _nil == ptr._nil; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypePtr.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypePtr.java similarity index 51% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypePtr.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypePtr.java index dda8dd2..714271c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypePtr.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypePtr.java @@ -7,32 +7,32 @@ /** * Represents a Scalar; a single register-sized value. */ -public class SONTypePtr extends SONTypeNil { +public class TypePtr extends TypeNil { - private SONTypePtr(byte nil) { super(TPTR,nil); } + private TypePtr(byte nil) { super(TPTR,nil); } // An abstract pointer, pointing to either a Struct or an Array. // Can also be null or not, so 4 choices {TOP,BOT} x {nil,not} - public static SONTypePtr XPTR = new SONTypePtr((byte)0).intern(); - public static SONTypePtr XNPTR= new SONTypePtr((byte)1).intern(); - public static SONTypePtr NPTR = new SONTypePtr((byte)2).intern(); - public static SONTypePtr PTR = new SONTypePtr((byte)3).intern(); - private static final SONTypePtr[] PTRS = new SONTypePtr[]{XPTR,XNPTR,NPTR,PTR}; + public static TypePtr XPTR = new TypePtr((byte)0).intern(); + public static TypePtr XNPTR= new TypePtr((byte)1).intern(); + public static TypePtr NPTR = new TypePtr((byte)2).intern(); + public static TypePtr PTR = new TypePtr((byte)3).intern(); + private static final TypePtr[] PTRS = new TypePtr[]{XPTR,XNPTR,NPTR,PTR}; - public static void gather(ArrayList ts) { ts.add(PTR); ts.add(NPTR); } + public static void gather(ArrayList ts) { ts.add(PTR); ts.add(NPTR); } - SONTypeNil makeFrom(byte nil) { throw Utils.TODO(); } + TypeNil makeFrom(byte nil) { throw Utils.TODO(); } - @Override public SONTypeNil xmeet(SONType t) { - SONTypePtr that = (SONTypePtr) t; + @Override public TypeNil xmeet(Type t) { + TypePtr that = (TypePtr) t; return PTRS[xmeet0(that)]; } - @Override public SONTypePtr dual() { return PTRS[dual0()]; } + @Override public TypePtr dual() { return PTRS[dual0()]; } // High scalar loses, low scalar wins @Override - SONTypeNil nmeet(SONTypeNil tn) { + TypeNil nmeet(TypeNil tn) { if( _nil==0 ) return tn; // High scalar loses if( _nil==1 ) return tn.makeFrom(xmeet0(tn)); // High scalar loses if( _nil==2 ) return tn._nil==3 ? PTR : NPTR; // Low scalar wins @@ -42,13 +42,13 @@ SONTypeNil nmeet(SONTypeNil tn) { // RHS is NIL @Override - SONType meet0() { return isHigh() ? NIL : PTR; } + Type meet0() { return isHigh() ? NIL : PTR; } // RHS is XNIL // 0->xscalar, 1->nscalar, 2->nscalar, 3->scalar @Override - SONType meetX() { return _nil==0 ? XNIL : (_nil==3 ? PTR : NPTR); } + Type meetX() { return _nil==0 ? XNIL : (_nil==3 ? PTR : NPTR); } - @Override public SONTypePtr glb() { return PTR; } + @Override public TypePtr glb() { return PTR; } private static final String[] STRS = new String[]{"~ptr","~nptr","nptr","ptr"}; @Override public String str() { return STRS[_nil]; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeRPC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeRPC.java similarity index 74% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeRPC.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeRPC.java index 82f0e2f..4070ab2 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeRPC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeRPC.java @@ -6,7 +6,7 @@ /** * Return Program Control or Return PC or RPC */ -public class SONTypeRPC extends SONType { +public class TypeRPC extends Type { // A set of CallEndNode IDs (or StopNode); commonly just one. // Basically a sparse bit set @@ -15,26 +15,26 @@ public class SONTypeRPC extends SONType { // If true, invert the meaning of the bits final boolean _any; - private SONTypeRPC(boolean any, HashSet rpcs) { + private TypeRPC(boolean any, HashSet rpcs) { super(TRPC); _any = any; _rpcs = rpcs; } - private static SONTypeRPC make(boolean any, HashSet rpcs) { - return new SONTypeRPC(any,rpcs).intern(); + private static TypeRPC make(boolean any, HashSet rpcs) { + return new TypeRPC(any,rpcs).intern(); } - public static SONTypeRPC constant(int cend) { + public static TypeRPC constant(int cend) { HashSet rpcs = new HashSet<>(); rpcs.add(cend); return make(false,rpcs); } - public static final SONTypeRPC BOT = make(true,new HashSet<>()); - private static final SONTypeRPC TEST2 = constant(2); - private static final SONTypeRPC TEST3 = constant(2); + public static final TypeRPC BOT = make(true,new HashSet<>()); + private static final TypeRPC TEST2 = constant(2); + private static final TypeRPC TEST3 = constant(2); - public static void gather(ArrayList ts) { ts.add(BOT); ts.add(TEST2); ts.add(TEST3); } + public static void gather(ArrayList ts) { ts.add(BOT); ts.add(TEST2); ts.add(TEST3); } @Override public String str() { if( _rpcs.isEmpty() ) @@ -50,8 +50,8 @@ public static SONTypeRPC constant(int cend) { } @Override - public SONTypeRPC xmeet(SONType other) { - SONTypeRPC rpc = (SONTypeRPC)other; + public TypeRPC xmeet(Type other) { + TypeRPC rpc = (TypeRPC)other; // If the two sets are equal, the _any must be unequal (invariant), // so they cancel and all bits are set. if( _rpcs.equals(rpc._rpcs) ) @@ -84,13 +84,13 @@ public SONTypeRPC xmeet(SONType other) { } @Override - public SONType dual() { return make(!_any,_rpcs); } + public Type dual() { return make(!_any,_rpcs); } @Override int hash() { return _rpcs.hashCode() ^ (_any ? -1 : 0) ; } @Override - public boolean eq( SONType t ) { - SONTypeRPC rpc = (SONTypeRPC)t; // Contract + public boolean eq( Type t ) { + TypeRPC rpc = (TypeRPC)t; // Contract return _any==rpc._any && _rpcs.equals(rpc._rpcs); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeStruct.java similarity index 84% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeStruct.java index cd69999..fa44da4 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeStruct.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeStruct.java @@ -1,13 +1,13 @@ package com.compilerprogramming.ezlang.compiler.sontypes; import com.compilerprogramming.ezlang.compiler.SB; -import com.compilerprogramming.ezlang.compiler.Utils; + import java.util.ArrayList; /** * Represents a struct type. */ -public class SONTypeStruct extends SONType { +public class TypeStruct extends Type { // A Struct has a name and a set of fields; the fields themselves have // names, types and aliases. The name has no semantic meaning, but is @@ -25,21 +25,21 @@ public class SONTypeStruct extends SONType { public final String _name; public Field[] _fields; - public SONTypeStruct(String name, Field[] fields) { + public TypeStruct(String name, Field[] fields) { super(TSTRUCT); _name = name; _fields = fields; } // All fields directly listed - public static SONTypeStruct make(String name, Field... fields) { return new SONTypeStruct(name, fields).intern(); } - public static final SONTypeStruct TOP = make("$TOP",new Field[0]); - public static final SONTypeStruct BOT = make("$BOT",new Field[0]); + public static TypeStruct make(String name, Field... fields) { return new TypeStruct(name, fields).intern(); } + public static final TypeStruct TOP = make("$TOP",new Field[0]); + public static final TypeStruct BOT = make("$BOT",new Field[0]); //public static final SONTypeStruct TEST = make("test",new Field[]{Field.TEST}); // Forward-ref version - public static SONTypeStruct makeFRef(String name) { return make(name, (Field[])null); } + public static TypeStruct makeFRef(String name) { return make(name, (Field[])null); } // Make a read-only version - @Override public SONTypeStruct makeRO() { + @Override public TypeStruct makeRO() { if( isFinal() ) return this; Field[] flds = new Field[_fields.length]; for( int i=0; i ts) { /* ts.add(TEST); ts.add(BOT); ts.add(S1); ts.add(S2); ts.add(ARY); */ } + public static void gather(ArrayList ts) { /* ts.add(TEST); ts.add(BOT); ts.add(S1); ts.add(S2); ts.add(ARY); */ } // Find field index by name public int find(String fname) { @@ -97,8 +97,8 @@ public int findAlias( int alias ) { @Override - SONType xmeet(SONType t) { - SONTypeStruct that = (SONTypeStruct) t; + Type xmeet(Type t) { + TypeStruct that = (TypeStruct) t; if( this==TOP ) return that; if( that==TOP ) return this; if( this==BOT ) return BOT; @@ -127,7 +127,7 @@ SONType xmeet(SONType t) { } @Override - public SONTypeStruct dual() { + public TypeStruct dual() { if( this==TOP ) return BOT; if( this==BOT ) return TOP; if( _fields == null ) return this; @@ -138,7 +138,7 @@ public SONTypeStruct dual() { } // Keeps the same struct, but lower-bounds all fields. - @Override public SONTypeStruct glb() { + @Override public TypeStruct glb() { if( _glb() ) return this; // Need to glb each field Field[] flds = new Field[_fields.length]; @@ -166,8 +166,8 @@ private boolean _glb() { } @Override - boolean eq(SONType t) { - SONTypeStruct ts = (SONTypeStruct)t; // Invariant + boolean eq(Type t) { + TypeStruct ts = (TypeStruct)t; // Invariant if( !_name.equals(ts._name) ) return false; // if( _fields == ts._fields ) return true; diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeTuple.java similarity index 50% rename from seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java rename to seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeTuple.java index 73ea8f4..70f2762 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/SONTypeTuple.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/sontypes/TypeTuple.java @@ -3,61 +3,61 @@ import com.compilerprogramming.ezlang.compiler.SB; import java.util.ArrayList; -public class SONTypeTuple extends SONType { +public class TypeTuple extends Type { - public final SONType[] _types; + public final Type[] _types; - public SONTypeTuple(SONType[] types) { super(TTUPLE); _types = types; } - public static SONTypeTuple make(SONType... types) { return new SONTypeTuple(types).intern(); } + public TypeTuple(Type[] types) { super(TTUPLE); _types = types; } + public static TypeTuple make(Type... types) { return new TypeTuple(types).intern(); } - public static final SONTypeTuple BOT = new SONTypeTuple(new SONType[0]).intern(); - public static final SONTypeTuple TOP = new SONTypeTuple(null).intern(); + public static final TypeTuple BOT = new TypeTuple(new Type[0]).intern(); + public static final TypeTuple TOP = new TypeTuple(null).intern(); //public static final SONTypeTuple TEST = make(SONTypeInteger.BOT, SONTypeMemPtr.TEST); - public static final SONTypeTuple START= make(SONType.CONTROL, SONTypeMem.TOP, SONTypeInteger.BOT); - public static final SONTypeTuple MAIN = make(SONTypeInteger.BOT); - public static final SONTypeTuple RET = make(SONType.CONTROL, SONTypeMem.BOT, SONType.BOTTOM); - public static final SONTypeTuple CALLOC = make(SONTypeInteger.BOT,SONTypeInteger.BOT); + public static final TypeTuple START= make(Type.CONTROL, TypeMem.TOP, TypeInteger.BOT); + public static final TypeTuple MAIN = make(TypeInteger.BOT); + public static final TypeTuple RET = make(Type.CONTROL, TypeMem.BOT, Type.BOTTOM); + public static final TypeTuple CALLOC = make(TypeInteger.BOT, TypeInteger.BOT); - public static final SONTypeTuple IF_BOTH = make(new SONType[]{SONType. CONTROL, SONType. CONTROL}); - public static final SONTypeTuple IF_NEITHER = make(new SONType[]{SONType.XCONTROL, SONType.XCONTROL}); - public static final SONTypeTuple IF_TRUE = make(new SONType[]{SONType. CONTROL, SONType.XCONTROL}); - public static final SONTypeTuple IF_FALSE = make(new SONType[]{SONType.XCONTROL, SONType. CONTROL}); + public static final TypeTuple IF_BOTH = make(new Type[]{Type. CONTROL, Type. CONTROL}); + public static final TypeTuple IF_NEITHER = make(new Type[]{Type.XCONTROL, Type.XCONTROL}); + public static final TypeTuple IF_TRUE = make(new Type[]{Type. CONTROL, Type.XCONTROL}); + public static final TypeTuple IF_FALSE = make(new Type[]{Type.XCONTROL, Type. CONTROL}); - public static void gather(ArrayList ts) { ts.add(BOT); /* ts.add(TEST); */ ts.add(START); ts.add(MAIN); ts.add(IF_TRUE); } + public static void gather(ArrayList ts) { ts.add(BOT); /* ts.add(TEST); */ ts.add(START); ts.add(MAIN); ts.add(IF_TRUE); } @Override - SONType xmeet(SONType other) { - SONTypeTuple tt = (SONTypeTuple)other; // contract from xmeet + Type xmeet(Type other) { + TypeTuple tt = (TypeTuple)other; // contract from xmeet if( this==TOP ) return other; if( tt ==TOP ) return this ; if( _types.length != tt._types.length ) return BOT; - SONType[] ts = new SONType[_types.length]; + Type[] ts = new Type[_types.length]; for( int i=0; i<_types.length; i++ ) ts[i] = _types[i].meet(tt._types[i]); return make(ts); } - @Override public SONTypeTuple dual() { + @Override public TypeTuple dual() { if( this==TOP ) return BOT; if( this==BOT ) return TOP; - SONType[] ts = new SONType[_types.length]; + Type[] ts = new Type[_types.length]; for( int i=0; i<_types.length; i++ ) ts[i] = _types[i].dual(); return make(ts); } @Override - public SONType glb() { - SONType[] ts = new SONType[_types.length]; + public Type glb() { + Type[] ts = new Type[_types.length]; for( int i=0; i<_types.length; i++ ) ts[i] = _types[i].glb(); return make(ts); } @Override public boolean isConstant() { - for( SONType t : _types ) + for( Type t : _types ) if( !t.isConstant() ) return false; return true; @@ -66,12 +66,12 @@ public SONType glb() { @Override public int log_size() { assert isConstant(); int log_size = 0; - for( SONType t : _types ) + for( Type t : _types ) log_size = Math.max(log_size,t.log_size()); return log_size; } - public SONType ret() { assert _types.length==3; return _types[2]; } + public Type ret() { assert _types.length==3; return _types[2]; } @Override public String str() { return print(new SB()).toString(); } @@ -79,7 +79,7 @@ public SONType glb() { if( this==TOP ) return sb.p("[TOP]"); if( this==BOT ) return sb.p("[BOT]"); sb.p("[ "); - for( SONType t : _types ) + for( Type t : _types ) t.print(sb).p(", "); return sb.unchar(2).p("]"); } @@ -87,7 +87,7 @@ public SONType glb() { if( this==TOP ) return sb.p("[TOP]"); if( this==BOT ) return sb.p("[BOT]"); sb.p("[ "); - for( SONType t : _types ) + for( Type t : _types ) t.gprint(sb).p(", "); return sb.unchar(2).p("]"); } @@ -95,13 +95,13 @@ public SONType glb() { @Override int hash() { int sum = 0; - if( _types!=null ) for( SONType type : _types ) sum ^= type.hashCode(); + if( _types!=null ) for( Type type : _types ) sum ^= type.hashCode(); return sum; } @Override - boolean eq( SONType t ) { - SONTypeTuple tt = (SONTypeTuple)t; // Contract + boolean eq( Type t ) { + TypeTuple tt = (TypeTuple)t; // Contract if( _types==null && tt._types==null ) return true; if( _types==null || tt._types==null ) return false; if( _types.length != tt._types.length ) return false; From e8eb5df1f7dd5e3a3880aaf1bbba549ea5672a7e Mon Sep 17 00:00:00 2001 From: dibyendumajumdar Date: Sun, 27 Jul 2025 20:09:44 +0100 Subject: [PATCH 3/9] WIP update --- .../ezlang/compiler/Compiler.java | 2 +- .../ezlang/compiler/Var.java | 4 - .../ezlang/compiler/codegen/CodeGen.java | 41 +++--- .../ezlang/compiler/codegen/ElfFile.java | 9 +- .../ezlang/compiler/codegen/Encoding.java | 118 +++++++++++++----- .../compiler/codegen/GlobalCodeMotion.java | 17 ++- .../ezlang/compiler/codegen/RegAlloc.java | 28 +++-- .../ezlang/compiler/nodes/AddNode.java | 56 +++------ .../ezlang/compiler/nodes/AndNode.java | 27 ++-- .../ezlang/compiler/nodes/ArithNode.java | 98 +++++++++++++++ .../ezlang/compiler/nodes/BoolNode.java | 22 +++- .../ezlang/compiler/nodes/CFGNode.java | 14 ++- .../ezlang/compiler/nodes/CallEndMach.java | 36 ++++++ .../ezlang/compiler/nodes/CallEndNode.java | 39 ++---- .../ezlang/compiler/nodes/CallNode.java | 10 +- .../{cpus/arm/CastARM.java => CastMach.java} | 13 +- .../ezlang/compiler/nodes/CastNode.java | 5 + .../ezlang/compiler/nodes/ConstantNode.java | 4 +- .../ezlang/compiler/nodes/DivNode.java | 30 +---- .../ezlang/compiler/nodes/ExternNode.java | 19 +++ .../ezlang/compiler/nodes/FunNode.java | 13 +- .../ezlang/compiler/nodes/IfNode.java | 2 +- .../ezlang/compiler/nodes/LoadNode.java | 6 + .../ezlang/compiler/nodes/LogicalNode.java | 28 ----- .../ezlang/compiler/nodes/LoopNode.java | 2 +- .../ezlang/compiler/nodes/MemMergeNode.java | 39 ++++-- .../ezlang/compiler/nodes/MemOpNode.java | 3 +- .../ezlang/compiler/nodes/MinusNode.java | 2 +- .../ezlang/compiler/nodes/MulNode.java | 36 +----- .../ezlang/compiler/nodes/NewNode.java | 6 +- .../ezlang/compiler/nodes/Node.java | 14 ++- .../ezlang/compiler/nodes/OrNode.java | 18 +-- .../ezlang/compiler/nodes/ParmNode.java | 4 + .../ezlang/compiler/nodes/PhiNode.java | 11 +- .../riscv/CastRISC.java => ReadOnlyMach.java} | 13 +- .../ezlang/compiler/nodes/ReadOnlyNode.java | 1 + .../ezlang/compiler/nodes/ReturnNode.java | 7 +- .../ezlang/compiler/nodes/SarNode.java | 25 ++-- .../ezlang/compiler/nodes/ScopeNode.java | 39 +++--- .../ezlang/compiler/nodes/ShlNode.java | 26 ++-- .../ezlang/compiler/nodes/ShrNode.java | 21 +--- .../ezlang/compiler/nodes/StartNode.java | 2 +- .../ezlang/compiler/nodes/StopNode.java | 20 ++- .../ezlang/compiler/nodes/StoreNode.java | 29 ++++- .../ezlang/compiler/nodes/SubNode.java | 38 ++---- .../ezlang/compiler/nodes/XorNode.java | 17 +-- .../compiler/nodes/cpus/arm/CallARM.java | 9 +- .../compiler/nodes/cpus/arm/CallEndARM.java | 8 -- .../compiler/nodes/cpus/arm/FloatARM.java | 2 - .../compiler/nodes/cpus/arm/IntARM.java | 2 +- .../compiler/nodes/cpus/arm/TFPARM.java | 13 +- .../compiler/nodes/cpus/arm/TMPARM.java | 53 ++++++++ .../ezlang/compiler/nodes/cpus/arm/arm.java | 94 +++++++------- .../nodes/cpus/riscv/CallEndRISC.java | 8 -- .../compiler/nodes/cpus/riscv/CallRISC.java | 20 +-- .../compiler/nodes/cpus/riscv/FltRISC.java | 3 +- .../compiler/nodes/cpus/riscv/ImmRISC.java | 3 +- .../compiler/nodes/cpus/riscv/Int8RISC.java | 47 +++++++ .../compiler/nodes/cpus/riscv/IntRISC.java | 2 +- .../ezlang/compiler/nodes/cpus/riscv/LUI.java | 2 +- .../compiler/nodes/cpus/riscv/MemOpRISC.java | 12 +- .../compiler/nodes/cpus/riscv/SraIRISC.java | 4 +- .../compiler/nodes/cpus/riscv/TFPRISC.java | 11 +- .../compiler/nodes/cpus/riscv/TMPRISC.java | 41 ++++++ .../compiler/nodes/cpus/riscv/XorIRISC.java | 1 + .../compiler/nodes/cpus/riscv/riscv.java | 34 ++--- .../nodes/cpus/x86_64_v2/AddIX86.java | 9 ++ .../nodes/cpus/x86_64_v2/CallEndX86.java | 8 -- .../nodes/cpus/x86_64_v2/CallX86.java | 10 +- .../nodes/cpus/x86_64_v2/CastX86.java | 16 --- .../nodes/cpus/x86_64_v2/CmpMemX86.java | 4 +- .../compiler/nodes/cpus/x86_64_v2/CmpX86.java | 6 + .../compiler/nodes/cpus/x86_64_v2/DivX86.java | 2 +- .../compiler/nodes/cpus/x86_64_v2/ImmX86.java | 3 +- .../compiler/nodes/cpus/x86_64_v2/IntX86.java | 2 +- .../nodes/cpus/x86_64_v2/LoadX86.java | 2 +- .../nodes/cpus/x86_64_v2/MemAddX86.java | 5 +- .../nodes/cpus/x86_64_v2/MulIX86.java | 35 +++++- .../compiler/nodes/cpus/x86_64_v2/RegX86.java | 1 + .../compiler/nodes/cpus/x86_64_v2/SetX86.java | 4 +- .../nodes/cpus/x86_64_v2/StoreX86.java | 2 +- .../compiler/nodes/cpus/x86_64_v2/TFPX86.java | 3 +- .../compiler/nodes/cpus/x86_64_v2/TMPX86.java | 35 ++++++ .../nodes/cpus/x86_64_v2/x86_64_v2.java | 36 +++--- .../ezlang/compiler/print/ASMPrinter.java | 90 +++++++------ .../ezlang/compiler/sontypes/Field.java | 14 ++- .../ezlang/compiler/sontypes/Type.java | 10 +- .../ezlang/compiler/sontypes/TypeFloat.java | 2 +- .../ezlang/compiler/sontypes/TypeFunPtr.java | 20 +-- .../ezlang/compiler/sontypes/TypeInteger.java | 10 +- .../ezlang/compiler/sontypes/TypeMem.java | 8 +- .../ezlang/compiler/sontypes/TypeMemPtr.java | 5 +- .../ezlang/compiler/sontypes/TypeNil.java | 2 +- .../ezlang/compiler/sontypes/TypePtr.java | 11 +- .../ezlang/compiler/sontypes/TypeStruct.java | 50 +++++--- .../ezlang/compiler/sontypes/TypeTuple.java | 16 +-- 96 files changed, 1098 insertions(+), 706 deletions(-) create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ArithNode.java create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/CallEndMach.java rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/{cpus/arm/CastARM.java => CastMach.java} (58%) create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ExternNode.java delete mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/LogicalNode.java rename seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/{cpus/riscv/CastRISC.java => ReadOnlyMach.java} (57%) delete mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallEndARM.java create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TMPARM.java delete mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallEndRISC.java create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/Int8RISC.java create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TMPRISC.java delete mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallEndX86.java delete mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CastX86.java create mode 100644 seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TMPX86.java diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java index 87b9624..b184240 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Compiler.java @@ -656,7 +656,7 @@ private Node compileCallExpr(AST.CallExpr callExpr) { if( expr._type == Type.NIL ) throw error("Calling a null function pointer"); if( !(expr instanceof FRefNode) && !expr._type.isa(TypeFunPtr.BOT) ) - throw error("Expected a function but got "+expr._type.glb().str()); + throw error("Expected a function but got "+expr._type.glb(false).str()); expr.keep(); // Keep while parsing args Ary args = new Ary(Node.class); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java index 29327fa..65d82ee 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/Var.java @@ -30,10 +30,6 @@ public Type type() { Type def = Compiler.TYPES.get(((TypeMemPtr)_type)._obj._name); return (_type=_type.meet(def)); } - public Type lazyGLB() { - Type t = type(); - return t instanceof TypeMemPtr ? t : t.glb(); - } // Forward reference variables (not types) must be BOTTOM and // distinct from inferred variables diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java index b5666e6..e1dfe42 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/CodeGen.java @@ -23,7 +23,7 @@ public enum Phase { Opto, // Run ideal optimizations TypeCheck, // Last check for bad programs LoopTree, // Build a loop tree; break infinite loops - InstSelect, // Convert to target hardware nodes + Select, // Convert to target hardware nodes Schedule, // Global schedule (code motion) nodes LocalSched, // Local schedule RegAlloc, // Register allocation @@ -40,7 +40,7 @@ public enum Phase { // --------------------------- public CodeGen( String src ) { this(src, TypeInteger.BOT, 123L ); } - public CodeGen(String src, TypeInteger arg, long workListSeed ) { + public CodeGen( String src, TypeInteger arg, long workListSeed ) { CODE = this; _phase = null; _callingConv = null; @@ -57,14 +57,15 @@ public CodeGen(String src, TypeInteger arg, long workListSeed ) { public CodeGen driver( Phase phase ) { return driver(phase,null,null); } public CodeGen driver( Phase phase, String cpu, String callingConv ) { if( _phase==null ) parse(); - if( _phase.ordinal() < phase.ordinal() ) opto(); - if( _phase.ordinal() < phase.ordinal() ) typeCheck(); - if( _phase.ordinal() < phase.ordinal() ) loopTree(); - if( _phase.ordinal() < phase.ordinal() && cpu != null ) instSelect(cpu,callingConv); - if( _phase.ordinal() < phase.ordinal() ) GCM(); - if( _phase.ordinal() < phase.ordinal() ) localSched(); - if( _phase.ordinal() < phase.ordinal() ) regAlloc(); - if( _phase.ordinal() < phase.ordinal() ) encode(); + int p1 = phase.ordinal(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.Opto .ordinal() ) opto(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.TypeCheck .ordinal() ) typeCheck(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.LoopTree .ordinal() ) loopTree(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.Select .ordinal() && cpu != null ) instSelect(cpu,callingConv); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.Schedule .ordinal() ) GCM(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.LocalSched.ordinal() ) localSched(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.RegAlloc .ordinal() ) regAlloc(); + if( _phase.ordinal() < p1 && _phase.ordinal() < Phase.Encoding .ordinal() ) encode(); return this; } @@ -123,7 +124,7 @@ public int iDepthFrom(int idepth) { // Compute "function indices": FIDX. // Each new request at the same signature gets a new FIDX. private final HashMap FIDXS = new HashMap<>(); - public TypeFunPtr makeFun(TypeTuple sig, Type ret ) { + public TypeFunPtr makeFun( TypeTuple sig, Type ret ) { Integer i = FIDXS.get(sig); int fidx = i==null ? 0 : i; FIDXS.put(sig,fidx+1); // Track count per sig @@ -229,7 +230,7 @@ public CodeGen loopTree() { _phase = Phase.LoopTree; long t0 = System.currentTimeMillis(); // Build the loop tree, fix never-exit loops - _start.buildLoopTree(_stop); + _start.buildLoopTree(_start,_stop); _tLoopTree = (int)(System.currentTimeMillis() - t0); return this; } @@ -257,7 +258,7 @@ public CodeGen loopTree() { public CodeGen instSelect( String cpu, String callingConv ) { return instSelect(cpu,callingConv,PORTS); } public CodeGen instSelect( String cpu, String callingConv, String base ) { assert _phase.ordinal() == Phase.LoopTree.ordinal(); - _phase = Phase.InstSelect; + _phase = Phase.Select; _callingConv = callingConv; @@ -288,14 +289,14 @@ public CodeGen instSelect( String cpu, String callingConv, String base ) { _rpcMask = new RegMask(_mach.rpc()); _retMasks[3] = _rpcMask; - // Convert to machine ops long t0 = System.currentTimeMillis(); _uid = 1; // All new machine nodes reset numbering var map = new IdentityHashMap(); _instSelect( _stop, map ); _stop = ( StopNode)map.get(_stop ); - _start = (StartNode)map.get(_start); + StartNode start = (StartNode)map.get(_start); + _start = start==null ? new StartNode(_start) : start; _instOuts(_stop,visit()); _visit.clear(); _tInsSel = (int)(System.currentTimeMillis() - t0); @@ -326,8 +327,6 @@ private Node _instSelect( Node n, IdentityHashMap map ) { if( x instanceof MachNode mach ) mach.postSelect(this); // Post selection action - // Updates forward edges only. - n._outputs.clear(); return x; } @@ -351,7 +350,7 @@ private void _instOuts( Node n, BitSet visit ) { // Global schedule (code motion) nodes public CodeGen GCM() { return GCM(false); } public CodeGen GCM( boolean show) { - assert _phase.ordinal() <= Phase.InstSelect.ordinal(); + assert _phase.ordinal() <= Phase.Select.ordinal(); _phase = Phase.Schedule; long t0 = System.currentTimeMillis(); @@ -451,8 +450,10 @@ public CodeGen exportELF(String fname) throws IOException { String printCFG() { if( _cfg==null ) return "no CFG"; SB sb = new SB(); - for( CFGNode cfg : _cfg ) - IRPrinter.printLine(cfg,sb); + for( CFGNode cfg : _cfg ) { + sb.fix(8,""+cfg._idepth); + IRPrinter.printLine( cfg, sb ); + } return sb.toString(); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java index 54bd967..d5b3462 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/ElfFile.java @@ -273,8 +273,6 @@ public void export(String fname) throws IOException { int start_global = num+1; // Add one to skip the final .rela.text local symbol for( Symbol a: symbols._symbols ) a._index = start_global++; - int bigConIdx = start_global; - start_global += enc._bigCons.size(); // create .text relocations DataSection text_rela = new DataSection(".rela.text", 4 /* SHT_RELA */); @@ -298,10 +296,10 @@ public void export(String fname) throws IOException { // Write relocations for the constant pool for( Encoding.Relo relo : enc._bigCons.values() ) { - Symbol glob = new Symbol("GLOB$"+bigConIdx, rdata._index, SYM_BIND_GLOBAL, SYM_TYPE_FUNC); + Symbol glob = new Symbol("CPOOL$"+start_global, rdata._index, SYM_BIND_GLOBAL, SYM_TYPE_FUNC); glob._value = relo._target; - glob._size = 1 << relo._t.log_size(); - glob._index = bigConIdx++; + glob._size = relo._t.size(); + glob._index = start_global++; symbols.push(glob); write8(text_rela._contents, relo._opStart+relo._off); write8(text_rela._contents, ((long)glob._index << 32L) | relo._elf ); @@ -313,6 +311,7 @@ public void export(String fname) throws IOException { text_rela._info = text._index; pushSection(text_rela); + // Final .rela.text symbol Symbol sym = new Symbol(text_rela._name, num++, SYM_BIND_LOCAL, SYM_TYPE_SECTION); sym._name_pos = text_rela._name_pos; sym._size = text_rela.size(); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java index b37e8fe..68c7f7f 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/Encoding.java @@ -98,7 +98,7 @@ static void padN(int n, BAOS bits) { } // Convenience for writing log-N - static void addN(int log, Type t, BAOS bits ) { + static void addN( int log, Type t, BAOS bits ) { long x = t instanceof TypeInteger ti ? ti.value() : log==3 @@ -138,14 +138,14 @@ public void jump( CFGNode jmp, CFGNode dst ) { final HashMap _externals = new HashMap<>(); - public Encoding external( Node call, String extern ) { - _externals.put(call,extern); + public Encoding external( Node n, String extern ) { + _externals.put(n,extern); return this; } // Store t as a 32/64 bit constant in the code space; generate RIP-relative // addressing to load it - public void largeConstant(Node relo, Type t, int off, int elf ) { + public void largeConstant( Node relo, Type t, int off, int elf ) { assert t.isConstant(); assert (byte)off == off; assert (byte)elf == elf; @@ -183,12 +183,10 @@ private void basicBlockLayout() { Ary rpo = new Ary<>(CFGNode.class); rpos.put(_code._start.loop(),rpo); BitSet visit = _code.visit(); - //IdentityHashMap looptail = new IdentityHashMap<>(); rpo.add(_code._stop); for( Node n : _code._start._outputs ) if( n instanceof FunNode fun ) { int x = rpo._len; - //_rpo_cfg2(fun, visit, rpo, looptail); _rpo_cfg(fun, visit, rpos ); assert rpo.at(x) instanceof ReturnNode; } @@ -299,7 +297,7 @@ private static boolean shouldInvert(CFGNode t, CFGNode f, int bld) { private static boolean forwardsEmptyScan( CFGNode c, int bld ) { if( c.nOuts()!=1 || c.loopDepth()!=bld ) return false; return c.uctrl() instanceof RegionNode cfg && - (cfg instanceof LoopNode || forwardsEmptyScan(cfg,bld)); + ((cfg instanceof LoopNode && cfg._ltree==c._ltree) || forwardsEmptyScan(cfg,bld)); } // Is the CFG from "next" to the end empty? This means jumping to "next" @@ -387,6 +385,23 @@ private void compactShortForm() { } + // Go again, inserting padding on function headers. Since no + // short-jumps span function headers, the padding will not make any + // short jumps fail. + for( int i=0; i 0 ) { // If no short-form ops, nothing to do here @@ -422,47 +437,90 @@ void patchLocalRelocations() { void writeConstantPool( BAOS bits, boolean patch ) { padN(16,bits); + // radix sort the big constants by alignment + Ary[] raligns = new Ary[5]; + for( Node op : _bigCons.keySet() ) { + Relo relo = _bigCons.get(op); + int align = relo._t.alignment(); + Ary relos = raligns[align]==null ? (raligns[align]=new Ary<>(Relo.class)) : raligns[align]; + relos.add(relo); + } + + + // Types can be used more than once; collapse the dups HashMap targets = new HashMap<>(); - // By log size - for( int log = 3; log >= 0; log-- ) { - // Write the 8-byte constants - for( Node op : _bigCons.keySet() ) { - Relo relo = _bigCons.get(op); - if( relo._t.log_size()==log ) { - // Map from relo to constant start and patch - Integer target = targets.get(relo._t); - if( target==null ) { - targets.put(relo._t,target = bits.size()); - // Put constant into code space. - if( relo._t instanceof TypeTuple tt ) // Constant tuples put all entries - for( Type tx : tt._types ) - addN(log,tx,bits); - else - addN(log,relo._t,bits); + // By alignment + for( int align = 4; align >= 0; align-- ) { + Ary relos = raligns[align]; + if( relos == null ) continue; + for( Relo relo : relos ) { + // Map from relo to constant start and patch + Integer target = targets.get(relo._t); + if( target==null ) { + targets.put(relo._t,target = bits.size()); + // Write constant into constant pool + switch( relo._t ) { + case TypeTuple tt -> cpool(align,bits,tt); + case TypeStruct ts -> cpool(bits,ts); + // Simple primitive (e.g. larger int, float) + default -> addN(align,relo._t,bits); } - relo._target = target; - relo._opStart= _opStart[op._nid]; - // Go ahead and locally patch in-memory - if( patch ) - ((RIPRelSize)op).patch(this, relo._opStart, _opLen[op._nid], relo._target - relo._opStart); } + // Record target address and opcode start + relo._target = target; + relo._opStart= _opStart[relo._op._nid]; + // Go ahead and locally patch in-memory + if( patch ) + ((RIPRelSize)relo._op).patch(this, relo._opStart, _opLen[relo._op._nid], relo._target - relo._opStart); } } } + // Constant tuples put all entries at same alignment + private void cpool(int align, BAOS bits, TypeTuple tt) { + for( Type tx : tt._types ) + addN(align,tx,bits); + } + + // Structs use internal field layout + private void cpool( BAOS bits, TypeStruct ts ) { + // Field order by offset + int[] layout = ts.layout(); + int off=0; // offset in the struct + for( int fn=0; fn SENTINAL_CALLOC; + case "calloc" -> SENTINEL_CALLOC; + case "write" -> SENTINEL_WRITE ; default -> throw Utils.TODO(); }; ((RIPRelSize)src).patch(this, start, _opLen[src._nid], target - start); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java index 1611468..f6f2f2c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/GlobalCodeMotion.java @@ -32,7 +32,6 @@ public static void buildCFG( CodeGen code ) { // Post-Order of CFG private static void _rpo_cfg(CFGNode def, Node use, BitSet visit, Ary rpo) { - if( use instanceof CallNode call ) call.unlink_all(); if( !(use instanceof CFGNode cfg) || visit.get(cfg._nid) ) return; // Been there, done that if( def instanceof ReturnNode && use instanceof CallEndNode ) @@ -178,14 +177,22 @@ private static void breadth(Node stop, Node[] ns, CFGNode[] late) { } // Walk all inputs and put on worklist, as their last-use might now be done - for( Node def : n._inputs ) - if( def!=null && late[def._nid]==null ) { + for( Node def : n._inputs ) { + if( def!=null && late[def._nid]==null ) work.push(def); - // if the def has a load use, maybe the load can fire + // if the def has a load use, maybe the load can fire + if( def!=null ) for( Node out : def._outputs ) if( out instanceof MemOpNode ld && ld._isLoad && late[ld._nid]==null ) work.push(ld); + } + + if( n instanceof LoopNode loop ) { + for( Node phi : loop._outputs ) { + if( phi instanceof PhiNode && late[phi._nid]==null ) + work.push(phi); } + } } } @@ -274,7 +281,7 @@ private static CFGNode find_anti_dep(CFGNode lca, MemOpNode load, CFGNode early, for( Node mem : load.mem()._outputs ) { switch( mem ) { case MemOpNode st: - if( !st._isLoad ) { + if( !st._isLoad && load._alias == st._alias ) { assert late[mem._nid] != null; lca = anti_dep( load, late[mem._nid], mem.cfg0(), lca, st ); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/RegAlloc.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/RegAlloc.java index 0c778d5..c75d6c4 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/RegAlloc.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/codegen/RegAlloc.java @@ -162,7 +162,7 @@ public void regAlloc() { } // Cache reg masks for New and Call for( CFGNode bb : _code._cfg ) { - if( bb instanceof CallEndNode cend ) cend.cacheRegs(_code); + if( bb instanceof CallEndMach cend ) cend.cacheRegs(_code); for( Node n : bb._outputs ) if( n instanceof NewNode nnn ) nnn.cacheRegs(_code); } @@ -287,24 +287,25 @@ boolean splitEmptyMaskByUse( byte round, LRG lrg ) { // all registers anyways. if( rclass._len <= 1 || ncalls > 1 ) return false; - // Split by classh + // Split by class Ary ns = new Ary<>(Node.class); for( RegMask rmask : rclass ) { ns.addAll(def._outputs); - Node split = makeSplit(def,"popular",round,lrg); + Node split = makeSplit(def,"popular",round,lrg,rmask); split.insertAfter( def ); if( split.nIns()>1 ) split.setDef(1,def); // all uses by class to split - for( Node use : ns ) { + for( int j=0; j < def._outputs._len; j++ ) { + Node use = def._outputs.at(j); if( use instanceof MachNode mach && use!=split ) { // Check all use inputs for n, in case there's several for( int i = 1; i < use.nIns(); i++ ) // Find a def input, and check register class - if( use.in( i ) == def ) { + if( use.in( i ) == def && mach.regmap( i ).overlap( rmask ) ) { RegMask m = mach.regmap( i ); if( m!=null && mach.regmap( i ).overlap( rmask ) ) // Modify use to use the split version specialized to this rclass - use.setDefOrdered( i, split ); + { use.setDefOrdered( i, split ); j--; break; } } } } @@ -500,15 +501,17 @@ void insertBefore(Node n, int i, String kind, byte round, LRG lrg, boolean skip) Node def = n.in(i); // Effective block for use CFGNode cfg = n instanceof PhiNode phi ? phi.region().cfg(i) : n.cfg0(); + // Use-side RegMask, if available + RegMask umask = n instanceof MachNode mach ? mach.regmap(i) : null; // Def is a split ? if( skip && def instanceof SplitNode ) { - boolean singleReg = n instanceof MachNode mach && mach.regmap(i).size1(); + boolean singleReg = umask!=null && umask.size1(); // Same block, multiple registers, split is only used by n, // assume this is good enough and do not split again. if( cfg==def.cfg0() && def.nOuts()==1 && !singleReg ) return; } - makeSplit(def,kind,round,lrg).insertBefore(n, i); + makeSplit(def,kind,round,lrg,umask).insertBefore(n, i); // Skip split-of-split same block if( skip && def instanceof SplitNode && cfg==def.cfg0() ) n.in(i).setDefOrdered(1,def.in(1)); @@ -535,8 +538,9 @@ public void insertAfterAndReplace( Node split, Node def, boolean must ) { } } - private Node makeSplit( Node def, String kind, byte round, LRG lrg ) { - Node split = def instanceof MachNode mach && mach.isClone() + private Node makeSplit( Node def, String kind, byte round, LRG lrg, RegMask umask ) { + // Clone simple constants if possible + Node split = def instanceof MachNode mach && mach.isClone() && (umask==null || mach.outregmap().overlap(umask)) ? mach.copy() : _code._mach.split(kind,round,lrg); _lrgs.put(split,lrg); @@ -554,13 +558,13 @@ private SplitNode makeSplit( String kind, byte round, LRG lrg ) { private void postColor() { int maxReg = -1; for( CFGNode bb : _code._cfg ) { // For all ops - if( bb instanceof FunNode fun ) + if( bb instanceof FunNode ) maxReg = -1; // Reset for new function // Compute frame size, based on arguments and largest reg seen if( bb instanceof ReturnNode ret ) ret.fun().computeFrameAdjust(_code,maxReg); // Raise frame size by max stack args passed, even if ignored - if( bb instanceof CallEndNode cend ) + if( bb instanceof CallEndMach cend ) maxReg = Math.max(maxReg,cend._xslot); for( int j=0; j 2 ) { @@ -174,8 +153,8 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) { if( lo._type.isConstant() ) return false; if( hi._type.isConstant() ) return true ; - if( lo instanceof PhiNode lphi && lphi.region()._type== Type.XCONTROL ) return false; - if( hi instanceof PhiNode hphi && hphi.region()._type== Type.XCONTROL ) return false; + if( lo instanceof PhiNode lphi && lphi.region()._type==Type.XCONTROL ) return false; + if( hi instanceof PhiNode hphi && hphi.region()._type==Type.XCONTROL ) return false; if( lo instanceof PhiNode && lo.allCons(dep) ) return false; if( hi instanceof PhiNode && hi.allCons(dep) ) return true ; @@ -189,11 +168,4 @@ static boolean spine_cmp( Node hi, Node lo, Node dep ) { @Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); } @Override Node copyF() { return new AddFNode(null,null); } -// FIXME Dibyendu -// @Override public Parser.ParseException err() { -// if( in(1)._type.isHigh() || in(2)._type.isHigh() ) return null; -// if( !(in(1)._type instanceof SONTypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(1)._type,null); -// if( !(in(2)._type instanceof SONTypeInteger) ) return Parser.error("Cannot '"+label()+"' " + in(2)._type,null); -// return null; -// } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java index fb35217..9308283 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/AndNode.java @@ -3,28 +3,18 @@ import com.compilerprogramming.ezlang.compiler.sontypes.Type; import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; -public class AndNode extends LogicalNode { +public class AndNode extends ArithNode { public AndNode(Node lhs, Node rhs) { super(lhs, rhs); } @Override public String label() { return "And"; } @Override public String op() { return "&"; } - @Override public String glabel() { return "&"; } - @Override - public Type compute() { - Type t1 = in(1)._type, t2 = in(2)._type; - if( t1.isHigh() || t2.isHigh() ) - return TypeInteger.TOP; - if( t1 instanceof TypeInteger i0 && - t2 instanceof TypeInteger i1 ) { - if( i0.isConstant() && i1.isConstant() ) - return TypeInteger.constant(i0.value()&i1.value()); - // Sharpen allowed bits if either value is narrowed - long mask = i0.mask() & i1.mask(); - return mask < 0 ? TypeInteger.BOT : TypeInteger.make(0,mask); - } - return TypeInteger.BOT; + @Override long doOp( long x, long y ) { return x & y; } + @Override TypeInteger doOp(TypeInteger x, TypeInteger y) { + // Sharpen allowed bits if either value is narrowed + long mask = x.mask() & y.mask(); + return mask < 0 ? TypeInteger.BOT : TypeInteger.make(0,mask); } @Override @@ -50,9 +40,10 @@ public Node idealize() { // Do we have ((x & (phi cons)) & (phi cons)) ? // Push constant up through the phi: x & (phi con0&con0 con1&con1...) Node phicon = AddNode.phiCon(this,true); - if( phicon!=null ) return phicon; + if( phicon!=null ) + return phicon; - return null; + return super.idealize(); } @Override Node copy(Node lhs, Node rhs) { return new AndNode(lhs,rhs); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ArithNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ArithNode.java new file mode 100644 index 0000000..8cc3854 --- /dev/null +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ArithNode.java @@ -0,0 +1,98 @@ +package com.compilerprogramming.ezlang.compiler.nodes; + +import com.compilerprogramming.ezlang.compiler.Compiler; +import com.compilerprogramming.ezlang.compiler.sontypes.Type; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; +import com.compilerprogramming.ezlang.exceptions.CompilerException; + +import java.util.BitSet; + +public abstract class ArithNode extends Node { + // Source location for late reported errors + + public ArithNode(Node lhs, Node rhs) { super(lhs, rhs); } + @Override public String glabel() { return op(); } + abstract String op(); + + @Override + public StringBuilder _print1(StringBuilder sb, BitSet visited) { + in(1)._print0(sb.append("("), visited); + in(2)._print0(sb.append(op()), visited); + return sb.append(")"); + } + + abstract long doOp(long x, long y); + abstract TypeInteger doOp(TypeInteger x, TypeInteger y); + + // Generic airthmetic op math: high returns high; low returns low; 2 + // constants fold; only 2 non-constants call specialized math. + @Override + public final TypeInteger compute() { + Type t1 = in(1)._type, t2 = in(2)._type; + if( t1.isHigh() || t2.isHigh() ) + return TypeInteger.TOP; + if( t1 instanceof TypeInteger x && + t2 instanceof TypeInteger y ) + return x.isConstant() && y.isConstant() + ? con(x,y) + : doOp(x,y); + return TypeInteger.BOT; + } + + private TypeInteger con( Type t1, Type t2 ) { + return TypeInteger.constant( doOp( ((TypeInteger)t1).value(), ((TypeInteger)t2).value()) ); + } + + // Check for "op(Phi(con,x),Phi(con,y))" and push-up through the Phi. + // Note that this is the exact reverse of Phi pulling a common op down + // to reduce total op-count. We don't get in an endless push-up + // push-down peephole cycle because the constants all fold first. + // Returns "Phi(op(con,con),op(x,y))". + + // This check is slightly different from the + + // Expected to be called after other ideal checks are done. + @Override public Node idealize() { + if( in(1) instanceof PhiNode lhs && + in(2) instanceof PhiNode rhs && + lhs.nIns() >= 2 && !lhs.inProgress() && + lhs.region()==rhs.region() ) { + // Profit check: only 1 instance of `this` will remain, all the + // others will fold to constants. + int cnt=0; + for( int i=1; i=nIns() || in(i)==null ) + setDefX(i,mem.in(i)); + } + } + setDef(1,mem.in(1)); + return this; + } + + return progress ? this : null; } public Node in( Var v ) { return in(v._idx); } public Node alias( int alias ) { - return in(alias _outputs; + public Ary _outputs; /** @@ -67,7 +67,7 @@ public abstract class Node implements Cloneable { // Make a Node using the existing arrays of nodes. // Used by any pass rewriting all Node classes but not the edges. Node( Node n ) { - assert CodeGen.CODE._phase.ordinal() >= CodeGen.Phase.InstSelect.ordinal(); + assert CodeGen.CODE._phase.ordinal() >= CodeGen.Phase.Select.ordinal(); _nid = CODE.getUID(); // allocate unique dense ID _inputs = new Ary<>(n==null ? new Node[0] : n._inputs.asAry()); _outputs = new Ary<>(Node.class); @@ -240,7 +240,7 @@ protected boolean delUse( Node use ) { // Shortcut for "popping" until n nodes. A "pop" is basically a // setDef(last,null) followed by lowering the nIns() count. - void popUntil(int n) { + public void popUntil(int n) { unlock(); while( nIns() > n ) { Node old_def = _inputs.pop(); @@ -256,9 +256,9 @@ void popUntil(int n) { * code elimination. */ public void kill( ) { + assert isUnused(); // Has no uses, so it is dead unlock(); moveDepsToWorklist(); - assert isUnused(); // Has no uses, so it is dead _type=null; // Flag as dead while( nIns()>0 ) { // Set all inputs to null, recursively killing unused Nodes Node old_def = _inputs.removeLast(); @@ -296,6 +296,10 @@ public void unkill() { if( unkeep().isUnused() ) kill(); } + public void isKill() { + if( isUnused() ) + kill(); + } // Replace self with nnn in the graph, making 'this' go dead @@ -640,7 +644,7 @@ public final Node widen() { } private boolean hasFloatInput() { for( int i=1; i 1 ) { // Too many users, but addDep in case lose users for( Node out : op._outputs ) if( out!=null && out!=this ) addDep(out); return false; } + for( int j=1; j>"; } - @Override public String glabel() { return ">>"; } - @Override - public Type compute() { - Type t1 = in(1)._type, t2 = in(2)._type; - if( t1.isHigh() || t2.isHigh() ) - return TypeInteger.TOP; - if (t1 instanceof TypeInteger i0 && - t2 instanceof TypeInteger i1) { - if( i0 == TypeInteger.ZERO ) - return TypeInteger.ZERO; - if( i0.isConstant() && i1.isConstant() ) - return TypeInteger.constant(i0.value()>>i1.value()); - if( i1.isConstant() ) { - int log = (int)i1.value(); - return TypeInteger.make(-1L<<(63-log),(1L<<(63-log))-1); - } + @Override long doOp( long x, long y ) { return x >> y; } + @Override TypeInteger doOp( TypeInteger x, TypeInteger y ) { + if( y.isConstant() ) { + int log = (int)y.value(); + return TypeInteger.make(-1L<<(63-log),(1L<<(63-log))-1); } return TypeInteger.BOT; } @@ -42,7 +31,7 @@ public Node idealize() { // TODO: x >> 3 >> (y ? 1 : 2) ==> x >> (y ? 4 : 5) - return null; + return super.idealize(); } @Override Node copy(Node lhs, Node rhs) { return new SarNode(lhs,rhs); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java index bf3b53e..525acd3 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ScopeNode.java @@ -49,7 +49,7 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { int j=1; for( int i=0; i N ctrl(N n) { return setDef(0,n); } public Node mem(Node n) { return setDef(1,n); } - public void push(Kind kind) { + public void push( Kind kind ) { assert _lexSize._len==_kinds._len; _lexSize.push(_vars.size()); _kinds .push(kind); @@ -94,11 +95,11 @@ public void push(Kind kind) { // Pop a lexical scope public void pop() { assert _lexSize._len==_kinds._len; - promote(); + promote(); // Promote forward references to the next outer scope int n = _lexSize.pop(); _kinds.pop(); - popUntil(n); - _vars.setLen(n); + popUntil(n); // Pop off inputs going out of scope + _vars.setLen(n); // Pop off variables going out of scope } @@ -107,7 +108,7 @@ public void pop() { public void promote() { int n = _lexSize.last(); for( int i=n; i=0; i-- ) - if( _vars.at(i)._name.equals(name) ) + if( var(i)._name.equals(name) ) return i; return -1; } @@ -148,7 +149,7 @@ public boolean define(String name, Type declaredType, boolean xfinal, Node init) assert _lexSize.isEmpty() || name.charAt(0)!='$' ; // Later scopes do not define memory if( _lexSize._len > 0 ) for( int i=_vars.size()-1; i>=_lexSize.last(); i-- ) { - Var n = _vars.at(i); + Var n = var(i); if( n._name.equals(name) ) { if( !n.isFRef() ) return false; // Double define FRefNode fref = (FRefNode)in(n._idx); // Get forward ref @@ -181,11 +182,11 @@ public boolean define(String name, Type declaredType, boolean xfinal, Node init) public Var lookup( String name ) { int idx = find(name); // -1 is missed in all scopes, not found - return idx == -1 ? null : update(_vars.at(idx),null); + return idx == -1 ? null : update(var(idx),null); } /** - * If the name is present in any scope, then redefine else null + * Redefine an existing name * * @param name Name being redefined * @param n The node to bind to the name @@ -193,7 +194,7 @@ public Var lookup( String name ) { public void update( String name, Node n ) { int idx = find(name); assert idx>=0; - update(_vars.at(idx),n); + update(var(idx),n); } public Var update( Var v, Node st ) { @@ -207,7 +208,7 @@ public Var update( Var v, Node st ) { // Set real Phi in the loop head // The phi takes its one input (no backedge yet) from a recursive // lookup, which might have insert a Phi in every loop nest. - : loop.setDef(v._idx,new PhiNode(v._name, v.lazyGLB(), loop.ctrl(), loop.in(loop.update(v,null)._idx),null).peephole()); + : loop.setDef(v._idx,new PhiNode(v._name, v.type(), loop.ctrl(), loop.in(loop.update(v,null)._idx),null).peephole()); setDef(v._idx,old); } assert !v._final || st==null; @@ -260,7 +261,7 @@ public ScopeNode dup(boolean loop) { for( int i=2; i>shf) != x._min || + ((x._max<>shf) != x._max ) ) + return TypeInteger.make(x._min< x << (y ? 4 : 5) - return null; + return super.idealize(); } @Override Node copy(Node lhs, Node rhs) { return new ShlNode(lhs,rhs); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java index f50ac65..2fb8aad 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/ShrNode.java @@ -3,27 +3,16 @@ import com.compilerprogramming.ezlang.compiler.sontypes.Type; import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; -public class ShrNode extends LogicalNode { +public class ShrNode extends ArithNode { public ShrNode(Node lhs, Node rhs) { super(lhs, rhs); } @Override public String label() { return "Shr"; } @Override public String op() { return ">>>"; } - @Override public String glabel() { return ">>>"; } - @Override - public Type compute() { - Type t1 = in(1)._type, t2 = in(2)._type; - if( t1.isHigh() || t2.isHigh() ) - return TypeInteger.TOP; - if (t1 instanceof TypeInteger i0 && - t2 instanceof TypeInteger i1 ) { - if( i0 == TypeInteger.ZERO ) - return TypeInteger.ZERO; - if( i0.isConstant() && i1.isConstant() ) - return TypeInteger.constant(i0.value()>>>i1.value()); - } - return TypeInteger.BOT; + @Override long doOp( long x, long y ) { return x >>> y; } + @Override TypeInteger doOp( TypeInteger x, TypeInteger y ) { + return x == TypeInteger.ZERO ? x : TypeInteger.BOT; } @Override @@ -38,7 +27,7 @@ public Node idealize() { // TODO: x >>> 3 >>> (y ? 1 : 2) ==> x >>> (y ? 4 : 5) - return null; + return super.idealize(); } @Override Node copy(Node lhs, Node rhs) { return new ShrNode(lhs,rhs); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java index ba18e35..b03a3a4 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StartNode.java @@ -44,7 +44,7 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { @Override public TypeTuple compute() { - return TypeTuple.make(Type.CONTROL, TypeMem.TOP,_arg); + return TypeTuple.make(Type.CONTROL,TypeMem.TOP,_arg); } @Override public Node idealize() { return null; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java index 5f19131..9dcc8e3 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/StopNode.java @@ -22,11 +22,15 @@ public String label() { @Override public StringBuilder _print1(StringBuilder sb, BitSet visited) { // For the sake of many old tests, and single value prints as "return val" - if( ret()!=null ) return ret()._print0(sb,visited); + ReturnNode ret1 = ret(); + if( ret1!=null ) return ret1._print0(sb,visited); sb.append("Stop[ "); for( Node ret : _inputs ) - if( ret!=null ) - ret._print0(sb, visited).append(" "); + if( ret!=null ) { + String name = ((ReturnNode)ret).fun()._name; + if( name== null || !name.startsWith("sys.") ) + ret._print0(sb, visited).append(" "); + } return sb.append("]"); } @@ -36,7 +40,13 @@ public StringBuilder _print1(StringBuilder sb, BitSet visited) { // If a single Return, return it. // Otherwise, null because ambiguous. public ReturnNode ret() { - return nIns()==1 && in(0) instanceof ReturnNode ret ? ret : null; + Node ret1 = this; + for( Node ret : _inputs ) { + String name = ((ReturnNode)ret).fun()._name; + if( name==null || !name.startsWith("sys.") ) + ret1 = ret1==this ? ((ReturnNode)ret) : null; + } + return ret1==this ? null : (ReturnNode)ret1; } @Override @@ -48,7 +58,7 @@ public Type compute() { public Node idealize() { int len = nIns(); for( int i=0; i= store_size && not_affected_bits >= store_size) { + setDef(4, shl.in(1)); + return this; + } + } return null; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SubNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SubNode.java index 151de7f..78fb8bc 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SubNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/SubNode.java @@ -4,38 +4,22 @@ import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; import java.util.BitSet; -public class SubNode extends Node { - public SubNode(Node lhs, Node rhs) { super(null, lhs, rhs); } +public class SubNode extends ArithNode { + public SubNode(Node lhs, Node rhs) { super(lhs, rhs); } @Override public String label() { return "Sub"; } + @Override public String op() { return "-"; } - @Override public String glabel() { return "-"; } - - @Override - public StringBuilder _print1(StringBuilder sb, BitSet visited) { - in(1)._print0(sb.append("("), visited); - in(2)._print0(sb.append("-"), visited); - return sb.append(")"); - } - - @Override - public Type compute() { - Type t1 = in(1)._type, t2 = in(2)._type; - if( t1.isHigh() || t2.isHigh() ) - return TypeInteger.TOP; + @Override long doOp( long x, long y ) { return x - y; } + @Override TypeInteger doOp(TypeInteger x, TypeInteger y) { // Sub of same is 0 if( in(1)==in(2) ) return TypeInteger.ZERO; - if( t1 instanceof TypeInteger i1 && - t2 instanceof TypeInteger i2 ) { - if (i1.isConstant() && i2.isConstant()) - return TypeInteger.constant(i1.value()-i2.value()); - // Fold ranges like {2-3} - {0-1} into {1-3}. - if( !AddNode.overflow(i1._min,-i2._max) && - !AddNode.overflow(i1._max,-i2._min) && - i2._min != Long.MIN_VALUE ) - return TypeInteger.make(i1._min-i2._max,i1._max-i2._min); - } + // Fold ranges like {2-3} - {0-1} into {1-3}. + if( !AddNode.overflow(x._min,-y._max) && + !AddNode.overflow(x._max,-y._min) && + y._min != Long.MIN_VALUE ) + return TypeInteger.make(x._min-y._max,x._max-y._min); return TypeInteger.BOT; } @@ -50,7 +34,7 @@ public Node idealize() { if( in(1) instanceof MinusNode minus ) return new MinusNode(new AddNode(minus.in(1),in(2)).peephole()); - return null; + return super.idealize(); } @Override Node copy(Node lhs, Node rhs) { return new SubNode(lhs,rhs); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/XorNode.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/XorNode.java index 3595b56..933ecc5 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/XorNode.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/XorNode.java @@ -3,24 +3,15 @@ import com.compilerprogramming.ezlang.compiler.sontypes.Type; import com.compilerprogramming.ezlang.compiler.sontypes.TypeInteger; -public class XorNode extends LogicalNode { +public class XorNode extends ArithNode { public XorNode(Node lhs, Node rhs) { super(lhs, rhs); } @Override public String label() { return "Xor"; } @Override public String op() { return "^"; } - @Override public String glabel() { return "^"; } - @Override - public Type compute() { - Type t1 = in(1)._type, t2 = in(2)._type; - if( t1.isHigh() || t2.isHigh() ) - return TypeInteger.TOP; - if( t1 instanceof TypeInteger i0 && - t2 instanceof TypeInteger i1 ) { - if( i0.isConstant() && i1.isConstant() ) - return TypeInteger.constant(i0.value()^i1.value()); - } + @Override long doOp( long x, long y ) { return x ^ y; } + @Override TypeInteger doOp( TypeInteger x, TypeInteger y ) { return TypeInteger.BOT; } @@ -46,7 +37,7 @@ public Node idealize() { Node phicon = AddNode.phiCon(this,true); if( phicon!=null ) return phicon; - return null; + return super.idealize(); } @Override Node copy(Node lhs, Node rhs) { return new XorNode(lhs,rhs); } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallARM.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallARM.java index 86b53ef..ca89abd 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallARM.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallARM.java @@ -8,13 +8,13 @@ public class CallARM extends CallNode implements MachNode, RIPRelSize { final TypeFunPtr _tfp; final String _name; - CallARM(CallNode call, TypeFunPtr tfp) { super(call); _inputs.pop(); // Pop constant target assert tfp.isConstant(); _tfp = tfp; - _name = CodeGen.CODE.link(tfp)._name; + FunNode fun = CodeGen.CODE.link(tfp); + _name = fun==null ? ((ExternNode)call.fptr())._extern : fun._name; // Can be null for extern calls } @Override public String op() { return "call"; } @@ -23,9 +23,12 @@ public class CallARM extends CallNode implements MachNode, RIPRelSize { @Override public TypeFunPtr tfp() { return _tfp; } @Override public RegMask regmap(int i) { return arm.callInMask(_tfp,i,fun()._maxArgSlot); } @Override public RegMask outregmap() { return null; } + @Override public int nargs() { return nIns()-2; } // Minus control, memory, fptr @Override public void encoding( Encoding enc ) { - enc.relo(this); + FunNode fun = CodeGen.CODE.link(_tfp); + if( fun==null ) enc.external(this,_name); + else enc.relo(this); // BL enc.add4(arm.b(arm.OP_CALL,0)); // Target patched at link time } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallEndARM.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallEndARM.java deleted file mode 100644 index eb868c1..0000000 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/CallEndARM.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.compilerprogramming.ezlang.compiler.nodes.cpus.arm; - -import com.compilerprogramming.ezlang.compiler.nodes.CallEndNode; -import com.compilerprogramming.ezlang.compiler.nodes.MachNode; - -public class CallEndARM extends CallEndNode implements MachNode { - CallEndARM( CallEndNode cend ) { super(cend); } -} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/FloatARM.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/FloatARM.java index 5d1b14a..dbf422a 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/FloatARM.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/FloatARM.java @@ -19,8 +19,6 @@ public class FloatARM extends ConstantNode implements MachNode, RIPRelSize { @Override public void encoding( Encoding enc ) { enc.largeConstant(this,_con,0,-1/*TODO: ARM-style ELF patching*/); short dst = (short)(enc.reg(this) - arm.D_OFFSET); - double d = ((TypeFloat)_con).value(); - long x = Double.doubleToRawLongBits(d); enc.add4(arm.load_pc(arm.OPF_ARM, 0, dst)); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/IntARM.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/IntARM.java index 4190ce4..6553b48 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/IntARM.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/IntARM.java @@ -20,7 +20,7 @@ public class IntARM extends ConstantNode implements MachNode { @Override public void encoding( Encoding enc ) { short self = enc.reg(this); - long x = _con== Type.NIL ? 0 : ((TypeInteger)_con).value(); + long x = _con==Type.NIL ? 0 : ((TypeInteger)_con).value(); int nb0 = 0; int nb1 = 0; // Count number of 0000 and FFFF blocks diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TFPARM.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TFPARM.java index eda874e..4742edb 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TFPARM.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TFPARM.java @@ -4,6 +4,7 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; +import com.compilerprogramming.ezlang.compiler.nodes.Node; public class TFPARM extends ConstantNode implements MachNode, RIPRelSize { TFPARM( ConstantNode con ) { super(con); } @@ -23,21 +24,22 @@ public class TFPARM extends ConstantNode implements MachNode, RIPRelSize { } @Override public byte encSize(int delta) { - if( -(1L<<12) <= delta && delta < (1L<<12) ) return 4; - throw Utils.TODO(); + return 8; } // Delta is from opcode start @Override public void patch( Encoding enc, int opStart, int opLen, int delta ) { short rpc = enc.reg(this); if(opLen == 8 ) { - // opstart of add - int next = opStart + opLen; + // ARM encoding delta is from PC & 0xFFF + int target = opStart+delta; + int base = opStart & ~0xFFF; + delta = target-base; int adrp_delta = delta >> 12; // patch upper 20 bits via adrp enc.patch4(opStart, arm.adrp(1, adrp_delta & 0b11, 0b10000, adrp_delta >> 2, rpc)); // low 12 bits via add - enc.patch4(next, arm.imm_inst_l(arm.OPI_ADD, delta & 0xfff, rpc)); + enc.patch4(opStart+4, arm.imm_inst_l(arm.OPI_ADD, delta & 0xfff, rpc)); } else { // should not happen as one instruction is 4 byte, and TFP arm encodes 2. throw Utils.TODO(); @@ -48,4 +50,5 @@ public class TFPARM extends ConstantNode implements MachNode, RIPRelSize { String reg = code.reg(this); _con.print(sb.p(reg).p(" #")); } + @Override public boolean eq(Node n) { return this==n; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TMPARM.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TMPARM.java new file mode 100644 index 0000000..0356677 --- /dev/null +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/TMPARM.java @@ -0,0 +1,53 @@ +package com.compilerprogramming.ezlang.compiler.nodes.cpus.arm; + +import com.compilerprogramming.ezlang.compiler.*; +import com.compilerprogramming.ezlang.compiler.codegen.*; +import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; +import com.compilerprogramming.ezlang.compiler.nodes.MachNode; +import com.compilerprogramming.ezlang.compiler.nodes.Node; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeMemPtr; + +public class TMPARM extends ConstantNode implements MachNode, RIPRelSize { + TMPARM( ConstantNode con ) { super(con); } + @Override public String op() { return "ldp"; } + @Override public RegMask regmap(int i) { return null; } + @Override public RegMask outregmap() { return arm.WMASK; } + @Override public boolean isClone() { return true; } + @Override public TMPARM copy() { return new TMPARM(this); } + @Override public void encoding( Encoding enc ) { + enc.largeConstant(this,((TypeMemPtr)_con)._obj,0,-1); + short dst = enc.reg(this); + // adrp x0, 0 + enc.add4(arm.adrp(1,0, arm.OP_ADRP, 0,dst)); + // add x0, x0, 0 + arm.imm_inst(enc,arm.OPI_ADD,0, dst); + } + + @Override public byte encSize(int delta) { + return 8; + } + + // Delta is from opcode start + @Override public void patch( Encoding enc, int opStart, int opLen, int delta ) { + short dst = enc.reg(this); + if(opLen == 8 ) { + // ARM encoding delta is from PC & 0xFFF + int target = opStart+delta; + int base = opStart & ~0xFFF; + delta = target-base; + int adrp_delta = delta >> 12; + // patch upper 20 bits via adrp + enc.patch4(opStart, arm.adrp(1, adrp_delta & 0b11, 0b10000, adrp_delta >> 2, dst)); + // low 12 bits via add + enc.patch4(opStart+4, arm.imm_inst_l(arm.OPI_ADD, delta & 0xfff, dst)); + } else { + throw Utils.TODO(); + } + } + + @Override public void asm(CodeGen code, SB sb) { + String reg = code.reg(this); + _con.print(sb.p(reg).p(" #")); + } + @Override public boolean eq(Node n) { return this==n; } +} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/arm.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/arm.java index e56b593..cf541f7 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/arm.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/arm/arm.java @@ -568,12 +568,14 @@ static RegMask callInMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { if( idx==0 ) return CodeGen.CODE._rpcMask; if( idx==1 ) return null; // Count floats in signature up to index + if( idx-2 >= tfp.nargs() ) return null; // Anti-dependence + // Count floats in signature up to index int fcnt=0; for( int i=2; i new AddFARM(addf); - case AddNode add -> add(add); - case AndNode and -> and(and); - case BoolNode bool -> cmp(bool); - case CallNode call -> call(call); - case CastNode cast -> new CastARM(cast); - case CallEndNode cend -> new CallEndARM(cend); - case CProjNode c -> new CProjNode(c); - case ConstantNode con -> con(con); - case DivFNode divf -> new DivFARM(divf); - case DivNode div -> new DivARM(div); - case FunNode fun -> new FunARM(fun); - case IfNode iff -> jmp(iff); - case LoadNode ld -> ld(ld); - case MemMergeNode mem -> new MemMergeNode(mem); - case MinusNode neg -> new NegARM(neg); - case MulFNode mulf -> new MulFARM(mulf); - case MulNode mul -> new MulARM(mul); - case NewNode nnn -> new NewARM(nnn); - case NotNode not -> new NotARM(not); - case OrNode or -> or(or); - case ParmNode parm -> new ParmARM(parm); - case PhiNode phi -> new PhiNode(phi); - case ProjNode prj -> new ProjARM(prj); - case ReadOnlyNode read -> new ReadOnlyNode(read); - case ReturnNode ret -> new RetARM(ret,ret.fun()); - case SarNode sar -> asr(sar); - case ShlNode shl -> lsl(shl); - case ShrNode shr -> lsr(shr); + case AddFNode addf -> new AddFARM(addf); + case AddNode add -> add(add); + case AndNode and -> and(and); + case BoolNode bool -> cmp(bool); + case CallNode call -> call(call); + case CastNode cast -> new CastMach(cast); + case CallEndNode cend-> new CallEndMach(cend); + case CProjNode c -> new CProjNode(c); + case ConstantNode con-> con(con); + case DivFNode divf -> new DivFARM(divf); + case DivNode div -> new DivARM(div); + case FunNode fun -> new FunARM(fun); + case IfNode iff -> jmp(iff); + case LoadNode ld -> ld(ld); + case MemMergeNode mem-> new MemMergeNode(mem); + case MinusNode neg -> new NegARM(neg); + case MulFNode mulf -> new MulFARM(mulf); + case MulNode mul -> new MulARM(mul); + case NewNode nnn -> new NewARM(nnn); + case NotNode not -> new NotARM(not); + case OrNode or -> or(or); + case ParmNode parm -> new ParmARM(parm); + case PhiNode phi -> new PhiNode(phi); + case ProjNode prj -> new ProjARM(prj); + case ReadOnlyNode read -> new ReadOnlyMach(read); + case ReturnNode ret -> new RetARM(ret,ret.fun()); + case SarNode sar -> asr(sar); + case ShlNode shl -> lsl(shl); + case ShrNode shr -> lsr(shr); case StartNode start -> new StartNode(start); - case StopNode stop -> new StopNode(stop); - case StoreNode st -> st(st); - case SubFNode subf -> new SubFARM(subf); - case SubNode sub -> sub(sub); - case ToFloatNode tfn-> new I2F8ARM(tfn); - case XorNode xor -> xor(xor); - - case LoopNode loop -> new LoopNode(loop); + case StopNode stop -> new StopNode(stop); + case StoreNode st -> st(st); + case SubFNode subf -> new SubFARM(subf); + case SubNode sub -> sub(sub); + case ToFloatNode tfn -> new I2F8ARM(tfn); + case XorNode xor -> xor(xor); + + case LoopNode loop -> new LoopNode(loop); case RegionNode region-> new RegionNode(region); default -> throw Utils.TODO(); }; @@ -729,13 +731,13 @@ private Node sub(SubNode sub) { private Node con( ConstantNode con ) { if( !con._con.isConstant() ) return new ConstantNode( con ); // Default unknown caller inputs return switch( con._con ) { - case TypeInteger ti -> new IntARM(con); - case TypeFloat tf -> new FloatARM(con); - case TypeFunPtr tfp -> new TFPARM(con); - case TypeMemPtr tmp -> new ConstantNode(con); - case TypeNil tn -> throw Utils.TODO(); - // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case Type t -> t== Type.NIL ? new IntARM(con) : new ConstantNode(con); + case TypeInteger ti -> new IntARM(con); + case TypeFloat tf -> new FloatARM(con); + case TypeFunPtr tfp -> new TFPARM(con); + case TypeMemPtr tmp -> new TMPARM(con); + case TypeNil tn -> throw Utils.TODO(); + // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. + case Type t -> t==Type.NIL ? new IntARM(con) : new ConstantNode(con); }; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallEndRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallEndRISC.java deleted file mode 100644 index 12f2b81..0000000 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallEndRISC.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.compilerprogramming.ezlang.compiler.nodes.cpus.riscv; - -import com.compilerprogramming.ezlang.compiler.nodes.CallEndNode; -import com.compilerprogramming.ezlang.compiler.nodes.MachNode; - -public class CallEndRISC extends CallEndNode implements MachNode { - CallEndRISC( CallEndNode cend ) { super(cend); } -} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java index a552e4c..79ab6ca 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/CallRISC.java @@ -13,23 +13,26 @@ public class CallRISC extends CallNode implements MachNode, RIPRelSize { assert tfp.isConstant(); _inputs.pop(); // Pop constant target _tfp = tfp; - _name = CodeGen.CODE.link(tfp)._name; + FunNode fun = CodeGen.CODE.link(tfp); + _name = fun==null ? ((ExternNode)call.fptr())._extern : fun._name; // Can be null for extern calls } @Override public String op() { return "call"; } @Override public String label() { return op(); } - @Override public RegMask regmap(int i) { - return riscv.callInMask(_tfp,i,fun()._maxArgSlot); - } - @Override public RegMask outregmap() { return null; } @Override public String name() { return _name; } @Override public TypeFunPtr tfp() { return _tfp; } + @Override public RegMask regmap(int i) { return riscv.callInMask(_tfp,i,fun()._maxArgSlot); } + @Override public RegMask outregmap() { return riscv.RPC_MASK; } + @Override public int nargs() { return nIns()-2; } // Minus control, memory, fptr @Override public void encoding( Encoding enc ) { // Short form +/-4K: beq r0,r0,imm12 // Long form: auipc rX,imm20/32; jal r0,[rX+imm12/32] - enc.relo(this); - enc.add4(riscv.j_type(riscv.OP_JAL, riscv.RPC, 0)); + FunNode fun = CodeGen.CODE.link(_tfp); + if( fun==null ) enc.external(this,_name); + else enc.relo(this); + short rpc = enc.reg(this); + enc.add4(riscv.j_type(riscv.OP_JAL, rpc, 0)); } // Delta is from opcode start @@ -41,8 +44,9 @@ public class CallRISC extends CallNode implements MachNode, RIPRelSize { // Delta is from opcode start @Override public void patch( Encoding enc, int opStart, int opLen, int delta ) { + short rpc = enc.reg(this); if( opLen==4 ) { - enc.patch4(opStart,riscv.j_type(riscv.OP_JAL, riscv.RPC, delta)); + enc.patch4(opStart,riscv.j_type(riscv.OP_JAL, rpc, delta)); } else { throw Utils.TODO(); } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java index 31d0f1b..4e8ab87 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/FltRISC.java @@ -14,7 +14,7 @@ public class FltRISC extends ConstantNode implements MachNode, RIPRelSize { @Override public FltRISC copy() { return new FltRISC(this); } @Override public void encoding( Encoding enc ) { - enc.largeConstant(this,_con, 0, -1/*TODO: RISC5 style patching*/); + enc.largeConstant(this,_con, 0, -1); short dst = (short)(enc.reg(this) - riscv.F_OFFSET); short tmp = (short)riscv.T6; // AUIPC dst,#hi20_constant_pool @@ -25,7 +25,6 @@ public class FltRISC extends ConstantNode implements MachNode, RIPRelSize { @Override public RegMask killmap() { return new RegMask(riscv.T6); } // Delta is from opcode start. - // TODO: always size 8? @Override public byte encSize(int delta) { return 8; } // Delta is from opcode start diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java index 814b984..59f638c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/ImmRISC.java @@ -40,8 +40,7 @@ abstract public class ImmRISC extends MachConcreteNode implements MachNode { @Override public void encoding( Encoding enc ) { short dst = enc.reg(this ); short src = enc.reg(in(1)); - int body = riscv.i_type(opcode(), dst, func3(), src, _imm12 & 0xFFF); - enc.add4(body); + enc.add4(riscv.i_type(opcode(), dst, func3(), src, _imm12 & 0xFFF)); } // General form: "addi rd = rs1 + imm" diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/Int8RISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/Int8RISC.java new file mode 100644 index 0000000..9b56665 --- /dev/null +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/Int8RISC.java @@ -0,0 +1,47 @@ +package com.compilerprogramming.ezlang.compiler.nodes.cpus.riscv; + +import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; + +import com.compilerprogramming.ezlang.compiler.*; +import com.compilerprogramming.ezlang.compiler.codegen.*; +import com.compilerprogramming.ezlang.compiler.nodes.MachNode; + +// Special instruction for loading 8 byte constants from the constant pool. + +public class Int8RISC extends ConstantNode implements MachNode, RIPRelSize { + public Int8RISC(ConstantNode con) { super(con); } + + @Override public String op() { return "ld8"; } + @Override public RegMask regmap(int i) { return null; } + @Override public RegMask outregmap() { return riscv.WMASK; } + @Override public boolean isClone() { return true; } + @Override public Int8RISC copy() { return new Int8RISC(this); } + + @Override public byte encSize(int delta) { return 8; } + + @Override public void encoding( Encoding enc ) { + short dst = enc.reg(this); + short tmp = (short)riscv.T6; + enc.largeConstant(this,_con, 0, -1); + // AUIPC dst,#hi20_constant_pool + enc.add4(riscv.u_type(riscv.OP_AUIPC, tmp, 0)); + // Load dst,[dst+#low12_constant_pool] + enc.add4(riscv.i_type(riscv.OP_LOAD, dst, 0b11, tmp, 0)); + } + @Override public RegMask killmap() { return new RegMask(riscv.T6); } + + // Delta is from opcode start + @Override public void patch( Encoding enc, int opStart, int opLen, int delta ) { + short dst = enc.reg(this); + short tmp = (short)riscv.T6; + // AUIPC dst,#hi20_constant_pool + enc.patch4(opStart , riscv.u_type(riscv.OP_AUIPC, tmp, delta>>12)); + // Load dst,[dst+#low12_constant_pool] + enc.patch4(opStart+4, riscv.i_type(riscv.OP_LOAD, dst, 0b11, tmp, delta & 0xFFF)); + } + + @Override public void asm(CodeGen code, SB sb) { + _con.print(sb.p(code.reg(this)).p(" = #")); + } + +} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java index 905800f..5b42b74 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/IntRISC.java @@ -18,7 +18,7 @@ public class IntRISC extends ConstantNode implements MachNode { @Override public IntRISC copy() { return new IntRISC(this); } @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); - int val = _con== Type.NIL ? 0 : (int)(((TypeInteger)_con).value() & 0xFFF); + int val = _con==Type.NIL ? 0 : (int)(((TypeInteger)_con).value() & 0xFFF); // Explicit truncation of larger immediates; this will sign-extend on // load and this is handled during instruction selection. enc.add4(riscv.i_type(riscv.OP_IMM, dst, 0, riscv.ZERO, val)); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java index a804ba8..435482e 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/LUI.java @@ -10,7 +10,7 @@ public class LUI extends ConstantNode implements MachNode { public LUI( int imm20 ) { super(TypeInteger.constant(imm20)); - assert riscv.imm20Exact((TypeInteger)_con); + assert (imm20 & 0xFFF)==0; } @Override public RegMask regmap(int i) { return null; } @Override public RegMask outregmap() { return riscv.WMASK; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java index f8d1ddf..a74ef63 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/MemOpRISC.java @@ -35,8 +35,9 @@ int func3() { if(this instanceof StoreRISC) { if( _declaredType == TypeInteger. I8 || _declaredType == TypeInteger.U8 || _declaredType == TypeInteger.BOOL) func3=0; // SB if( _declaredType == TypeInteger.I16 || _declaredType == TypeInteger.U16 ) func3=1; // SH - if( _declaredType == TypeInteger.I32 || _declaredType == TypeInteger.U32 || _declaredType instanceof TypeMemPtr) func3=2; // SW - if( _declaredType == TypeInteger.BOT ) func3=3; // SD + if( _declaredType == TypeInteger.I32 || _declaredType == TypeInteger.U32 ) func3=2; // SW + if( _declaredType instanceof TypeMemPtr) func3=3; // SD + if( _declaredType == TypeInteger.BOT ) func3=3; // SD if( func3 == -1 ) throw Utils.TODO(); return func3; } @@ -50,9 +51,10 @@ int func3() { if( _declaredType == TypeInteger.U32 ) func3=6; // LWU // float - if(_declaredType == TypeFloat.F32) func3 = 2; // fLW fSW - if(_declaredType == TypeFloat.F64) func3 = 3; // fLD fSD - if( _declaredType instanceof TypeMemPtr) func3=3; // 8 byte pointers + if( _declaredType == TypeFloat.F32) func3 = 2; // fLW fSW + if( _declaredType == TypeFloat.F64) func3 = 3; // fLD fSD + + if( _declaredType instanceof TypeMemPtr) func3 = 3; // 8 byte pointers (pick ld) if( func3 == -1 ) throw Utils.TODO(); return func3; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/SraIRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/SraIRISC.java index 2ccf60a..b8842ff 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/SraIRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/SraIRISC.java @@ -4,7 +4,9 @@ import com.compilerprogramming.ezlang.compiler.nodes.Node; public class SraIRISC extends ImmRISC { - public SraIRISC( Node and, int imm) { super(and,imm); } + public SraIRISC( Node and, int imm) { + super(and,imm | (0x20 << 5)); + } @Override int opcode() { return riscv.OP_IMM; } @Override int func3() { return 5;} @Override public String glabel() { return ">>"; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java index fcdd904..599fbed 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TFPRISC.java @@ -4,6 +4,7 @@ import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; +import com.compilerprogramming.ezlang.compiler.nodes.Node; import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; public class TFPRISC extends ConstantNode implements MachNode, RIPRelSize { @@ -19,11 +20,9 @@ public class TFPRISC extends ConstantNode implements MachNode, RIPRelSize { short dst = enc.reg(this); TypeFunPtr tfp = (TypeFunPtr)_con; // auipc t0,0 - int auipc = riscv.u_type(riscv.OP_AUIPC, dst, 0); + enc.add4(riscv.u_type(riscv.OP_AUIPC, dst, 0)); // addi t1,t0 + #0 - int addi = riscv.i_type(riscv.OP_IMM, dst, 0, dst, 0); - enc.add4(auipc); - enc.add4(addi); + enc.add4(riscv.i_type(riscv.OP_IMM, dst, 0, dst, 0)); } @Override public byte encSize(int delta) { @@ -38,7 +37,7 @@ public class TFPRISC extends ConstantNode implements MachNode, RIPRelSize { // AUIPC (upper 20 bits) // opstart of add int next = opStart + opLen; - enc.patch4(opStart,riscv.u_type(riscv.OP_AUIPC, rpc, delta)); + enc.patch4(opStart,riscv.u_type(riscv.OP_AUIPC, rpc, delta>>12)); // addi(low 12 bits) enc.patch4(next,riscv.i_type(riscv.OP_IMM, rpc, 0, rpc, delta & 0xFFF)); // addi @@ -51,5 +50,5 @@ public class TFPRISC extends ConstantNode implements MachNode, RIPRelSize { @Override public void asm(CodeGen code, SB sb) { _con.print(sb.p(code.reg(this)).p(" #")); } - + @Override public boolean eq(Node n) { return this==n; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TMPRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TMPRISC.java new file mode 100644 index 0000000..6079e6c --- /dev/null +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/TMPRISC.java @@ -0,0 +1,41 @@ +package com.compilerprogramming.ezlang.compiler.nodes.cpus.riscv; + +import com.compilerprogramming.ezlang.compiler.*; +import com.compilerprogramming.ezlang.compiler.codegen.*; +import com.compilerprogramming.ezlang.compiler.nodes.ConstantNode; +import com.compilerprogramming.ezlang.compiler.nodes.MachNode; +import com.compilerprogramming.ezlang.compiler.nodes.Node; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeMemPtr; + +public class TMPRISC extends ConstantNode implements MachNode, RIPRelSize { + TMPRISC(ConstantNode con) { super(con); } + @Override public String op() { return "ldp"; } + @Override public RegMask regmap(int i) { return null; } + @Override public RegMask outregmap() { return riscv.WMASK; } + @Override public boolean isClone() { return true; } + @Override public TMPRISC copy() { return new TMPRISC(this); } + @Override public void encoding( Encoding enc ) { + enc.largeConstant(this,((TypeMemPtr)_con)._obj,0,-1); + short dst = enc.reg(this); + // AUIPC dst,#hi20_constant_pool + enc.add4(riscv.u_type(riscv.OP_AUIPC, dst, 0)); + // addi dst,[dst+#low12_constant_pool] + enc.add4(riscv.i_type(riscv.OP_IMM, dst, 0, dst, 0)); + } + + @Override public byte encSize(int delta) { return 8; } + + // Delta is from opcode start + @Override public void patch( Encoding enc, int opStart, int opLen, int delta ) { + short dst = enc.reg(this); + // AUIPC dst,#hi20_constant_pool + enc.patch4(opStart , riscv.u_type(riscv.OP_AUIPC, dst, delta>>12)); + // Load dst,[dst+#low12_constant_pool] + enc.patch4(opStart+4, riscv.i_type(riscv.OP_IMM, dst, 0, dst, delta & 0xFFF)); + } + + @Override public void asm(CodeGen code, SB sb) { + _con.print(sb.p(code.reg(this)).p(" #")); + } + @Override public boolean eq(Node n) { return this==n; } +} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/XorIRISC.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/XorIRISC.java index b7c03b0..9672291 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/XorIRISC.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/XorIRISC.java @@ -5,6 +5,7 @@ public class XorIRISC extends ImmRISC { public XorIRISC( Node and, int imm) { super(and,imm); } + public XorIRISC( Node and, int imm, boolean pop) { super(and,imm,pop); } @Override int opcode() { return riscv.OP_IMM; } @Override int func3() { return 4; } @Override public String glabel() { return "^"; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java index 854112f..6018b9d 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/riscv/riscv.java @@ -188,7 +188,6 @@ public static int i_type(int opcode, int rd, int func3, int rs1, int imm12) { return (imm12 << 20) | (rs1 << 15) | (func3 << 12) | (rd << 7) | opcode; } - // S-type instructions(store) public static int s_type(int opcode, int func3, int rs1, int rs2, int imm12) { assert 0 <= rs1 && rs1 < 32; @@ -253,17 +252,18 @@ static public int fsetop(String op) { }; } - @Override public RegMask callArgMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } - static RegMask callInMask(TypeFunPtr tfp, int idx, int maxArgSlot ) { + @Override public RegMask callArgMask( TypeFunPtr tfp, int idx, int maxArgSlot ) { return callInMask(tfp,idx,maxArgSlot); } + static RegMask callInMask( TypeFunPtr tfp, int idx, int maxArgSlot ) { if( idx==0 ) return RPC_MASK; if( idx==1 ) return null; + if( idx-2 >= tfp.nargs() ) return null; // Anti-dependence // Count floats in signature up to index int fcnt=0; for( int i=2; i>>44)<<12) == ti.value(); + return ti.isConstant() && (((ti.value()<<32)>>44)<<12) == ti.value(); } @Override public Node instSelect( Node n ) { @@ -328,8 +328,8 @@ public static boolean imm20Exact(TypeInteger ti) { case AndNode and -> and(and); case BoolNode bool -> cmp(bool); case CallNode call -> call(call); - case CastNode cast -> new CastRISC(cast); - case CallEndNode cend -> new CallEndRISC(cend); + case CastNode cast -> new CastMach(cast); + case CallEndNode cend -> new CallEndMach(cend); case CProjNode c -> new CProjNode(c); case ConstantNode con -> con(con); case DivFNode divf -> new DivFRISC(divf); @@ -347,7 +347,7 @@ public static boolean imm20Exact(TypeInteger ti) { case ParmNode parm -> new ParmRISC(parm); case PhiNode phi -> new PhiNode(phi); case ProjNode prj -> prj(prj); - case ReadOnlyNode read-> new ReadOnlyNode(read); + case ReadOnlyNode read-> new ReadOnlyMach(read); case ReturnNode ret -> new RetRISC(ret, ret.fun()); case SarNode sar -> sra(sar); case ShlNode shl -> sll(shl); @@ -420,7 +420,7 @@ private Node cmp(BoolNode bool) { ? new SetIRISC(bool, (int)ti.value(),false) : new SetRISC(bool, false); // x <= y - flip and negate; !(y < x); `slt tmp=y,x; xori dst=tmp,#1` - case "<=" -> new XorIRISC(new SetRISC(bool.swap12(), false),1); + case "<=" -> new XorIRISC(new SetRISC(bool.swap12(), false),1,false); // x == y - sub and vs0 == `sub tmp=x-y; sltu dst=tmp,#1` case "==" -> new SetIRISC(new SubRISC(bool),1,true); default -> throw Utils.TODO(); @@ -443,15 +443,15 @@ private Node con( ConstantNode con ) { } // Need more complex sequence for larger constants... or a load // from a constant pool, which does not need an extra register - throw Utils.TODO(); + yield new Int8RISC(con); } // Load from constant pool - case TypeFloat tf -> new FltRISC(con); - case TypeFunPtr tfp -> new TFPRISC(con); - case TypeMemPtr tmp -> throw Utils.TODO(); - case TypeNil tn -> throw Utils.TODO(); + case TypeFloat tf -> new FltRISC(con); + case TypeFunPtr tfp -> new TFPRISC(con); + case TypeMemPtr tmp -> new TMPRISC(con); + case TypeNil tn -> throw Utils.TODO(); // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case Type t -> t== Type.NIL ? new IntRISC(con) : new ConstantNode(con); + case Type t -> t==Type.NIL ? new IntRISC(con) : new ConstantNode(con); }; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/AddIX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/AddIX86.java index 7659e35..b3f3f9f 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/AddIX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/AddIX86.java @@ -1,5 +1,6 @@ package com.compilerprogramming.ezlang.compiler.nodes.cpus.x86_64_v2; +import com.compilerprogramming.ezlang.compiler.codegen.Encoding; import com.compilerprogramming.ezlang.compiler.nodes.Node; public class AddIX86 extends ImmX86 { @@ -10,4 +11,12 @@ public class AddIX86 extends ImmX86 { @Override public String glabel() { return "+"; } @Override int opcode() { return 0x81; } @Override int mod() { return 0; } + @Override public final void encoding( Encoding enc ) { + if( _imm== 1 || _imm== -1 ) { + short dst = enc.reg(this); // Also src1 + enc.add1( x86_64_v2.rex(0,dst,0)); + enc.add1(0xFF); + enc.add1( x86_64_v2.modrm(x86_64_v2.MOD.DIRECT, _imm==1 ? 0 : 1, dst) ); + } else super.encoding(enc); + } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallEndX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallEndX86.java deleted file mode 100644 index 1dfff37..0000000 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallEndX86.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.compilerprogramming.ezlang.compiler.nodes.cpus.x86_64_v2; - -import com.compilerprogramming.ezlang.compiler.nodes.CallEndNode; -import com.compilerprogramming.ezlang.compiler.nodes.MachNode; - -public class CallEndX86 extends CallEndNode implements MachNode { - CallEndX86( CallEndNode cend ) { super(cend); } -} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java index ead9f8b..b43ea38 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CallX86.java @@ -3,6 +3,8 @@ import com.compilerprogramming.ezlang.compiler.SB; import com.compilerprogramming.ezlang.compiler.codegen.*; import com.compilerprogramming.ezlang.compiler.nodes.CallNode; +import com.compilerprogramming.ezlang.compiler.nodes.ExternNode; +import com.compilerprogramming.ezlang.compiler.nodes.FunNode; import com.compilerprogramming.ezlang.compiler.nodes.MachNode; import com.compilerprogramming.ezlang.compiler.sontypes.TypeFunPtr; @@ -14,7 +16,8 @@ public class CallX86 extends CallNode implements MachNode, RIPRelSize { _inputs.pop(); // Pop constant target assert tfp.isConstant(); _tfp = tfp; - _name = CodeGen.CODE.link(tfp)._name; + FunNode fun = CodeGen.CODE.link(tfp); + _name = fun==null ? ((ExternNode)call.fptr())._extern : fun._name; // Can be null for extern calls } @Override public String op() { return "call"; } @Override public String label() { return op(); } @@ -25,7 +28,10 @@ public class CallX86 extends CallNode implements MachNode, RIPRelSize { @Override public int nargs() { return nIns()-2; } // Minus control, memory, fptr @Override public void encoding( Encoding enc ) { - enc.relo(this).add1(0xe8).add4(0); + FunNode fun = CodeGen.CODE.link(_tfp); + if( fun==null ) enc.external(this,_name); + else enc.relo(this); + enc.add1(0xe8).add4(0); } // Delta is from opcode start, but X86 measures from the end of the 5-byte encoding diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CastX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CastX86.java deleted file mode 100644 index f494e79..0000000 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CastX86.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.compilerprogramming.ezlang.compiler.nodes.cpus.x86_64_v2; - -import com.compilerprogramming.ezlang.compiler.SB; -import com.compilerprogramming.ezlang.compiler.codegen.*; -import com.compilerprogramming.ezlang.compiler.nodes.CastNode; -import com.compilerprogramming.ezlang.compiler.nodes.MachNode; - -public class CastX86 extends CastNode implements MachNode { - CastX86( CastNode cast ) { super(cast); } - @Override public String op() { return label(); } - @Override public RegMask regmap(int i) { assert i==1; return RegMask.FULL; } - @Override public RegMask outregmap() { return RegMask.FULL; } - @Override public int twoAddress( ) { return 1; } - @Override public void encoding( Encoding enc ) { } - @Override public void asm(CodeGen code, SB sb) { _t.print(sb.p(code.reg(in(1))).p(" isa ")); } -} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java index 35c0efe..308e0f5 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpMemX86.java @@ -11,7 +11,7 @@ public class CmpMemX86 extends MemOpX86 { CmpMemX86( BoolNode bool, LoadNode ld, Node base, Node idx, int off, int scale, int imm, Node val, boolean swap ) { super(bool,ld, base, idx, off, scale, imm, val ); _swap = swap; - assert !_swap || val() == null || bool.op()=="==" || bool.op()=="!="; // Cannot swap with immediate + assert !_swap || val() != null || bool.op()=="==" || bool.op()=="!="; // Cannot swap with immediate } @Override public String op() { return ((val()==null && _imm==0) ? "test" : "cmp") + _sz; } @Override public RegMask outregmap() { return x86_64_v2.FLAGS_MASK; } @@ -48,7 +48,7 @@ public class CmpMemX86 extends MemOpX86 { static void encVal(Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale, boolean _swap) { if(decl instanceof TypeFloat) { src -= (short)x86_64_v2.XMM_OFFSET; - enc.add1(decl== TypeFloat.F32 ? 0xF3 : 0xF2).add1(0x0F).add1(0xC2); + enc.add1(decl==TypeFloat.F32 ? 0xF3 : 0xF2).add1(0x0F).add1(0xC2); } else { int log = decl.log_size(); x86_64_v2.rexF(src, ptr, idx, log == 3, enc); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpX86.java index b238ed2..563cb9d 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/CmpX86.java @@ -9,6 +9,12 @@ public class CmpX86 extends MachConcreteNode implements MachNode { @Override public String op() { return "cmp"; } @Override public RegMask regmap(int i) { return x86_64_v2.RMASK; } @Override public RegMask outregmap() { return x86_64_v2.FLAGS_MASK; } + // This one is marginal: cloning a Cmp[reg,reg] means stretching the + // lifetimes of 2 normal registers in exchange for shortening a flags + // register lifetime. Spilling flags is (probably) relatively expensive + // compared to some normal registers - and those normal registers might not + // be spilling! + @Override public boolean isClone() { return true; } @Override public void encoding( Encoding enc ) { short dst = enc.reg(in(1)); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/DivX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/DivX86.java index 301cb2c..83a5dda 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/DivX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/DivX86.java @@ -8,7 +8,7 @@ public class DivX86 extends MachConcreteNode implements MachNode { DivX86( Node div ) { super(div); } @Override public String op() { return "div"; } @Override public RegMask regmap(int i) { - return (i==1) ? x86_64_v2.RAX_MASK : x86_64_v2.RMASK; + return (i==1) ? x86_64_v2.RAX_MASK : x86_64_v2.DIV_MASK; } @Override public RegMask outregmap() { return x86_64_v2.RAX_MASK; } @Override public RegMask killmap() { return x86_64_v2.RDX_MASK; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java index 7b45ec3..4b33b82 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/ImmX86.java @@ -14,11 +14,12 @@ public abstract class ImmX86 extends MachConcreteNode implements MachNode { @Override public RegMask regmap(int i) { return x86_64_v2.RMASK; } @Override public RegMask outregmap() { return x86_64_v2.WMASK; } @Override public int twoAddress() { return 1; } + @Override public RegMask killmap() { return x86_64_v2.FLAGS_MASK; } abstract int opcode(); abstract int mod(); - @Override public final void encoding( Encoding enc ) { + @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); // Also src1 enc.add1(x86_64_v2.rex(0, dst, 0)); // opcode; 0x81 or 0x83; 0x69 or 0x6B diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java index cc68d2a..243d0e1 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/IntX86.java @@ -24,7 +24,7 @@ public class IntX86 extends ConstantNode implements MachNode { @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); // Short form for zero - if( _con== Type.NIL || _con== TypeInteger.ZERO ) { + if( _con==Type.NIL || _con==TypeInteger.ZERO ) { // XOR dst,dst. Can skip REX is dst is low 8, makes this a 32b // xor, which will also zero the high bits. if( dst >= 8 ) enc.add1(x86_64_v2.rex(dst, dst, 0)); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java index c7f6624..6b308c2 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/LoadX86.java @@ -25,7 +25,7 @@ public class LoadX86 extends MemOpX86 { enc(enc, _declaredType, dst, ptr, idx, _off, _scale); } - static void enc(Encoding enc, Type decl, short dst, short ptr, short idx, int off, int scale ) { + static void enc( Encoding enc, Type decl, short dst, short ptr, short idx, int off, int scale ) { if( decl == TypeFloat.F32) enc.add1(0xF3); if( decl == TypeFloat.F64) enc.add1(0xF2); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemAddX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemAddX86.java index a0edf38..205ad36 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemAddX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MemAddX86.java @@ -13,6 +13,7 @@ public class MemAddX86 extends MemOpX86 { } // Register mask allowed as a result. 0 for no register. @Override public RegMask outregmap() { return null; } + @Override public RegMask killmap() { return x86_64_v2.FLAGS_MASK; } @Override public void encoding( Encoding enc ) { // add something to memory // REX.W + 01 /r | REX.W + 81 /0 id @@ -23,11 +24,11 @@ public class MemAddX86 extends MemOpX86 { enc.add1(x86_64_v2.rex(src, ptr, idx)); // opcode - enc.add1( src == -1 ? 0x81: 0x01); + enc.add1( src == -1 ? (_imm==1 ? 0xFF : 0x81): 0x01); // includes modrm x86_64_v2.indirectAdr(_scale, idx, ptr, _off, src == -1 ? 0 : src, enc); - if( src == -1 ) enc.add4(_imm); + if( src == -1 && _imm!=1 ) enc.add4(_imm); } // General form: "add [base + idx<<2 + 12] += src" @Override public void asm(CodeGen code, SB sb) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MulIX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MulIX86.java index c53599d..8f0a90c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MulIX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/MulIX86.java @@ -1,11 +1,38 @@ package com.compilerprogramming.ezlang.compiler.nodes.cpus.x86_64_v2; +import com.compilerprogramming.ezlang.compiler.SB; +import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; +import com.compilerprogramming.ezlang.compiler.codegen.Encoding; +import com.compilerprogramming.ezlang.compiler.codegen.RegMask; +import com.compilerprogramming.ezlang.compiler.nodes.MachConcreteNode; +import com.compilerprogramming.ezlang.compiler.nodes.MachNode; import com.compilerprogramming.ezlang.compiler.nodes.Node; -public class MulIX86 extends ImmX86 { - MulIX86( Node add, int imm ) { super(add,imm); } +public class MulIX86 extends MachConcreteNode implements MachNode { + final int _imm; + MulIX86( Node add, int imm ) { + super(add); + _inputs.pop(); // Pop the constant input + _imm = imm; // Record the constant + } @Override public String op() { return "muli"; } @Override public String glabel() { return "*"; } - @Override int opcode() { return 0x69; } - @Override int mod() { return 0; } + @Override public RegMask regmap(int i) { return x86_64_v2.RMASK; } + @Override public RegMask outregmap() { return x86_64_v2.WMASK; } + // Rare 3-address form + @Override public final void encoding( Encoding enc ) { + short dst = enc.reg(this ); // Also src1 + short src = enc.reg(in(1)); + enc.add1(x86_64_v2.rex(src, dst, 0)); + // opcode; 0x69 or 0x6B + enc.add1( 0x69 + (x86_64_v2.imm8(_imm) ? 2 : 0) ); + enc.add1( x86_64_v2.modrm(x86_64_v2.MOD.DIRECT, dst, src) ); + // immediate(4 bytes) 32 bits or (1 byte)8 bits + if( x86_64_v2.imm8(_imm) ) enc.add1(_imm); + else enc.add4(_imm); + } + // General form: "addi dst += #imm" + @Override public void asm(CodeGen code, SB sb) { + sb.p(code.reg(this)).p(" = ").p(code.reg(in(1))).p(" * #").p(_imm); + } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/RegX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/RegX86.java index 8a5e305..6a3838b 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/RegX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/RegX86.java @@ -8,6 +8,7 @@ public abstract class RegX86 extends MachConcreteNode { RegX86( Node add ) { super(add); } @Override public RegMask regmap(int i) { return x86_64_v2.RMASK; } @Override public RegMask outregmap() { return x86_64_v2.WMASK; } + @Override public RegMask killmap() { return x86_64_v2.FLAGS_MASK; } @Override public int twoAddress() { return 1; } abstract int opcode(); @Override public void encoding( Encoding enc ) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SetX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SetX86.java index ae9ff53..62126e8 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SetX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/SetX86.java @@ -27,7 +27,7 @@ public class SetX86 extends MachConcreteNode implements MachNode { short dst = enc.reg(this ); // Optional rex, for dst - if( dst >= 8 ) enc.add1(x86_64_v2.rex(0, dst, 0)); + if( dst >= 4 ) enc.add1(x86_64_v2.rex(0, dst, 0, false)); enc.add1(0x0F); // opcode enc.add1(switch (_bop) { case "==" -> 0x94; // SETE @@ -39,7 +39,7 @@ public class SetX86 extends MachConcreteNode implements MachNode { enc.add1(x86_64_v2.modrm(x86_64_v2.MOD.DIRECT, 0, dst)); // low 8 bites are set, now zero extend for next instruction - if( dst >= 8 ) enc.add1(x86_64_v2.rex(0, dst, 0)); + if( dst >= 4 ) enc.add1(x86_64_v2.rex(dst, dst, 0, false)); enc.add1(0x0F); // opcode enc.add1(0xB6); // opcode enc.add1(x86_64_v2.modrm(x86_64_v2.MOD.DIRECT, dst, dst)); diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java index aa641c0..1fb8cd9 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/StoreX86.java @@ -51,7 +51,7 @@ public class StoreX86 extends MemOpX86 { } // Non-immediate encoding - static void encVal(Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale ) { + static void encVal( Encoding enc, Type decl, short ptr, short idx, short src, int off, int scale ) { int log = decl.log_size(); // Float reg being stored if( src >= x86_64_v2.XMM_OFFSET ) { diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java index 19034fb..46e9e9c 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TFPX86.java @@ -23,7 +23,7 @@ public class TFPX86 extends ConstantNode implements MachNode, RIPRelSize { @Override public void encoding( Encoding enc ) { short dst = enc.reg(this); // Short form for zero - if( _con== Type.NIL ) { + if( _con==Type.NIL ) { // XOR dst,dst. Can skip REX is dst is low 8, makes this a 32b // xor, which will also zero the high bits. if( dst >= 8 ) enc.add1(x86_64_v2.rex(dst, dst, 0)); @@ -61,4 +61,5 @@ public class TFPX86 extends ConstantNode implements MachNode, RIPRelSize { else _con.print(sb.p(reg).p(" = #")); } + @Override public boolean eq(Node n) { return this==n; } } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TMPX86.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TMPX86.java new file mode 100644 index 0000000..746379b --- /dev/null +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/TMPX86.java @@ -0,0 +1,35 @@ +package com.compilerprogramming.ezlang.compiler.nodes.cpus.x86_64_v2; + +import com.compilerprogramming.ezlang.compiler.SB; +import com.compilerprogramming.ezlang.compiler.codegen.*; +import com.compilerprogramming.ezlang.compiler.nodes.*; +import com.compilerprogramming.ezlang.compiler.sontypes.TypeMemPtr; + +public class TMPX86 extends ConstantNode implements MachNode, RIPRelSize{ + TMPX86(ConstantNode con ) { super(con); } + @Override public String op() { return "ldp"; } + @Override public RegMask regmap(int i) { return null; } + @Override public RegMask outregmap() { return x86_64_v2.WMASK; } + @Override public boolean isClone() { return true; } + @Override public Node copy() { return new TMPX86(this); } + + @Override public void encoding( Encoding enc ) { + short dst = enc.reg(this); + x86_64_v2.rexF(dst, 0, 0, true, enc); + enc.add1(0x8D).add1(x86_64_v2.modrm(x86_64_v2.MOD.INDIRECT, dst, 0b101)).add4(0); + enc.largeConstant(this,((TypeMemPtr)_con)._obj,7-4,2/*ELF encoding PC32*/); + } + + // Delta is from opcode start + @Override public byte encSize(int delta) { return (byte)7; } + + // Delta is from opcode start + @Override public void patch( Encoding enc, int opStart, int opLen, int delta ) { + enc.patch4(opStart+opLen-4,delta-opLen); + } + + @Override public void asm(CodeGen code, SB sb) { + _con.print(sb.p(code.reg(this)).p(" = #")); + } + @Override public boolean eq(Node n) { return this==n; } +} diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java index ad134b5..1133035 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/nodes/cpus/x86_64_v2/x86_64_v2.java @@ -43,6 +43,8 @@ public x86_64_v2( CodeGen code ) {} static RegMask XMASK = new RegMask(FP_BITS); static RegMask FLAGS_MASK = new RegMask(FLAGS); static RegMask RPC_MASK = new RegMask(RPC); + // Divide mask: exclude RDX:RAX + static RegMask DIV_MASK = new RegMask(RD_BITS & ~(1L<= tfp.nargs() ) return null; // Anti-dependence return switch( CodeGen.CODE._callingConv ) { case "SystemV" -> callSys5 (tfp,idx,maxArgSlot); case "Win64" -> callWin64(tfp,idx,maxArgSlot); @@ -276,7 +279,7 @@ static RegMask callSys5(TypeFunPtr tfp, int idx, int maxArgSlot ) { // First 8 floats passed in registers: xmm0-xmm7 int icnt=0, fcnt=0; // Count of ints, floats for( int i=2; i and(and); case BoolNode bool -> cmp(bool); case CProjNode c -> new CProjNode(c); - case CallEndNode cend -> new CallEndX86(cend); + case CallEndNode cend -> new CallEndMach(cend); case CallNode call -> call(call); - case CastNode cast -> new CastX86(cast); + case CastNode cast -> new CastMach(cast); case ConstantNode con -> con(con); case DivFNode divf -> new DivFX86(divf); case DivNode div -> new DivX86(div); @@ -362,7 +365,7 @@ public Node instSelect(Node n) { case ParmNode parm -> new ParmX86(parm); case PhiNode phi -> new PhiNode(phi); case ProjNode prj -> prj(prj); - case ReadOnlyNode read-> new ReadOnlyNode(read); + case ReadOnlyNode read-> new ReadOnlyMach(read); case ReturnNode ret -> new RetX86(ret, ret.fun()); case SarNode sar -> sar(sar); case ShlNode shl -> shl(shl); @@ -499,15 +502,16 @@ private Node _cmp(BoolNode bool) { } private Node con( ConstantNode con ) { - if(!con._con.isConstant()) return new ConstantNode(con); // Default unknown caller inputs + if( !con._con.isConstant() ) + return new ConstantNode(con); // Default unknown caller inputs return switch (con._con) { - case TypeInteger ti -> new IntX86(con); - case TypeFloat tf -> new FltX86(con); - case TypeFunPtr tfp -> new TFPX86(con); - case TypeMemPtr tmp -> throw Utils.TODO(); - case TypeNil tn -> throw Utils.TODO(); - // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. - case Type t -> t == Type.NIL ? new IntX86(con) : new ConstantNode(con); + case TypeInteger ti -> new IntX86(con); + case TypeFloat tf -> new FltX86(con); + case TypeFunPtr tfp -> new TFPX86(con); + case TypeMemPtr tmp -> new TMPX86(con); + case TypeNil tn -> throw Utils.TODO(); + // TOP, BOTTOM, XCtrl, Ctrl, etc. Never any executable code. + case Type t -> t == Type.NIL ? new IntX86(con) : new ConstantNode(con); }; } diff --git a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java index 20dd43d..0c5c680 100644 --- a/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java +++ b/seaofnodes/src/main/java/com/compilerprogramming/ezlang/compiler/print/ASMPrinter.java @@ -1,5 +1,6 @@ package com.compilerprogramming.ezlang.compiler.print; +import com.compilerprogramming.ezlang.compiler.Ary; import com.compilerprogramming.ezlang.compiler.codegen.CodeGen; import com.compilerprogramming.ezlang.compiler.SB; import com.compilerprogramming.ezlang.compiler.codegen.Encoding; @@ -20,54 +21,71 @@ public static SB print(SB sb, CodeGen code) { if( code._cfg.at(i) instanceof FunNode fun ) iadr = print(iadr,sb,code,fun,i); - // Skip padding - while( ((iadr+7) & -8) > iadr ) - iadr++; - // constant pool + iadr = (iadr+15)&-16; // pad to 16 Encoding enc = code._encoding; - if( enc!=null && !enc._bigCons.isEmpty() ) { - iadr = (iadr+15)&-16; // pad to 16 + if( enc!=null && !enc._bigCons.isEmpty() && iadr < enc._bits.size() ) { + // radix sort the big constants by alignment + Ary[] raligns = new Ary[5]; + for( Node op : enc._bigCons.keySet() ) { + Encoding.Relo relo = enc._bigCons.get(op); + int align = relo._t.alignment(); + Ary relos = raligns[align]==null ? (raligns[align]=new Ary<>( Encoding.Relo.class)) : raligns[align]; + relos.add(relo); + } + HashSet targets = new HashSet<>(); sb.p("--- Constant Pool ------").nl(); - // By log size - for( int log = 3; log >= 0; log-- ) { - for( Node op : enc._bigCons.keySet() ) { - Encoding.Relo relo = enc._bigCons.get(op); + + // By alignment + for( int align = 4; align >= 0; align-- ) { + Ary relos = raligns[align]; + if( relos == null ) continue; + for( Encoding.Relo relo : relos ) { if( targets.contains(relo._t) ) continue; targets.add(relo._t); - if( relo._t.log_size()==log ) { - sb.hex2(iadr).p(" "); - if( relo._t instanceof TypeTuple tt ) { - for( Type tx : tt._types ) { - switch( log ) { - case 0: sb.hex1(enc.read1(iadr)); break; - case 1: sb.hex2(enc.read2(iadr)); break; - case 2: sb.hex4(enc.read4(iadr)); break; - case 3: sb.hex8(enc.read8(iadr)); break; - } - iadr += (1< { + for( Type tx : tt._types ) { + pN(enc,sb,iadr,align).p(" "); + iadr += (1< { + int sz = ts.size(); + int log = 1< { + pN(enc,sb,iadr,align).fix(9-(1<