summaryrefslogtreecommitdiff
path: root/chapter15/src/main/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-09-12 11:40:20 -0700
committerCliff Click <cliffc@acm.org>2024-10-06 08:56:22 -0700
commit77d72a8d972061965f9d1155460d1a4c49561a5c (patch)
treed210352f0d36e0619fe1468c10d8139856369a59 /chapter15/src/main/java/com/seaofnodes
parent1b5c0f43171820812f09b71fa0512c0025d2d187 (diff)
downloadSimple-77d72a8d972061965f9d1155460d1a4c49561a5c.tar.gz
Simple-77d72a8d972061965f9d1155460d1a4c49561a5c.zip
Adding TypeArray
Diffstat (limited to 'chapter15/src/main/java/com/seaofnodes')
-rw-r--r--chapter15/src/main/java/com/seaofnodes/simple/type/Type.java1
-rw-r--r--chapter15/src/main/java/com/seaofnodes/simple/type/TypeArray.java70
-rw-r--r--chapter15/src/main/java/com/seaofnodes/simple/type/TypeStruct.java2
3 files changed, 73 insertions, 0 deletions
diff --git a/chapter15/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter15/src/main/java/com/seaofnodes/simple/type/Type.java
index 49dc090a..cb816848 100644
--- a/chapter15/src/main/java/com/seaofnodes/simple/type/Type.java
+++ b/chapter15/src/main/java/com/seaofnodes/simple/type/Type.java
@@ -39,6 +39,7 @@ public class Type {
static final byte TMEMPTR = 9; // Memory pointer type
static final byte TSTRUCT =10; // Structs; tuples with named fields
static final byte TFLD =11; // Fields into struct
+ static final byte TARRAY =12; // Array
public final byte _type;
diff --git a/chapter15/src/main/java/com/seaofnodes/simple/type/TypeArray.java b/chapter15/src/main/java/com/seaofnodes/simple/type/TypeArray.java
new file mode 100644
index 00000000..32d9259c
--- /dev/null
+++ b/chapter15/src/main/java/com/seaofnodes/simple/type/TypeArray.java
@@ -0,0 +1,70 @@
+package com.seaofnodes.simple.type;
+
+import com.seaofnodes.simple.Utils;
+import java.util.ArrayList;
+
+/**
+ * Represents an array type
+ */
+public class TypeArray extends Type {
+
+ // An Array has a type and a size; the size is any integer type.
+ // The type can be any scalar type, so not e.g. TypeMem.
+ public final Type _type;
+ public final TypeInteger _len;
+
+ private TypeArray(Type type, TypeInteger len) {
+ super(Type.TARRAY);
+ _type = type;
+ _len = len;
+ }
+
+ // All fields directly listed
+ public static TypeArray make(Type type, TypeInteger len) { return new TypeArray(type,len).intern(); }
+
+ public static final TypeArray BOT = make(Type.BOTTOM,TypeInteger.BOT);
+ public static final TypeArray FLTS = make(TypeFloat.BOT,TypeInteger.U32);
+
+ public static void gather(ArrayList<Type> ts) { ts.add(BOT); ts.add(FLTS); }
+
+ @Override
+ Type xmeet(Type t) {
+ TypeArray that = (TypeArray) t;
+ Type type = _type.meet(that._type);
+ TypeInteger len = (TypeInteger)_len.meet(that._len);
+ return make(type,len);
+ }
+
+ @Override
+ public TypeArray dual() {
+ return make(_type.dual(),_len.dual());
+ }
+
+ // Keeps the same array, but lower-bounds the type not length
+ @Override
+ public TypeArray glb() {
+ return make(_type.glb(),_len);
+ }
+
+ @Override
+ boolean eq(Type t) {
+ TypeArray ts = (TypeArray)t; // Invariant
+ return _type==ts._type && _len==ts._len;
+ }
+
+ @Override
+ int hash() {
+ return Utils.fold(Utils.rot(_type.hashCode(),17) ^ _len.hashCode());
+ }
+
+ @Override
+ public StringBuilder print(StringBuilder sb) {
+ _type.print(sb).append("[");
+ _len .print(sb).append("]");
+ return sb;
+ }
+
+ @Override public String str() {
+ return _type.str() + "[" + _len.str() + "]";
+ }
+}
diff --git a/chapter15/src/main/java/com/seaofnodes/simple/type/TypeStruct.java b/chapter15/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
index e2489d6f..b77f1dc2 100644
--- a/chapter15/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
+++ b/chapter15/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
@@ -12,6 +12,8 @@ public class TypeStruct extends Type {
// names and types. Briefly during parsing its allowed to have a
// forward-ref to a Struct; in this case the _fields array is null.
// Its illegal to attempt to load a field from a forward-ref struct.
+ // Example: "int rez = new S.x; struct S { int x; } return rez;" // Error, S not defined
+ // Rewrite: "struct S { int x; } int rez = new S.x; return rez;" // Ok, no forward ref
//
// During the normal optimization run, struct types "bottom out" at further
// struct references, so we don't have to handle e.g. cyclic types. The