Title: [221468] trunk/Tools
Revision
221468
Author
fpi...@apple.com
Date
2017-08-31 19:52:14 -0700 (Thu, 31 Aug 2017)

Log Message

WSL should support loading from arrays
https://bugs.webkit.org/show_bug.cgi?id=176207

Reviewed by Saam Barati.
        
Getting a test that loads from arrays to work required doing a lot of things:
        
- a[i] now parses to ^(operator\[](a, i)).
- added operator\[] for four array reference types.
- to make thread|threadgroup|device T[] work, you need T:primitive.
- so this adds the magical primitive protocol.
        
This required a little bit of rejuggling in the name resolver and type checker. The main thing
is that to prevent the rewriter from copying the types referenced by some function, you need to
make sure that when you add a reference to a type, you wrap it in a TypeRef. This doesn't
completely feel right, but I'm sure we'll figure out the balance eventually. See bug 176208.

* WebGPUShadingLanguageRI/All.js:
* WebGPUShadingLanguageRI/Checker.js:
(Checker.prototype.visitProtocolDecl.set throw):
* WebGPUShadingLanguageRI/EArrayRef.js: Added.
(EArrayRef):
(EArrayRef.prototype.get ptr):
(EArrayRef.prototype.get length):
(EArrayRef.prototype.toString):
* WebGPUShadingLanguageRI/EPtr.js:
(EPtr.prototype.plus):
(EPtr.prototype.toString):
(EPtr):
* WebGPUShadingLanguageRI/Evaluator.js:
(Evaluator.prototype.visitUintLiteral):
* WebGPUShadingLanguageRI/FuncInstantiator.js:
(FuncInstantiator.prototype.getUnique.Instantiate.prototype.visitNativeFunc):
(FuncInstantiator.prototype.getUnique.Instantiate):
(FuncInstantiator.prototype.getUnique):
(FuncInstantiator):
* WebGPUShadingLanguageRI/Intrinsics.js:
(Intrinsics):
* WebGPUShadingLanguageRI/Lexer.js:
(Lexer.prototype.next):
(Lexer):
* WebGPUShadingLanguageRI/NameContext.js:
(NameContext.prototype.recognizeIntrinsics):
* WebGPUShadingLanguageRI/NameResolver.js:
(NameResolver.prototype.visitProtocolRef):
* WebGPUShadingLanguageRI/NativeFuncInstance.js:
(NativeFuncInstance.prototype.get isNative):
* WebGPUShadingLanguageRI/Parse.js:
(parseTerm):
(parseTypeDef):
(parseNative):
(parsePossibleSuffix):
(parse):
* WebGPUShadingLanguageRI/ProtocolDecl.js:
(ProtocolDecl):
* WebGPUShadingLanguageRI/ProtocolRef.js:
(ProtocolRef.prototype.get isPrimitive):
(ProtocolRef):
* WebGPUShadingLanguageRI/PtrType.js:
(PtrType.prototype.populateDefaultValue): Deleted.
* WebGPUShadingLanguageRI/ReferenceType.js:
(ReferenceType.prototype.populateDefaultValue):
(ReferenceType):
* WebGPUShadingLanguageRI/Rewriter.js:
(Rewriter.prototype.visitUintLiteral):
(Rewriter.prototype.visitFunc): Deleted.
(Rewriter.prototype.visitTypeVariable): Deleted.
(Rewriter.prototype.visitConstexprTypeParameter): Deleted.
* WebGPUShadingLanguageRI/StandardLibrary.js:
* WebGPUShadingLanguageRI/Test.js:
(TEST_threadArrayLoad):
(TEST_deviceArrayLoad):
* WebGPUShadingLanguageRI/TypeRef.js:
(TypeRef.prototype.get instantiatedType):
* WebGPUShadingLanguageRI/TypeVariable.js:
(TypeVariable.prototype.toString):
(TypeVariable):
* WebGPUShadingLanguageRI/UintLiteral.js: Added.
(UintLiteral):
(UintLiteral.prototype.get value):
(UintLiteral.prototype.get isConstexpr):
(UintLiteral.prototype.toString):
* WebGPUShadingLanguageRI/Visitor.js:
(Visitor.prototype.visitNativeFuncInstance):
(Visitor.prototype.visitTypeVariable):
(Visitor.prototype.visitPtrType):
(Visitor.prototype.visitArrayRefType):
(Visitor.prototype.visitUintLiteral):
* WebGPUShadingLanguageRI/WTrapError.js: Added.
(WTrapError):

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (221467 => 221468)


