From d81c347e0edcbbf181885baf2b13978c28dfc9a8 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 12 Jun 2017 12:26:12 -0700 Subject: First pass at support for cross-compilation This is a large change that contains many pieces: - Update the `cross-compile0` test to actually make use of cross compilation. Now the `cross-compile0.hlsl` file contains both HLSL and GLSL source code, and then imports code from `cross-compile0.slang`, which provides a "library" (one function) that can be shared between both the HLSL and GLSL version of things. - Fixed a bug in the support for backslash-escaped newlines. - Added a new `__import` declaration type (replaces the `using` directive that was still around in a vestigial form) An `__import` causes the compiler to look for a Slang source file (currently using the ordinary `#include` lookup logic), and then parse/check the found file as an additional module ("translation unit"), before making its declarations visible in the current scope. - Refactored the main compilation flow to be simpler. There were the `ShaderCompiler` and `ShaderCompilerImpl` classes that weren't relaly doing anything, but added complexity to the whole workflow. - The `render-test` application has been heavily modified to better support testing cross-compilation workflows. At the most basic level we are starting to distinguish pass-through vs. rewriter workflows, and are passing various `#define`s down to the compiler(s) to let the source code be customized as needed for each case. Several annoying corner cases are caused here by having to support the GLSL compilation model, which really wants each entry point in its own specific translation unit, whereas we really want to keep things nicely contained in single files. - Added support for `__intrinsic` operations to have target-specific behavior. This allows a function to be given a different name for some specific target (so a call gets emitted as a call to that other operation). More generally, the library writer can put together an arbitrary format string that will be used in place of expressions that call the given function, e.g.: __intrinsic(hlsl, "$1 - $0") __intrinsic int foo(int a, int b); Given this declaration, a call like `foo(x,y)` will code generate as `x - y` for HLSL, and as `foo(x,y)` for all other targets. Annoying things still to be dealt with: - The way that I'm filtering the user-provided options when passing things down to the compilation of dynamically loaded modules is a bit ad hoc. It would be good to have a systematic notion of which options will be inherited and which won't. There is also more code duplication than I'd like, so we risk having the compiler behave differently when compiling a file at the top level, vs. because of `__import`. - Adding target-specific behavior to intrinsics is all well and good, but the current approach means we can only add this to the original declaration, which limits the ability to easily extend the set of targets. A better approach long-term would be to add a more robust notion of target-based overload resolution (which would happen after semantic checking). Then one mechanism would be used to find the right target-specific overload to use for an operation, and then each (target-specific) definition could use a simpler attribute to intercept code-generation behavior. Note that we might eventually need a similar notion to deal with stage- or profile-specific functions and the overloading behavior around them, so using this for intrinsics doesn't seem like a bad idea. --- source/slang/syntax.h | 67 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'source/slang/syntax.h') diff --git a/source/slang/syntax.h b/source/slang/syntax.h index 9e4486d4e..ac2bcac87 100644 --- a/source/slang/syntax.h +++ b/source/slang/syntax.h @@ -72,7 +72,16 @@ namespace Slang IntrinsicOp findIntrinsicOp(char const* name); - class IntrinsicModifier : public Modifier + // Base class for modifiers that mark something as "intrinsic" + // and thus lacking a direct implementation in the language. + class IntrinsicModifierBase : public Modifier + { + }; + + // A modifier that marks something as one of a small set of + // truly intrinsic operations that the compiler knows about + // directly. + class IntrinsicOpModifier : public IntrinsicModifierBase { public: // token that names the intrinsic op @@ -82,6 +91,20 @@ namespace Slang IntrinsicOp op = IntrinsicOp::Unknown; }; + // A modifier that marks something as an intrinsic function, + // for some subset of targets. + class TargetIntrinsicModifier : public IntrinsicModifierBase + { + public: + // Token that names the target that the operation + // is an intrisic for. + Token targetToken; + + // A custom definition for the operation + Token definitionToken; + }; + + class InOutModifier : public OutModifier {}; @@ -2041,22 +2064,12 @@ namespace Slang virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; - class UsingFileDecl : public Decl - { - public: - Token fileName; - - virtual RefPtr Accept(SyntaxVisitor * visitor) override; - }; - + // A "module" of code (essentiately, a single translation unit) + // that provides a scope for some number of declarations. class ProgramSyntaxNode : public ContainerDecl { public: // Access members of specific types - FilteredMemberList GetUsings() - { - return GetMembersOfType(); - } FilteredMemberList GetFunctions() { return GetMembersOfType(); @@ -2074,15 +2087,26 @@ namespace Slang { return GetMembersOfType(); } -#if 0 - void Include(ProgramSyntaxNode * other) - { - Members.AddRange(other->Members); - } -#endif + virtual RefPtr Accept(SyntaxVisitor * visitor) override; }; + class ImportDecl : public Decl + { + public: + // The name of the module we are trying to import + Token nameToken; + + // The scope that we want to import into + RefPtr scope; + + // The module that actually got imported + RefPtr importedModuleDecl; + + virtual RefPtr Accept(SyntaxVisitor * visitor) override; + }; + + class IfStatementSyntaxNode : public StatementSyntaxNode { public: @@ -2492,10 +2516,7 @@ namespace Slang return program; } - virtual RefPtr VisitUsingFileDecl(UsingFileDecl * decl) - { - return decl; - } + virtual void visitImportDecl(ImportDecl * decl) = 0; virtual RefPtr VisitFunction(FunctionSyntaxNode* func) { -- cgit v1.2.3