Title: [221550] trunk/Tools
Revision
221550
Author
[email protected]
Date
2017-09-03 10:46:48 -0700 (Sun, 03 Sep 2017)

Log Message

WSL should support structs
https://bugs.webkit.org/show_bug.cgi?id=176290

Reviewed by Mylex Maxfield.
        
This adds parsing of struct types and makes them work.
        
This required adding a struct layout phase, which in turn required being a bit more careful
about how we instantiate functions and types. We cannot have any instantiation TypeRefs left
once we're doing struct layout.

* WebGPUShadingLanguageRI/All.js:
* WebGPUShadingLanguageRI/CallFunction.js:
(callFunction):
* WebGPUShadingLanguageRI/Checker.js:
(Checker.prototype.visitProtocolDecl.set throw):
* WebGPUShadingLanguageRI/DotExpression.js: Added.
(DotExpression):
(DotExpression.prototype.get struct):
(DotExpression.prototype.get fieldName):
(DotExpression.prototype.get isLValue):
(DotExpression.prototype.toString):
* WebGPUShadingLanguageRI/EBufferBuilder.js:
(EBufferBuilder.prototype._createEPtr):
* WebGPUShadingLanguageRI/EPtr.js:
(EPtr.prototype.copyFrom): Deleted.
(EPtr.prototype.toString): Deleted.
* WebGPUShadingLanguageRI/Evaluator.js:
(Evaluator.prototype.visitAssignment):
(Evaluator.prototype.visitDotExpression):
* WebGPUShadingLanguageRI/Field.js:
(Field):
* WebGPUShadingLanguageRI/FuncDef.js:
(FuncDef.prototype.rewrite):
* WebGPUShadingLanguageRI/FuncInstantiator.js:
(FuncInstantiator.prototype.getUnique):
(FuncInstantiator):
* WebGPUShadingLanguageRI/Inline.js:
(inline):
(_inlineFunction):
(resolveInlinedFunction):
* WebGPUShadingLanguageRI/Inliner.js:
(Inliner.prototype.visitDotExpression):
* WebGPUShadingLanguageRI/InstantiateImmediates.js:
(InstantiateImmediates.prototype.visitTypeRef):
(InstantiateImmediates.prototype.visitVariableDecl):
(InstantiateImmediates):
* WebGPUShadingLanguageRI/NameResolver.js:
(NameResolver.prototype._resolveTypeArguments):
(NameResolver.prototype.visitTypeRef):
* WebGPUShadingLanguageRI/Node.js:
(Node.prototype.visit):
* WebGPUShadingLanguageRI/OverloadResolutionFailure.js: Added.
(OverloadResolutionFailure):
(OverloadResolutionFailure.prototype.get func):
(OverloadResolutionFailure.prototype.get reason):
(OverloadResolutionFailure.prototype.toString):
* WebGPUShadingLanguageRI/Parse.js:
(parsePossibleSuffix):
(parseField):
(parseStructType):
(parse):
* WebGPUShadingLanguageRI/ResolveOverloadImpl.js:
(resolveOverloadImpl):
* WebGPUShadingLanguageRI/Rewriter.js:
(Rewriter.prototype.visitDotExpression):
(Rewriter.prototype.visitVariableRef):
* WebGPUShadingLanguageRI/StructLayoutBuilder.js: Added.
(StructLayoutBuilder):
(StructLayoutBuilder.prototype.visitStructType):
(StructLayoutBuilder.prototype.get offset):
(StructLayoutBuilder.prototype.visitField):
(StructLayoutBuilder.prototype.visitVariableRef):
(StructLayoutBuilder.prototype.visitVariableDecl):
* WebGPUShadingLanguageRI/StructType.js:
(StructType.prototype.add):
(StructType.prototype.instantiate):
(StructType.prototype.populateDefaultValue):
* WebGPUShadingLanguageRI/Substitution.js:
(Substitution.prototype.visitTypeRef):
(Substitution.prototype.visitVariableRef):
(Substitution):
* WebGPUShadingLanguageRI/Test.js:
(TEST_genericStructInstance):
* WebGPUShadingLanguageRI/Type.js:
(Type.prototype.inherits):
* WebGPUShadingLanguageRI/TypeRef.js:
(TypeRef.instantiate):
* WebGPUShadingLanguageRI/VariableRef.js:
(VariableRef.wrap):
* WebGPUShadingLanguageRI/Visitor.js:
(Visitor.prototype.visitTypeRef):
(Visitor.prototype.visitDotExpression):
(Visitor.prototype.visitVariableRef):

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (221549 => 221550)


--- trunk/Tools/ChangeLog	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/ChangeLog	2017-09-03 17:46:48 UTC (rev 221550)
@@ -1,5 +1,102 @@
 2017-09-02  Filip Pizlo  <[email protected]>
 