--- trunk/Tools/ChangeLog	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/ChangeLog	2017-09-01 02:52:14 UTC (rev 221468)
@@ -1,3 +1,96 @@
+2017-08-31  Filip Pizlo  <fpi...@apple.com>
+
+        WSL should support loading from arrays
+        https://bugs.webkit.org/show_bug.cgi?id=176207
+
+        Reviewed by Saam Barati.
+        
+        Getting a test that loads from arrays to work required doing a lot of things:
+        
+        - a[i] now parses to ^(operator\[](a, i)).
+        - added operator\[] for four array reference types.
+        - to make thread|threadgroup|device T[] work, you need T:primitive.
+        - so this adds the magical primitive protocol.
+        
+        This required a little bit of rejuggling in the name resolver and type checker. The main thing
+        is that to prevent the rewriter from copying the types referenced by some function, you need to
+        make sure that when you add a reference to a type, you wrap it in a TypeRef. This doesn't
+        completely feel right, but I'm sure we'll figure out the balance eventually. See bug 176208.
+
+        * WebGPUShadingLanguageRI/All.js:
+        * WebGPUShadingLanguageRI/Checker.js:
+        (Checker.prototype.visitProtocolDecl.set throw):
+        * WebGPUShadingLanguageRI/EArrayRef.js: Added.
+        (EArrayRef):
+        (EArrayRef.prototype.get ptr):
+        (EArrayRef.prototype.get length):
+        (EArrayRef.prototype.toString):
+        * WebGPUShadingLanguageRI/EPtr.js:
+        (EPtr.prototype.plus):
+        (EPtr.prototype.toString):
+        (EPtr):
+        * WebGPUShadingLanguageRI/Evaluator.js:
+        (Evaluator.prototype.visitUintLiteral):
+        * WebGPUShadingLanguageRI/FuncInstantiator.js:
+        (FuncInstantiator.prototype.getUnique.Instantiate.prototype.visitNativeFunc):
+        (FuncInstantiator.prototype.getUnique.Instantiate):
+        (FuncInstantiator.prototype.getUnique):
+        (FuncInstantiator):
+        * WebGPUShadingLanguageRI/Intrinsics.js:
+        (Intrinsics):
+        * WebGPUShadingLanguageRI/Lexer.js:
+        (Lexer.prototype.next):
+        (Lexer):
+        * WebGPUShadingLanguageRI/NameContext.js:
+        (NameContext.prototype.recognizeIntrinsics):
+        * WebGPUShadingLanguageRI/NameResolver.js:
+        (NameResolver.prototype.visitProtocolRef):
+        * WebGPUShadingLanguageRI/NativeFuncInstance.js:
+        (NativeFuncInstance.prototype.get isNative):
+        * WebGPUShadingLanguageRI/Parse.js:
+        (parseTerm):
+        (parseTypeDef):
+        (parseNative):
+        (parsePossibleSuffix):
+        (parse):
+        * WebGPUShadingLanguageRI/ProtocolDecl.js:
+        (ProtocolDecl):
+        * WebGPUShadingLanguageRI/ProtocolRef.js:
+        (ProtocolRef.prototype.get isPrimitive):
+        (ProtocolRef):
+        * WebGPUShadingLanguageRI/PtrType.js:
+        (PtrType.prototype.populateDefaultValue): Deleted.
+        * WebGPUShadingLanguageRI/ReferenceType.js:
+        (ReferenceType.prototype.populateDefaultValue):
+        (ReferenceType):
+        * WebGPUShadingLanguageRI/Rewriter.js:
+        (Rewriter.prototype.visitUintLiteral):
+        (Rewriter.prototype.visitFunc): Deleted.
+        (Rewriter.prototype.visitTypeVariable): Deleted.
+        (Rewriter.prototype.visitConstexprTypeParameter): Deleted.
+        * WebGPUShadingLanguageRI/StandardLibrary.js:
+        * WebGPUShadingLanguageRI/Test.js:
+        (TEST_threadArrayLoad):
+        (TEST_deviceArrayLoad):
+        * WebGPUShadingLanguageRI/TypeRef.js:
+        (TypeRef.prototype.get instantiatedType):
+        * WebGPUShadingLanguageRI/TypeVariable.js:
+        (TypeVariable.prototype.toString):
+        (TypeVariable):
+        * WebGPUShadingLanguageRI/UintLiteral.js: Added.
+        (UintLiteral):
+        (UintLiteral.prototype.get value):
+        (UintLiteral.prototype.get isConstexpr):
+        (UintLiteral.prototype.toString):
+        * WebGPUShadingLanguageRI/Visitor.js:
+        (Visitor.prototype.visitNativeFuncInstance):
+        (Visitor.prototype.visitTypeVariable):
+        (Visitor.prototype.visitPtrType):
+        (Visitor.prototype.visitArrayRefType):
+        (Visitor.prototype.visitUintLiteral):
+        * WebGPUShadingLanguageRI/WTrapError.js: Added.
+        (WTrapError):
+
 2017-08-31  Alex Christensen  <achristen...@webkit.org>
 
         Add WKUIDelegatePrivate equivalent of WKPageUIClient's isPlayingAudioDidChange

