summaryrefslogtreecommitdiffstats
path: root/source/slang/lower-to-ir.cpp
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2017-11-20 13:45:10 -0800
committerGitHub <noreply@github.com>2017-11-20 13:45:10 -0800
commit37315c96ea48045fae60f0e1cb1a3293f3ddd962 (patch)
treea96f139a7a2dae57f67956711be14bf773a50ec2 /source/slang/lower-to-ir.cpp
parent3dff5a53a21ab2404918e772962004767024c0f3 (diff)
IR: support global variable with initializers (#294)
The big change here is that the ability to contain basic blocks with instructions in them has been hoisted from `IRFunc` into a new base type `IRGlobalValueWithCode` shared with `IRGlobalVar`. The basic blocks of a global variable define initialization logic for it; they can be looked at like a function that returns the initial value. Places in the IR that used to assume functions contain all the code need to be updated, but so far I only handled the cloning step. The emit logic currently handles an initializer for a global variable by outputting its logic as a separate function, and then having the variable call that function to initialize itself. This should be cleaned up over time so that we generate an ordinary expression whenever possible. I also made the emit logic correctly label any global variable without a layout (that is, any that don't represent a shader parameter) as `static` so that the downstream HLSL compiler sees them as variables rather than parameters.
Diffstat (limited to 'source/slang/lower-to-ir.cpp')
-rw-r--r--source/slang/lower-to-ir.cpp18
1 files changed, 17 insertions, 1 deletions
diff --git a/source/slang/lower-to-ir.cpp b/source/slang/lower-to-ir.cpp
index 9e58d908b..c01059c61 100644
--- a/source/slang/lower-to-ir.cpp
+++ b/source/slang/lower-to-ir.cpp
@@ -2693,7 +2693,23 @@ struct DeclLoweringVisitor : DeclVisitor<DeclLoweringVisitor, LoweredValInfo>
if( auto initExpr = decl->initExpr )
{
- // TODO: need to handle global with initializer!
+ IRBuilder subBuilderStorage = *getBuilder();
+ IRBuilder* subBuilder = &subBuilderStorage;
+
+ subBuilder->curFunc = irGlobal;
+
+ IRGenContext subContextStorage = *context;
+ IRGenContext* subContext = &subContextStorage;
+
+ subContext->irBuilder = subBuilder;
+
+ // TODO: set up a parent IR decl to put the instructions into
+
+ IRBlock* entryBlock = subBuilder->emitBlock();
+ subBuilder->curBlock = entryBlock;
+
+ LoweredValInfo initVal = lowerLValueExpr(subContext, initExpr);
+ subContext->irBuilder->emitReturn(getSimpleVal(subContext, initVal));
}
return globalVal;