+        WSL should support structs
+        https://bugs.webkit.org/show_bug.cgi?id=176290
+
+        Reviewed by Mylex Maxfield.
+        
+        This adds parsing of struct types and makes them work.
+        
+        This required adding a struct layout phase, which in turn required being a bit more careful
+        about how we instantiate functions and types. We cannot have any instantiation TypeRefs left
+        once we're doing struct layout.
+
+        * WebGPUShadingLanguageRI/All.js:
+        * WebGPUShadingLanguageRI/CallFunction.js:
+        (callFunction):
+        * WebGPUShadingLanguageRI/Checker.js:
+        (Checker.prototype.visitProtocolDecl.set throw):
+        * WebGPUShadingLanguageRI/DotExpression.js: Added.
+        (DotExpression):
+        (DotExpression.prototype.get struct):
+        (DotExpression.prototype.get fieldName):
+        (DotExpression.prototype.get isLValue):
+        (DotExpression.prototype.toString):
+        * WebGPUShadingLanguageRI/EBufferBuilder.js:
+        (EBufferBuilder.prototype._createEPtr):
+        * WebGPUShadingLanguageRI/EPtr.js:
+        (EPtr.prototype.copyFrom): Deleted.
+        (EPtr.prototype.toString): Deleted.
+        * WebGPUShadingLanguageRI/Evaluator.js:
+        (Evaluator.prototype.visitAssignment):
+        (Evaluator.prototype.visitDotExpression):
+        * WebGPUShadingLanguageRI/Field.js:
+        (Field):
+        * WebGPUShadingLanguageRI/FuncDef.js:
+        (FuncDef.prototype.rewrite):
+        * WebGPUShadingLanguageRI/FuncInstantiator.js:
+        (FuncInstantiator.prototype.getUnique):
+        (FuncInstantiator):
+        * WebGPUShadingLanguageRI/Inline.js:
+        (inline):
+        (_inlineFunction):
+        (resolveInlinedFunction):
+        * WebGPUShadingLanguageRI/Inliner.js:
+        (Inliner.prototype.visitDotExpression):
+        * WebGPUShadingLanguageRI/InstantiateImmediates.js:
+        (InstantiateImmediates.prototype.visitTypeRef):
+        (InstantiateImmediates.prototype.visitVariableDecl):
+        (InstantiateImmediates):
+        * WebGPUShadingLanguageRI/NameResolver.js:
+        (NameResolver.prototype._resolveTypeArguments):
+        (NameResolver.prototype.visitTypeRef):
+        * WebGPUShadingLanguageRI/Node.js:
+        (Node.prototype.visit):
+        * WebGPUShadingLanguageRI/OverloadResolutionFailure.js: Added.
+        (OverloadResolutionFailure):
+        (OverloadResolutionFailure.prototype.get func):
+        (OverloadResolutionFailure.prototype.get reason):
+        (OverloadResolutionFailure.prototype.toString):
+        * WebGPUShadingLanguageRI/Parse.js:
+        (parsePossibleSuffix):
+        (parseField):
+        (parseStructType):
+        (parse):
+        * WebGPUShadingLanguageRI/ResolveOverloadImpl.js:
+        (resolveOverloadImpl):
+        * WebGPUShadingLanguageRI/Rewriter.js:
+        (Rewriter.prototype.visitDotExpression):
+        (Rewriter.prototype.visitVariableRef):
+        * WebGPUShadingLanguageRI/StructLayoutBuilder.js: Added.
+        (StructLayoutBuilder):
+        (StructLayoutBuilder.prototype.visitStructType):
+        (StructLayoutBuilder.prototype.get offset):
+        (StructLayoutBuilder.prototype.visitField):
+        (StructLayoutBuilder.prototype.visitVariableRef):
+        (StructLayoutBuilder.prototype.visitVariableDecl):
+        * WebGPUShadingLanguageRI/StructType.js:
+        (StructType.prototype.add):
+        (StructType.prototype.instantiate):
+        (StructType.prototype.populateDefaultValue):
+        * WebGPUShadingLanguageRI/Substitution.js:
+        (Substitution.prototype.visitTypeRef):
+        (Substitution.prototype.visitVariableRef):
+        (Substitution):
+        * WebGPUShadingLanguageRI/Test.js:
+        (TEST_genericStructInstance):
+        * WebGPUShadingLanguageRI/Type.js:
+        (Type.prototype.inherits):
+        * WebGPUShadingLanguageRI/TypeRef.js:
+        (TypeRef.instantiate):
+        * WebGPUShadingLanguageRI/VariableRef.js:
+        (VariableRef.wrap):
+        * WebGPUShadingLanguageRI/Visitor.js:
+        (Visitor.prototype.visitTypeRef):
+        (Visitor.prototype.visitDotExpression):
+        (Visitor.prototype.visitVariableRef):
+
+2017-09-02  Filip Pizlo  <[email protected]>
+
         WSL NullType should be a special type variable so it can unify with any pointer type
         https://bugs.webkit.org/show_bug.cgi?id=176235
 