Modified: trunk/Tools/WebGPUShadingLanguageRI/All.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/All.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/All.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -45,6 +45,7 @@
 load("CommaExpression.js");
 load("ConstexprTypeParameter.js");
 load("DereferenceExpression.js");
+load("EArrayRef.js");
 load("EBuffer.js");
 load("EBufferBuilder.js");
 load("EPtr.js");
@@ -93,9 +94,11 @@
 load("TypeRef.js");
 load("TypeVariable.js");
 load("TypedValue.js");
+load("UintLiteral.js");
 load("UnificationContext.js");
 load("VariableDecl.js");
 load("VariableRef.js");
 load("VisitingSet.js");
 load("WSyntaxError.js");
+load("WTrapError.js");
 load("WTypeError.js");

Modified: trunk/Tools/WebGPUShadingLanguageRI/Checker.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Checker.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Checker.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -100,8 +100,8 @@
         if (node.addressSpace == "thread")
             return;
         
-        if (!node.elementType.withRecursivelyInstantiatedImmediates.isPrimitive)
-            throw new WTypeError(node.origin.originString, "Illegal pointer to non-primitive type");
+        if (!node.elementType.instantiatedType.isPrimitive)
+            throw new WTypeError(node.origin.originString, "Illegal pointer to non-primitive type: " + node.elementType);
     }
     
     visitArrayType(node)
@@ -129,7 +129,7 @@
         let rhsType = node.rhs.visit(this);
         if (!lhsType.equals(rhsType))
             throw new WTypeError(node.origin.originString, "Type mismatch in assignment: " + lhsType + " versus " + rhsType);
-        node.type = lhsType;
+        node.type = TypeRef.wrap(lhsType);
         return lhsType;
     }
     
@@ -138,7 +138,7 @@
         let type = node.ptr.visit(this).unifyNode;
         if (!type.isPtr)
             throw new WTypeError(node.origin.originString, "Type passed to dereference is not a pointer: " + type);
-        node.type = type.elementType;
+        node.type = TypeRef.wrap(type.elementType);
         node.addressSpace = type.addressSpace;
         return node.type;
     }
@@ -175,9 +175,17 @@
     
     visitIntLiteral(node)
     {
+        // FIXME: This should return some kind of type variable that defaults to int but is happy to
+        // unify with uint.
+        // https://bugs.webkit.org/show_bug.cgi?id=176209
         return this._program.intrinsics.int32;
     }
     
