From 664761624f4b0e3d6d1593b99e576854beec5949 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 6 May 2025 13:44:10 -0700 Subject: Chapter 23: Methods and Revisiting Types --- src/test/java/com/seaofnodes/simple/Eval2.java | 45 ++++++++++++++++---------- 1 file changed, 28 insertions(+), 17 deletions(-) (limited to 'src/test/java/com/seaofnodes/simple/Eval2.java') diff --git a/src/test/java/com/seaofnodes/simple/Eval2.java b/src/test/java/com/seaofnodes/simple/Eval2.java index 86f0e9db..094b5143 100644 --- a/src/test/java/com/seaofnodes/simple/Eval2.java +++ b/src/test/java/com/seaofnodes/simple/Eval2.java @@ -4,6 +4,8 @@ import com.seaofnodes.simple.codegen.CodeGen; import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.print.IRPrinter; import com.seaofnodes.simple.type.*; +import com.seaofnodes.simple.util.SB; +import com.seaofnodes.simple.util.Utils; import java.util.*; public abstract class Eval2 { @@ -115,7 +117,7 @@ public abstract class Eval2 { public static String eval( CodeGen code, long arg ) { return eval(code,arg,1000); } public static String eval( CodeGen code, long arg, int timeout ) { if( code._start.uctrl()==null ) return ""; // The empty program - SB trace = null; // new SB(); // TRACE, set to null for off, new SB() for on + SB trace = null; // = new SB(); // TRACE, set to null for off, new SB() for on // Force local scheduling phase code.driver(CodeGen.Phase.LocalSched); // Set global, so don't have to pass everywhere @@ -302,9 +304,20 @@ public abstract class Eval2 { return switch ( t ) { case TypeInteger i -> i.isConstant() ? (Long )i.value() : "INT"; case TypeFloat f -> f.isConstant() ? (Double)f.value() : "FLT"; - case TypeMemPtr tmp -> tmp; case TypeFunPtr tfp -> tfp; case TypeMem mem -> "MEM"; + case TypeMemPtr tmp -> { + if( tmp._obj.isAry() ) { // Constant array ptr + TypeConAry con = (TypeConAry)tmp._obj.field("[]")._t; + Object[] xs = new Object[con.len()]; + for( int i=0; i null; }; } @@ -313,8 +326,7 @@ public abstract class Eval2 { private static int offToIdx( long off, TypeStruct t) { off -= t.aryBase(); int scale = t.aryScale(); - long mask = (1L << scale)-1; - assert (off & mask) == 0; + assert (off & ((1L << scale)-1)) == 0; return (int)(off>>scale); } @@ -328,7 +340,7 @@ public abstract class Eval2 { if( n!=x || n<0 ) throw new NegativeArraySizeException(""+n); Object[] ary = new Object[n]; // Array body - var elem = type._fields[1]._type; + var elem = type._fields[1]._t; if (elem instanceof TypeInteger) { Arrays.fill( ary, 0L ); } else if (elem instanceof TypeFloat) { @@ -342,7 +354,7 @@ public abstract class Eval2 { int num = type._fields.length; Object[] ptr = new Object[num]; for( int i=0; i { if( visit.containsKey(x) ) yield sb.p("$cyclic"); visit.put(x,x); - // Since never can close the type cycle, without cyclic types, have - // to handle FRefs even here - if( tmp.isFRef() ) - tmp = tmp.makeFrom(((TypeMemPtr)Parser.TYPES.get(tmp._obj._name))._obj); + assert !tmp.isFRef(); Object[] xs = (Object[])x; // Array of fields if( tmp._obj.isAry() ) { - Type elem = tmp._obj._fields[1]._type; + Type elem = tmp._obj._fields[1]._t; if( elem == TypeInteger.U8 ) { // Shortcut u8[] as a String for( Object o : xs ) @@ -427,6 +437,7 @@ public abstract class Eval2 { } else { // Array of elements elem.print(sb).p("[ "); + if( elem instanceof TypeConAry ) elem = TypeInteger.ZERO; for( Object o : xs ) _print( elem, o, sb, visit ).p( "," ); sb.unchar().p("]"); @@ -435,7 +446,7 @@ public abstract class Eval2 { sb.p(tmp._obj._name).p("{"); Field[] flds = tmp._obj._fields; for( int i=0; i