Modified: trunk/Tools/WebGPUShadingLanguageRI/All.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/All.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/All.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -48,6 +48,7 @@
 load("CommaExpression.js");
 load("ConstexprTypeParameter.js");
 load("DereferenceExpression.js");
+load("DotExpression.js");
 load("EArrayRef.js");
 load("EBuffer.js");
 load("EBufferBuilder.js");
@@ -77,6 +78,7 @@
 load("NativeTypeInstance.js");
 load("NullLiteral.js");
 load("NullType.js");
+load("OverloadResolutionFailure.js");
 load("Parse.js");
 load("Prepare.js");
 load("Program.js");
@@ -92,6 +94,7 @@
 load("ReturnChecker.js");
 load("ReturnException.js");
 load("StandardLibrary.js");
+load("StructLayoutBuilder.js");
 load("StructType.js");
 load("Substitution.js");
 load("SuffixCallAssignment.js");

Modified: trunk/Tools/WebGPUShadingLanguageRI/CallFunction.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/CallFunction.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/CallFunction.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -28,11 +28,17 @@
 function callFunction(program, name, typeArguments, argumentList)
 {
     let argumentTypes = argumentList.map(argument => argument.type);
-    let func = resolveInlinedFunction(program, name, typeArguments, argumentTypes);
-    if (!func)
-        throw new WTypeError("<callFunction>", "Cannot resolve function call " + name + "<" + typeArguments + ">(" + argumentList + ")");
-    for (let i = 0; i < func.parameters.length; ++i)
-        func.parameters[i].ePtr.copyFrom(argumentList[i].ePtr, argumentTypes[i].size);
+    let funcOrFailures = resolveInlinedFunction(program, name, typeArguments, argumentTypes);
+    if (!(funcOrFailures instanceof Func)) {
+        let failures = funcOrFailures;
+        throw new WTypeError("<callFunction>", "Cannot resolve function call " + name + "<" + typeArguments + ">(" + argumentList + ")" + (failures.length ? "; tried:\n" + failures.join("\n") : ""));
+    }
+    let func = funcOrFailures;
+    for (let i = 0; i < func.parameters.length; ++i) {
+        let type = argumentTypes[i].instantiatedType;
+        type.visit(new StructLayoutBuilder());
+        func.parameters[i].ePtr.copyFrom(argumentList[i].ePtr, type.size);
+    }
     let result = new Evaluator(program).runBody(func.returnType, func.body);
     return new TypedValue(func.returnType.unifyNode, result);
 }