+    visitUintLiteral(node)
+    {
+        return this._program.intrinsics.uint32;
+    }
+    
     visitCommaExpression(node)
     {
         let result = null;

Added: trunk/Tools/WebGPUShadingLanguageRI/EArrayRef.js (0 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/EArrayRef.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/EArrayRef.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+"use strict";
+
+class EArrayRef {
+    constructor(ptr, length)
+    {
+        this._ptr = ptr;
+        this._length = length;
+    }
+    
+    get ptr() { return this._ptr; }
+    get length() { return this._length; }
+    
+    toString()
+    {
+        return "A:<" + this.ptr + ", " + this.length + ">";
+    }
+}
+

Modified: trunk/Tools/WebGPUShadingLanguageRI/EPtr.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/EPtr.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/EPtr.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -46,6 +46,11 @@
     get buffer() { return this._buffer; }
     get offset() { return this._offset; }
     
+    plus(offset)
+    {
+        return new EPtr(this.buffer, this.offset + offset);
+    }
+    
     loadValue()
     {
         return this.buffer.get(this.offset);
@@ -59,8 +64,6 @@
     
     toString()
     {
-        if (!this.buffer)
-            return "null";
         return "B" + this.buffer.index + ":" + this.offset;
     }
 }

Modified: trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -121,6 +121,11 @@
         return EPtr.box(node.value);
     }
     
+    visitUintLiteral(node)
+    {
+        return EPtr.box(node.value);
+    }
+    
     visitCallExpression(node)
     {
         // We evaluate inlined ASTs, so this can only be a native call.

Modified: trunk/Tools/WebGPUShadingLanguageRI/FuncInstantiator.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/FuncInstantiator.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/FuncInstantiator.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -71,7 +71,7 @@
                 return new NativeFuncInstance(
                     func,
                     func.returnType.visit(substitution),
-                    parameters.map(parameter => parameter.visit(substitution)));
+                    func.parameters.map(parameter => parameter.visit(substitution)));
             }
         }
         let resultingFunc = func.visit(new Instantiate());

