From 831896f844453ba09c9e6cbfe7d29f6d44282632 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 7 Aug 2017 16:06:58 -0700 Subject: Starting to add intermediate representation (IR) Right now none of this is hooked up, but I want to get things checked in incrementally rather than have along long-lived branches. - Added placeholder declarations for IR representation of instructions, basic blocks, etc. - Start adding a `lower-to-ir` pass to translate from AST representation to IR Again: none of this is functional, so it shouldn't mess with existing users of the compiler. --- source/slang/ir.h | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 source/slang/ir.h (limited to 'source/slang/ir.h') diff --git a/source/slang/ir.h b/source/slang/ir.h new file mode 100644 index 000000000..db46235b3 --- /dev/null +++ b/source/slang/ir.h @@ -0,0 +1,131 @@ +// ir.h +#ifndef SLANG_IR_H_INCLUDED +#define SLANG_IR_H_INCLUDED + +// This file defines the intermediate representation (IR) used for Slang +// shader code. This is a typed static single assignment (SSA) IR, +// similar in spirit to LLVM (but much simpler). +// + +namespace Slang { + +struct IRBlock; +struct IRFunc; +struct IRModule; +struct IRType; + +// A value that can be referenced in the program. +struct IRValue +{ + // Type type of this value + IRType* type; +}; + +// Representation of a type at the IR level. +// Such a type may not correspond to the high-level-language notion +// of a type as used by the front end. +// +// Note that types are values in the IR, so that operations +// may take type operands as easily as values. +struct IRType : IRValue +{ +}; + +// An instruction in the program. +struct IRInst : IRValue +{ + // The basic block that contains this instruction, + // or NULL if the instruction currently has no parent. + IRBlock* parentBlock; + + // The next and previous instructions in the same parent block + IRInst* nextInst; + IRInst* prevInst; +}; + +// A instruction that ends a basic block (usually because of control flow) +struct IRTerminatorInst : IRInst +{}; + +// A basic block, consisting of a sequence of instructions that can only +// be entered at the top, and can only be exited at the last instruction. +// +// Note that a block is itself a value, so that it can be a direct operand +// of an instruction (e.g., an instruction that branches to the block) +struct IRBlock : IRValue +{ + // The function that contains this block + IRFunc* parentFunc; + + // The first and last instruction in the block (or NULL in + // the case that the block is empty). + // + // Note that in a valid program, every block must end with + // a "terminator" instruction, so these should be non-NULL, + // and `last` should actually be an `IRTerminatorInst`. + IRInst* first; + IRInst* last; + + // Next and previous block in the same function + IRBlock* nextBlock; + IRBlock* prevBlock; +}; + +// A function parameter. +struct IRParam : IRValue +{ + // The function that declared this parameter + IRFunc* parentFunc; + + // The next and previous parameter of the function + IRParam* nextParam; + IRParam* prevParam; + +}; + +// A function, which consists of zero or more blocks of instructions. +// +// A function is itself a value, so that it can be a direct operand of +// an instruction (e.g., a call). +struct IRFunc : IRValue +{ + // The IR module that defines this function + IRModule* parentModule; + + // The unique entry block for the function is always the + // first block in the list of blocks. + IRBlock* firstBlock; + + // The last block in the function. + IRBlock* lastBlock; + + // The parameters of the function + IRParam* firstParam; + IRParam* lastParam; + + // The next/previous function in the same IR module + IRFunc* nextFunc; + IRFunc* prevFunc; +}; + +// A module defining global values +struct IRModule +{ +}; + +typedef long long IRIntegerValue; +typedef double IRFloatingPointValue; + + +struct IRBuilder +{ + IRType* getBoolType(); + + IRValue* getBoolValue(bool value); + IRValue* getIntValue(IRType* type, IRIntegerValue value); + IRValue* getFloatValue(IRType* type, IRFloatingPointValue value); +}; + +} + +#endif -- cgit v1.2.3