Modified: trunk/Tools/WebGPUShadingLanguageRI/Checker.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Checker.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Checker.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -75,9 +75,9 @@
     {
         for (let i = 0; i < typeParameters.length; ++i) {
             let argumentIsType = typeArguments[i] instanceof Type;
-            let result = node.typeArguments[i].visit(this);
+            let result = typeArguments[i].visit(this);
             if (argumentIsType) {
-                if (!typeArguments[i].inherits(typeArguments[i]))
+                if (!typeArguments[i].inherits(typeParameters[i].protocol))
                     throw new WTypeError(origin.originString, "Type argument does not inherit protocol");
             } else {
                 if (!result.equalsWithCommit(typeParameters[i].type))
@@ -153,6 +153,29 @@
         return new PtrType(node.origin, node.lValue.addressSpace, elementType);
     }
     
+    visitDotExpression(node)
+    {
+        let structType = node.struct.visit(this).unifyNode;
+        
+        node.structType = structType;
+        
+        let underlyingStruct = structType;
+        
+        if (structType instanceof TypeRef)
+            underlyingStruct = underlyingStruct.type;
+        
+        if (!(underlyingStruct instanceof StructType))
+            throw new WTypeError(node.origin.originString, "Operand to dot _expression_ is not a struct type: " + structType);
+        
+        if (structType instanceof TypeRef) 
+            underlyingStruct = underlyingStruct.instantiate(structType.typeArguments, "shallow");
+        
+        let field = underlyingStruct.fieldByName(node.fieldName);
+        if (!field)
+            throw new WTypeError(node.origin.originString, "Field " + node.fieldName + " not found in " + structType);
+        return field.type;
+    }
+    
     visitVariableRef(node)
     {
         return node.variable.type;
@@ -235,11 +258,8 @@
             if (!overload.func) {
                 failures.push(...overload.failures);
                 let message = "Did not find function for call";
-                if (failures.length) {
-                    let stringifyFailure =
-                        failure => failure.func.toDeclString() + " did not match because: " + failure.reason;
-                    message += ", but considered:\n" + failures.map(stringifyFailure).join("\n")
-                }
+                if (failures.length)
+                    message += ", but considered:\n" + failures.join("\n")
                 throw new WTypeError(node.origin.originString, message);
             }
         }

Added: trunk/Tools/WebGPUShadingLanguageRI/DotExpression.js (0 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/DotExpression.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/DotExpression.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -0,0 +1,43 @@
+/*
+ * 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 DotExpression extends _expression_ {
+    constructor(origin, struct, fieldName)
+    {
+        super(origin);
+        this._struct = struct;
+        this._fieldName = fieldName;
+    }
+    
+    get struct() { return this._struct; }
+    get fieldName() { return this._fieldName; }
+    get isLValue() { return this.struct.isLValue; }
+    
+    toString()
+    {
+        return "(" + this.struct + "." + this.fieldName + ")";
+    }
+}

Modified: trunk/Tools/WebGPUShadingLanguageRI/EBufferBuilder.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/EBufferBuilder.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/EBufferBuilder.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -33,7 +33,6 @@
     
     _createEPtr(type)
     {
-        type = type.instantiatedType;
         let buffer = new EBuffer(type.size);
         if (!type.populateDefaultValue)
             throw new Error("Cannot populateDefaultValue with: " + type);

Modified: trunk/Tools/WebGPUShadingLanguageRI/EPtr.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/EPtr.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/EPtr.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -56,10 +56,22 @@
         return this.buffer.get(this.offset);
     }
     
+    get(offset)
+    {
+        return this.buffer.get(this.offset + offset);
+    }
+    
+    set(offset, value)
+    {
+        this.buffer.set(this.offset + offset, value);
+    }
+    
     copyFrom(other, size)
     {
+        if (size == null)
+            throw new Error("Cannot copy null size");
         for (let i = size; i--;)
-            this.buffer.set(i, other.buffer.get(i));
+            this.set(i, other.get(i));
     }
     
     toString()

Modified: trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Evaluator.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -87,9 +87,10 @@
     
     visitAssignment(node)
     {
-        let result = node.lhs.visit(this);
-        result.copyFrom(node.rhs.visit(this), node.type.size);
-        return result;
+        let target = node.lhs.visit(this);
+        let source = node.rhs.visit(this);
+        target.copyFrom(source, node.type.size);
+        return target;
     }
     
     visitDereferenceExpression(node)
@@ -105,6 +106,12 @@
         return EPtr.box(node.lValue.visit(this));
     }
     
+    visitDotExpression(node)
+    {
+        let structPtr = node.struct.visit(this);
+        return structPtr.plus(node.field.offset);
+    }
+    
     visitCommaExpression(node)
     {
         let result;

Modified: trunk/Tools/WebGPUShadingLanguageRI/Field.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Field.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Field.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -24,9 +24,10 @@
  */
 "use strict";
 
-class Field {
+class Field extends Node {
     constructor(origin, name, type)
     {
+        super();
         this._origin = origin;
         this._name = name;
         this._type = type;

Modified: trunk/Tools/WebGPUShadingLanguageRI/FuncDef.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/FuncDef.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/FuncDef.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -31,6 +31,15 @@
         this.body = body;
     }
     
+    rewrite(rewriter)
+    {
+        if (this._typeParameters.length)
+            throw new Error("Cannot rewrite an uninstantiated function");
+        this._returnType = this._returnType.visit(rewriter);
+        this._parameters = this._parameters.map(parameter => parameter.visit(rewriter));
+        this.body = this.body.visit(rewriter);
+    }
+    
     toString()
     {
         return super.toString() + " " + this.body;

Modified: trunk/Tools/WebGPUShadingLanguageRI/FuncInstantiator.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/FuncInstantiator.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/FuncInstantiator.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -32,12 +32,12 @@
     }
     
     // Returns a Func object that uniquely identifies a particular system of type arguments. You must
-    // intantiate things with concrete types, because this code casually assumes this.
+    // intantiate things with concrete types, because this code casually assumes this. Note that this
+    // will return a different func from `func` no matter what. This ensures that we can use the
+    // returned func for rewrites that instantiate types without destroying our ability to do overload
+    // resolutions on the original Program.
     getUnique(func, typeArguments)
     {
-        if (!func.typeParameters.length)
-            return func;
-        
         let instances = this._instances.get(func);
         if (!instances)
             this._instances.set(func, instances = []);

Modified: trunk/Tools/WebGPUShadingLanguageRI/Inline.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Inline.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Inline.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -28,8 +28,10 @@
 {
     for (let funcList of program.functions.values()) {
         for (let func of funcList) {
-            if (!func.typeParameters.length)
+            if (!func.typeParameters.length) {
+                func = program.funcInstantiator.getUnique(func, [])
                 _inlineFunction(program, func, new VisitingSet(func));
+            }
         }
     }
 }
@@ -41,11 +43,15 @@
     if (func.inlined || func.isNative)
         return;
     
+    func.rewrite(new InstantiateImmediates());
+    
     // This is the precise time when we can build EBuffers in order to get them to be uniqued by
     // type instantiation but nothing else.
+    func.visit(new StructLayoutBuilder());
     func.visit(new EBufferBuilder(program));
     
-    func.body = func.body.visit(new Inliner(program, func, visiting));
+    func.rewrite(new Inliner(program, func, visiting));
+
     func.inlined = true;
 }
 
@@ -52,8 +58,8 @@
 function resolveInlinedFunction(program, name, typeArguments, argumentTypes)
 {
     let overload = program.resolveFuncOverload(name, typeArguments, argumentTypes);
-    if (!overload)
-        return null;
+    if (!overload.func)
+        return overload.failures;
     if (!overload.func.typeParameters)
         return overload.func;
     let func = program.funcInstantiator.getUnique(overload.func, overload.typeArguments);

Modified: trunk/Tools/WebGPUShadingLanguageRI/Inliner.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Inliner.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Inliner.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -32,6 +32,15 @@
         this._visiting = visiting;
     }
     
+    visitDotExpression(node)
+    {
+        let result = super.visitDotExpression(node);
+        result.field = result.structType.unifyNode.fieldByName(result.fieldName);
+        if (result.field.offset == null)
+            throw new Error("Un-laid-out field: " + result.field + " (in " + result.structType + ")");
+        return result;
+    }
+    
     visitCallExpression(node)
     {
         return this._visiting.doVisit(node.func, () => {

Modified: trunk/Tools/WebGPUShadingLanguageRI/InstantiateImmediates.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/InstantiateImmediates.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/InstantiateImmediates.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -28,7 +28,8 @@
     visitTypeRef(node)
     {
         node = super.visitTypeRef(node);
-        return node.instantiatedType;
+        let result = TypeRef.wrap(node.instantiatedType);
+        return result;
     }
     
     visitReferenceType(node)

Modified: trunk/Tools/WebGPUShadingLanguageRI/NameResolver.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/NameResolver.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/NameResolver.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -140,9 +140,9 @@
                 if (!thing)
                     new WTypeError(typeArgument.origin.originString, "Could not find type or variable named " + typeArgument.name);
                 if (thing instanceof Value) {
-                    typeArgument[i] = new VariableRef(typeArgument.origin, typeArgument.name);
+                    typeArguments[i] = new VariableRef(typeArgument.origin, typeArgument.name);
                 } else
-                    typeArgument[i] = new TypeRef(typeArgument.origin, typeArgument.name, []);
+                    typeArguments[i] = new TypeRef(typeArgument.origin, typeArgument.name, []);
             }
             
             if (typeArgument[i] instanceof Value
@@ -163,7 +163,7 @@
         node.type = type;
         
         if (type.typeParameters.length != node.typeArguments.length)
-            throw new WTypeError(node.origin.originString, "Wrong number of type arguments");
+            throw new WTypeError(node.origin.originString, "Wrong number of type arguments (passed " + node.typeArguments.length + ", expected " + type.typeParameters.length + ")");
         for (let i = 0; i < type.typeParameters.length; ++i) {
             let parameterIsType = type.typeParameters[i] instanceof TypeVariable;
             let argumentIsType = node.typeArguments[i] instanceof Type;

Modified: trunk/Tools/WebGPUShadingLanguageRI/Node.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Node.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Node.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -27,6 +27,12 @@
 class Node {
     visit(visitor)
     {
+        // FIXME: We could stash a memo table in the visitor. So, before calling the visitor, we ask
+        // it if they have seen this node before. If they have, then we return what they returned the
+        // last time. This also trivially makes the rewriter do remapping correctly. The downside of
+        // not doing it is mostly that various visitors might check the same types multiple times.
+        // https://bugs.webkit.org/show_bug.cgi?id=176286
+        
         let visitFunc = visitor["visit" + this.constructor.name];
         if (!visitFunc)
             throw new Error("No visit function for " + this.constructor.name + " in " + visitor.constructor.name);

Added: trunk/Tools/WebGPUShadingLanguageRI/OverloadResolutionFailure.js (0 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/OverloadResolutionFailure.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/OverloadResolutionFailure.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -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 OverloadResolutionFailure {
+    constructor(func, reason)
+    {
+        this._func = func;
+        this._reason = reason;
+    }
+    
+    get func() { return this._func; }
+    get reason() { return this._reason; }
+    
+    toString()
+    {
+        return this.func.toDeclString() + " did not match because: " + this.reason;
+    }
+}
+

Modified: trunk/Tools/WebGPUShadingLanguageRI/Parse.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Parse.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Parse.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -376,7 +376,7 @@
             case "->":
                 if (token.text == "->")
                     left = new DereferenceExpression(token, left);
-                left = new DotExpression(token, left, consumeKind("identifier"));
+                left = new DotExpression(token, left, consumeKind("identifier").text);
                 break;
             case "[": {
                 let index = parseExpression();
@@ -672,6 +672,26 @@
         return result;
     }
     
+    function parseField()
+    {
+        let type = parseType();
+        let name = consumeKind("identifier");
+        consume(";");
+        return new Field(name, name.text, type);
+    }
+    
+    function parseStructType()
+    {
+        let origin = consume("struct");
+        let name = consumeKind("identifier").text;
+        let typeParameters = parseTypeParameters();
+        let result = new StructType(origin, name, typeParameters);
+        consume("{");
+        while (!tryConsume("}"))
+            result.add(parseField());
+        return result;
+    }
+    
     for (;;) {
         let token = lexer.peek();
         if (!token)

Modified: trunk/Tools/WebGPUShadingLanguageRI/ResolveOverloadImpl.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/ResolveOverloadImpl.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/ResolveOverloadImpl.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -29,11 +29,11 @@
     let failures = [];
     for (let func of functions) {
         if (typeArguments.length && typeArguments.length != func.typeParameters.length) {
-            failures.push({func, reason: "Wrong number of type arguments (passed " + typeArguments.length + ", require " + func.typeParameters.length + ")"});
+            failures.push(new OverloadResolutionFailure(func, "Wrong number of type arguments (passed " + typeArguments.length + ", require " + func.typeParameters.length + ")"));
             continue;
         }
         if (argumentTypes.length != func.parameters.length) {
-            failures.push({func, reason: "Wrong number of arguments (passed " + argumentTypes.length + ", require " + func.parameters.length + ")"});
+            failures.push(new OverloadResolutionFailure(func, "Wrong number of arguments (passed " + argumentTypes.length + ", require " + func.parameters.length + ")"));
             continue;
         }
         let unificationContext = new UnificationContext(func.typeParameters);
@@ -42,7 +42,7 @@
             let argument = typeArguments[i];
             let parameter = func.typeParameters[i];
             if (!argument.unify(unificationContext, parameter)) {
-                failures.push({func, reason: "Type argument #" + (i + 1) + " for parameter " + func.typeParameters.name + " does not match (passed " + argument + ", require " + parameter + ")"});
+                failures.push(new OverloadResolutionFailure(func, "Type argument #" + (i + 1) + " for parameter " + func.typeParameters.name + " does not match (passed " + argument + ", require " + parameter + ")"));
                 ok = false;
                 break;
             }
@@ -53,7 +53,7 @@
             if (!argumentTypes[i])
                 throw new Error("Null argument type at i = " + i);
             if (!argumentTypes[i].unify(unificationContext, func.parameters[i].type)) {
-                failures.push({func, reason: "Argument #" + (i + 1) + " " + (func.parameters[i].name ? "for parameter " + func.parameters[i].name : "") + " does not match (passed " + argumentTypes[i] + ", require " + func.parameters[i].type + ")"});
+                failures.push(new OverloadResolutionFailure(func, "Argument #" + (i + 1) + " " + (func.parameters[i].name ? "for parameter " + func.parameters[i].name : "") + " does not match (passed " + argumentTypes[i] + ", require " + func.parameters[i].type + ")"));
                 ok = false;
                 break;
             }
@@ -61,7 +61,7 @@
         if (!ok)
             continue;
         if (!unificationContext.verify()) {
-            failures.push({func, reason: "Violates type variable constraints"});
+            failures.push(new OverloadResolutionFailure(func, "Violates type variable constraints"));
             continue;
         }
         let shouldBuildTypeArguments = !typeArguments.length;
@@ -70,7 +70,7 @@
         for (let typeParameter of func.typeParameters) {
             let typeArgument = unificationContext.find(typeParameter);
             if (typeArgument == typeParameter) {
-                failures.push({func, reason: "Type parameter " + typeParameter + " did not get assigned a type"});
+                failures.push(new OverloadResolutionFailure(func, "Type parameter " + typeParameter + " did not get assigned a type"));
                 ok = false;
                 break;
             }

Modified: trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Rewriter.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -61,6 +61,8 @@
 
     // This is almost wrong. We instantiate Func in Substitution in ProtocolDecl. Then, we end up
     // not rewriting type variables. I think that just works because not rewriting them there is OK.
+    // Everywhere else, it's mandatory that we don't rewrite these because we always assume that
+    // type variables are outside the scope of rewriting.
     visitTypeVariable(node) { return node; }
 
     visitProtocolFuncDecl(node)
@@ -169,6 +171,16 @@
         return result;
     }
     
+    visitDotExpression(node)
+    {
+        let result = new DotExpression(node.origin, node.struct.visit(this), node.fieldName);
+        result.structType = node.structType ? node.structType.visit(this) : null;
+        // We don't want to rewrite field, since this is a deep reference going out of the function.
+        // Also, this is set up during inlining, so we're already fully monomorphised.
+        result.field = node.field;
+        return result;
+    }
+    
     visitMakePtrExpression(node)
     {
         return new MakePtrExpression(node.origin, node.lValue.visit(this));
@@ -176,8 +188,9 @@
     
     visitVariableRef(node)
     {
-        node.variable = this._getMapping(node.variable);
-        return node;
+        let result = new VariableRef(node.origin, node.name);
+        result.variable = this._getMapping(node.variable);
+        return result;
     }
     
     visitReturn(node)

Added: trunk/Tools/WebGPUShadingLanguageRI/StructLayoutBuilder.js (0 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/StructLayoutBuilder.js	                        (rev 0)
+++ trunk/Tools/WebGPUShadingLanguageRI/StructLayoutBuilder.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -0,0 +1,58 @@
+/*
+ * 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 StructLayoutBuilder extends Visitor {
+    constructor()
+    {
+        super();
+        this._offset = 0;
+    }
+    
+    visitStructType(node)
+    {
+        if (node.size != null)
+            return;
+        if (node.typeParameters.length)
+            throw new Error("Cannot do layout for generic type: " + node);
+        let oldOffset = this._offset;
+        this._offset = 0;
+        super.visitStructType(node);
+        node.size = this._offset;
+        this._offset += oldOffset;
+    }
+    
+    get offset() { return this._offset; }
+    
+    visitField(node)
+    {
+        node.offset = this._offset;
+        let size = node.type.size;
+        if (size == null)
+            throw new Error("Type does not have size: " + node.type);
+        this._offset += size;
+    }
+}
+

Modified: trunk/Tools/WebGPUShadingLanguageRI/StructType.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/StructType.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/StructType.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -36,6 +36,7 @@
     
     add(field)
     {
+        field.struct = this;
         if (this._fields.has(field.name))
             throw new WTypeError(field.origin.originString, "Duplicate field name: " + field.name);
         this._fields.set(field.name, field);
@@ -56,7 +57,7 @@
         return result;
     }
     
-    instantiate(typeArguments)
+    instantiate(typeArguments, mode)
     {
         if (typeArguments.length != this.typeParameters.length)
             throw new WTypeError(origin.originString, "Wrong number of type arguments to instantiation");
@@ -65,13 +66,25 @@
             return this;
         
         let substitution = new Substitution(this.typeParameters, typeArguments);
-        let instantiateImmediates = new InstantiateImmediates();
+        let instantiateImmediates = mode == "shallow" ? null : new InstantiateImmediates();
         let result = new StructType(this.origin, this.name, []);
-        for (let field of this.fields)
-            result.add(field.visit(substitution).visit(instantiateImmediates));
+        for (let field of this.fields) {
+            let newField = field.visit(substitution);
+            if (instantiateImmediates)
+                newField = newField.visit(instantiateImmediates);
+            result.add(newField);
+        }
         return result;
     }
     
+    populateDefaultValue(buffer, offset)
+    {
+        if (this.size == null)
+            throw new Error("Struct does not have layout: " + this + " " + describe(this));
+        for (let field of this.fields)
+            field.type.populateDefaultValue(buffer, offset + field.offset);
+    }
+    
     toString()
     {
         return "struct " + this.name + "<" + this.typeParameters + "> { " + Array.from(this.fields).join("; ") + "; }";

Modified: trunk/Tools/WebGPUShadingLanguageRI/Substitution.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Substitution.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Substitution.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -41,7 +41,7 @@
         if (replacement) {
             if (node.typeArguments.length)
                 throw new Error("Unexpected type arguments on type variable");
-            return replacement;
+            return TypeRef.wrap(replacement);
         }
         
         return super.visitTypeRef(node);
@@ -51,7 +51,7 @@
     {
         let replacement = this._map.get(node.variable);
         if (replacement)
-            return replacement;
+            return VariableRef.wrap(replacement);
         
         return super.visitVariableRef(node);
     }

Modified: trunk/Tools/WebGPUShadingLanguageRI/Test.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Test.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Test.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -384,6 +384,66 @@
         (e) => e instanceof WTypeError);
 }
 
+function TEST_simpleStruct()
+{
+    let program = doPrep(`
+        struct Foo {
+            int x;
+            int y;
+        }
+        Foo foo(Foo foo)
+        {
+            Foo result;
+            result.x = foo.y;
+            result.y = foo.x;
+            return result;
+        }
+    `);
+    let structType = program.types.get("Foo");
+    if (!structType)
+        throw new Error("Did not find Foo type");
+    let buffer = new EBuffer(2);
+    buffer.set(0, 62);
+    buffer.set(1, 24);
+    let result = callFunction(program, "foo", [], [new TypedValue(structType, new EPtr(buffer, 0))]);
+    if (!result.type.equals(structType))
+        throw new Error("Wrong result type: " + result.type);
+    let x = result.ePtr.get(0);
+    let y = result.ePtr.get(1);
+    if (x != 24)
+        throw new Error("Wrong result for x: " + x + " (y = " + y + ")");
+    if (y != 62)
+        throw new Error("Wrong result for y: " + y + " (x + " + x + ")");
+}
+
+function TEST_genericStructInstance()
+{
+    let program = doPrep(`
+        struct Foo<T> {
+            T x;
+            T y;
+        }
+        Foo<int> foo(Foo<int> foo)
+        {
+            Foo<int> result;
+            result.x = foo.y;
+            result.y = foo.x;
+            return result;
+        }
+    `);
+    let structType = TypeRef.instantiate(program.types.get("Foo"), [program.intrinsics.int32]);
+    let buffer = new EBuffer(2);
+    buffer.set(0, 62);
+    buffer.set(1, 24);
+    let result = callFunction(program, "foo", [], [new TypedValue(structType, new EPtr(buffer, 0))]);
+    let x = result.ePtr.get(0);
+    let y = result.ePtr.get(1);
+    if (x != 24)
+        throw new Error("Wrong result for x: " + x + " (y = " + y + ")");
+    if (y != 62)
+        throw new Error("Wrong result for y: " + y + " (x + " + x + ")");
+}
+
 function TEST_doubleGenericCallsDoubleGeneric()
 {
     doPrep(`

Modified: trunk/Tools/WebGPUShadingLanguageRI/Type.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Type.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Type.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -32,6 +32,8 @@
     
     inherits(protocol)
     {
+        if (!protocol)
+            return true;
         return protocol.hasHeir(this);
     }
     

Modified: trunk/Tools/WebGPUShadingLanguageRI/TypeRef.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/TypeRef.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/TypeRef.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -40,6 +40,13 @@
         result.type = type;
         return result;
     }
+    
+    static instantiate(type, typeArguments)
+    {
+        let result = new TypeRef(type.origin, type.name, typeArguments);
+        result.type = type;
+        return result;
+    }
  
     get origin() { return this._origin; }
     get name() { return this._name; }

Modified: trunk/Tools/WebGPUShadingLanguageRI/VariableRef.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/VariableRef.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/VariableRef.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -32,6 +32,13 @@
         this.variable = null;
     }
     
+    static wrap(variable)
+    {
+        let result = new VariableRef(variable.origin, variable.name);
+        result.variable = variable;
+        return result;
+    }
+    
     get name() { return this._name; }
     get isConstexpr() { return this.variable.isConstexpr; }
     get unifyNode() { return this.variable.unifyNode; }

Modified: trunk/Tools/WebGPUShadingLanguageRI/Visitor.js (221549 => 221550)


--- trunk/Tools/WebGPUShadingLanguageRI/Visitor.js	2017-09-03 17:43:25 UTC (rev 221549)
+++ trunk/Tools/WebGPUShadingLanguageRI/Visitor.js	2017-09-03 17:46:48 UTC (rev 221550)
@@ -97,6 +97,8 @@
     {
         for (let typeArgument of node.typeArguments)
             typeArgument.visit(this);
+        if (node.type)
+            node.type.visit(this);
     }
     
     visitNativeType(node)
@@ -187,6 +189,13 @@
         node.ptr.visit(this);
     }
     
+    visitDotExpression(node)
+    {
+        node.struct.visit(this);
+        if (node.structType)
+            node.structType.visit(this);
+    }
+    
     visitMakePtrExpression(node)
     {
         node.lValue.visit(this);
@@ -194,6 +203,8 @@
     
     visitVariableRef(node)
     {
+        if (node.variable)
+            node.variable.visit(this);
     }
     
     visitReturn(node)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to