Modified: trunk/Tools/WebGPUShadingLanguageRI/Intrinsics.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Intrinsics.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Intrinsics.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -25,8 +25,12 @@
 "use strict";
 
 class Intrinsics {
-    constructor()
+    constructor(nameContext)
     {
+        this.primitive = new ProtocolDecl(null, "primitive");
+        this.primitive.isPrimitive = true;
+        nameContext.add(this.primitive);
+        
         this._map = new Map();
 
         // NOTE: Intrinsic resolution happens before type name resolution, so the strings we use here
@@ -36,7 +40,7 @@
         
         this._map.set(
             "native primitive type void<>",
-            (type) => {
+            type => {
                 this.void = type;
                 type.size = 0;
                 type.populateDefaultValue = () => { };
@@ -44,7 +48,7 @@
 
         this._map.set(
             "native primitive type int32<>",
-            (type) => {
+            type => {
                 this.int32 = type;
                 type.size = 1;
                 type.populateDefaultValue = (buffer, offset) => buffer.set(offset, 0);
@@ -51,8 +55,16 @@
             });
 
         this._map.set(
+            "native primitive type uint32<>",
+            type => {
+                this.uint32 = type;
+                type.size = 1;
+                type.populateDefaultValue = (buffer, offset) => buffer.set(offset, 0);
+            });
+
+        this._map.set(
             "native primitive type double<>",
-            (type) => {
+            type => {
                 this.double = type;
                 type.size = 1;
                 type.populateDefaultValue = (buffer, offset) => buffer.set(offset, 0);
@@ -60,10 +72,33 @@
         
         this._map.set(
             "native int operator+<>(int,int)",
-            (func) => {
-                func.implementation =
-                    ([left, right]) => EPtr.box((left.loadValue() + right.loadValue()) | 0);
+            func => {
+                func.implementation = ([left, right]) =>
+                    EPtr.box((left.loadValue() + right.loadValue()) | 0);
             });
+        
+        let arrayElementPtr = func => {
+            func.implementation = ([ref, index], node) => {
+                ref = ref.loadValue();
+                index = index.loadValue();
+                if (index > ref.length)
+                    throw new WTrapError(node.origin.originString, "Array index " + index + " is out of bounds of " + ref);
+                return EPtr.box(ref.ptr.plus(index * node.actualTypeArguments[0].size));
+            };
+        };
+        
+        this._map.set(
+            "native thread T^ operator\\[]<T>(thread T[],uint)",
+            arrayElementPtr);
+        this._map.set(
+            "native threadgroup T^ operator\\[]<T:primitive>(threadgroup T[],uint)",
+            arrayElementPtr);
+        this._map.set(
+            "native device T^ operator\\[]<T:primitive>(device T[],uint)",
+            arrayElementPtr);
+        this._map.set(
+            "native constant T^ operator\\[]<T:primitive>(constant T[],uint)",
+            arrayElementPtr);
     }
     
     add(thing)

Modified: trunk/Tools/WebGPUShadingLanguageRI/Lexer.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Lexer.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Lexer.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -105,6 +105,9 @@
         if (/^[^\d\W]\w*/.test(relevantText))
             return result("identifier");
 
+        if (/^[0-9]+u/.test(relevantText))
+            return result("uintLiteral");
+
         if (/^[0-9]+/.test(relevantText))
             return result("intLiteral");
 
@@ -112,7 +115,7 @@
         if (/^([0-9]*\.[0-9]+)|([0-9]+\.[0-9]*)/.test(relevantText))
             return result("doubleLiteral");
         
-        if (/^(struct|protocol|type|if|else|enum|continue|break|switch|case|default|for|while|do|return|sizeof|constant|device|threadgroup|thread|operator|null)/.test(relevantText))
+        if (/^(struct|protocol|typedef|if|else|enum|continue|break|switch|case|default|for|while|do|return|sizeof|constant|device|threadgroup|thread|operator|null)/.test(relevantText))
             return result("keyword");
         
         if (/^([{}()\[\]?:=+*\/,.%!~^&|<>\\;-]|->|=>|<=|==|!=|\+=|-=|\*=|\/=|%=|^=|\|=|&=)/.test(relevantText))

Modified: trunk/Tools/WebGPUShadingLanguageRI/NameContext.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/NameContext.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/NameContext.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -137,7 +137,7 @@
     
     recognizeIntrinsics()
     {
-        this._intrinsics = new Intrinsics();
+        this._intrinsics = new Intrinsics(this);
     }
     
     get intrinsics()

Modified: trunk/Tools/WebGPUShadingLanguageRI/NameResolver.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/NameResolver.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/NameResolver.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -100,6 +100,14 @@
             signature.visit(checker);
     }
     
+    visitProtocolRef(node)
+    {
+        let result = this._nameContext.get(Protocol, node.name);
+        if (!result)
+            throw new WTypeError(node.origin.originString, "Could not find protocol named " + node.name);
+        node.protocolDecl = result;
+    }
+    
     visitTypeDef(node)
     {
         let nameContext = new NameContext(this._nameContext);

Modified: trunk/Tools/WebGPUShadingLanguageRI/NativeFuncInstance.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/NativeFuncInstance.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/NativeFuncInstance.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -32,6 +32,7 @@
     }
     
     get func() { return this._func; }
+    get isNative() { return true; }
 
     toString()
     {

Modified: trunk/Tools/WebGPUShadingLanguageRI/Parse.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Parse.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Parse.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -177,6 +177,12 @@
                 lexer.fail("Integer literal is not 32-bit integer");
             return new IntLiteral(token, intVersion);
         }
+        if (token = tryConsumeKind("uintLiteral")) {
+            let uintVersion = token.text >>> 0;
+            if (uintVersion + "u" !== token.text)
+                lexer.fail("Integer literal is not 32-bit unsigned integer");
+            return new UintLiteral(token, uintVersion);
+        }
         if (token = tryConsumeKind("doubleLiteral")) {
             token = consumeKind("doubleLiteral");
             return new DoubleLiteral(token, +token.text);
@@ -280,7 +286,7 @@
     
     function parseTypeDef()
     {
-        let origin = consume("type");
+        let origin = consume("typedef");
         let name = consumeKind("identifier").text;
         let typeParameters = parseTypeParameters();
         consume("=");
@@ -293,10 +299,10 @@
     {
         let origin = consume("native");
         let isType = lexer.backtrackingScope(() => {
-            if (tryConsume("type"))
+            if (tryConsume("typedef"))
                 return "normal";
             consume("primitive");
-            consume("type");
+            consume("typedef");
             return "primitive";
         });
         if (isType) {
@@ -374,7 +380,9 @@
             case "[": {
                 let index = parseExpression();
                 consume("]");
-                left = new IndexExpression(token, left, index);
+                left = new DereferenceExpression(
+                    token,
+                    new CallExpression(token, "operator\\[]", [], [left, index]));
                 break;
             }
             default:
@@ -644,7 +652,7 @@
             return;
         if (token.text == ";")
             lexer.next();
-        else if (token.text == "type")
+        else if (token.text == "typedef")
             program.add(parseTypeDef());
         else if (token.text == "native")
             program.add(parseNative());

Modified: trunk/Tools/WebGPUShadingLanguageRI/ProtocolDecl.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/ProtocolDecl.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/ProtocolDecl.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -31,6 +31,7 @@
         this._signatures = [];
         this._signatureMap = new Map();
         this._typeVariable = new TypeVariable(origin, name, null);
+        this.isPrimitive = false;
     }
     
     addSignature(signature)

Modified: trunk/Tools/WebGPUShadingLanguageRI/ProtocolRef.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/ProtocolRef.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/ProtocolRef.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -40,4 +40,9 @@
     {
         return this.protocolDecl.hasHeir(type);
     }
+    
+    get isPrimitive()
+    {
+        return this.protocolDecl.isPrimitive;
+    }
 }

Modified: trunk/Tools/WebGPUShadingLanguageRI/PtrType.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/PtrType.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/PtrType.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -27,11 +27,6 @@
 class PtrType extends ReferenceType {
     get isPtr() { return true; }
     
-    populateDefaultValue(buffer, offset)
-    {
-        buffer.set(offset, null);
-    }
-    
     unifyImpl(unificationContext, other)
     {
         if (!(other instanceof PtrType))

Modified: trunk/Tools/WebGPUShadingLanguageRI/ReferenceType.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/ReferenceType.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/ReferenceType.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -40,5 +40,10 @@
     get isPrimitive() { return false; }
 
     get size() { return 1; }
+
+    populateDefaultValue(buffer, offset)
+    {
+        buffer.set(offset, null);
+    }
 }
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -24,6 +24,9 @@
  */
 "use strict";
 
+// FIXME: This should have sensible behavior when it encounters definitions that it cannot handle. Right
+// now we are hackishly preventing this by wrapping things in TypeRef. That's probably wrong.
+// https://bugs.webkit.org/show_bug.cgi?id=176208
 class Rewriter {
     constructor()
     {
@@ -44,14 +47,6 @@
         return oldItem;
     }
     
-    visitFunc(node)
-    {
-        return new Func(
-            node.name, node.returnType.visit(this),
-            node.typeParameters.map(typeParameter => typeParameter.visit(this)),
-            node.parameters.map(parameter => parameter.visit(this)));
-    }
-    
     visitFuncParameter(node)
     {
         let result = new FuncParameter(node.origin, node.name, node.type.visit(this));
@@ -97,20 +92,12 @@
     visitTypeRef(node)
     {
         let result = new TypeRef(node.origin, node.name, node.typeArguments.map(typeArgument => typeArgument.visit(this)));
+        // We should probably visit this type.
+        // https://bugs.webkit.org/show_bug.cgi?id=176208
         result.type = node.type;
         return result;
     }
     
-    visitTypeVariable(node)
-    {
-        return node;
-    }
-    
-    visitConstexprTypeParameter(node)
-    {
-        return new ConstexprTypeParameter(node.origin, node.name, node.type.visit(this));
-    }
-    
     visitField(node)
     {
         return new Field(node.origin, node.name, node.type.visit(this));
@@ -171,6 +158,11 @@
         return node;
     }
 
+    visitUintLiteral(node)
+    {
+        return node;
+    }
+
     visitCallExpression(node)
     {
         let result = new CallExpression(

Modified: trunk/Tools/WebGPUShadingLanguageRI/StandardLibrary.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/StandardLibrary.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/StandardLibrary.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -27,12 +27,21 @@
 // NOTE: The next line is line 28, and we rely on this in Prepare.js.
 const standardLibrary = `
 // This is the WSL standard library. Implementations of all of these things are in
-// Intrinsics.js.
+// Intrinsics.js. The only thing that gets defined before we get here is the primitive
+// protocol.
 
 // Need to bootstrap void first.
-native primitive type void;
-native primitive type int32;
-type int = int32;
+native primitive typedef void;
 
+native primitive typedef int32;
+native primitive typedef uint32;
+typedef int = int32;
+typedef uint = uint32;
+
 native int operator+(int, int);
+
+native thread T^ operator\\[]<T>(thread T[], uint);
+native threadgroup T^ operator\\[]<T:primitive>(threadgroup T[], uint);
+native device T^ operator\\[]<T:primitive>(device T[], uint);
+native constant T^ operator\\[]<T:primitive>(constant T[], uint);
 `;

Modified: trunk/Tools/WebGPUShadingLanguageRI/Test.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Test.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Test.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -157,6 +157,32 @@
         throw new Error("Expected 42 but got: " + value);
 }
 
+function TEST_threadArrayLoad()
+{
+    let program = doPrep(`
+        int foo(thread int[] array)
+        {
+            return array[0u];
+        }`);
+    let buffer = new EBuffer(1);
+    buffer.set(0, 89);
+    let result = callFunction(program, "foo", [], [TypedValue.box(new ArrayRefType(null, "thread", program.intrinsics.int32), new EArrayRef(new EPtr(buffer, 0), 1))]);
+    checkInt(program, result, 89);
+}
+
+function TEST_deviceArrayLoad()
+{
+    let program = doPrep(`
+        int foo(device int[] array)
+        {
+            return array[0u];
+        }`);
+    let buffer = new EBuffer(1);
+    buffer.set(0, 89);
+    let result = callFunction(program, "foo", [], [TypedValue.box(new ArrayRefType(null, "device", program.intrinsics.int32), new EArrayRef(new EPtr(buffer, 0), 1))]);
+    checkInt(program, result, 89);
+}
+
 let before = preciseTime();
 
 let filter = /.*/; // run everything by default

Modified: trunk/Tools/WebGPUShadingLanguageRI/TypeRef.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/TypeRef.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/TypeRef.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -48,6 +48,8 @@
     get instantiatedType()
     {
         let type = this.type.unifyNode;
+        if (!this.typeArguments.length)
+            return type;
         if (!type.instantiate)
             throw new Error("type does not support instantiation: " + type + " (" + type.constructor.name + ")");
         return type.instantiate(this.typeArguments);

Modified: trunk/Tools/WebGPUShadingLanguageRI/TypeVariable.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/TypeVariable.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/TypeVariable.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -93,7 +93,7 @@
     
     toString()
     {
-        return this.name;
+        return this.name + (this.protocol ? ":" + this.protocol.name : "");
     }
 }
 

Added: trunk/Tools/WebGPUShadingLanguageRI/UintLiteral.js (0 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/UintLiteral.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/UintLiteral.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+"use strict";
+
+class UintLiteral extends _expression_ {
+    constructor(origin, value)
+    {
+        super(origin);
+        this._value = value;
+    }
+    
+    get value() { return this._value; }
+    get isConstexpr() { return true; }
+    
+    toString()
+    {
+        return "" + this._value + "u";
+    }
+}
+

Modified: trunk/Tools/WebGPUShadingLanguageRI/Visitor.js (221467 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/Visitor.js	2017-09-01 02:01:43 UTC (rev 221467)
+++ trunk/Tools/WebGPUShadingLanguageRI/Visitor.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -56,6 +56,12 @@
         this.visitFunc(node);
     }
     
+    visitNativeFuncInstance(node)
+    {
+        node.func.visit(this);
+        this.visitFunc(node);
+    }
+    
     visitBlock(node)
     {
         for (let statement of node.statements)
@@ -107,6 +113,8 @@
     
     visitTypeVariable(node)
     {
+        if (node.protocol)
+            node.protocol.visit(this);
     }
     
     visitConstexprTypeParameter(node)
@@ -124,14 +132,19 @@
         node.elementType.visit(this);
     }
     
-    visitPtrType(node)
+    visitReferenceType(node)
     {
         this.visitElementalType(node);
     }
     
+    visitPtrType(node)
+    {
+        this.visitReferenceType(node);
+    }
+    
     visitArrayRefType(node)
     {
-        this.visitElementalType(node);
+        this.visitReferenceType(node);
     }
     
     visitArrayType(node)
@@ -177,6 +190,10 @@
     {
     }
     
+    visitUintLiteral(node)
+    {
+    }
+    
     visitCallExpression(node)
     {
         for (let typeArgument of node.typeArguments)

Added: trunk/Tools/WebGPUShadingLanguageRI/WTrapError.js (0 => 221468)


--- trunk/Tools/WebGPUShadingLanguageRI/WTrapError.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/WTrapError.js	2017-09-01 02:52:14 UTC (rev 221468)
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+"use strict";
+
+class WTrapError extends Error {
+    constructor(originString, message)
+    {
+        super("Trap at " + originString + ": " + message);
+        this.originString = originString;
+        this.syntaxErrorMessage = message;
+    }
+}
+
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to