blob: e073763cf4cb791f3ca73b8c162e28804d90dfc5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// bytecode.h
#ifndef SLANG_BYTECODE_H_INCLUDED
#define SLANG_BYTECODE_H_INCLUDED
// This file defines a "bytecode" format for storing shader code
// that has been generated via the Slang IR. The bytecode has
// two main goals, that can end up in a bit of conflict:
//
// 1) It is the official serialized form of the Slang IR, and
// so it is of some importance that constructs in the IR be
// able to round-trip through the bytecode.
//
// 2) It should support being directly executed/interpreted,
// so that Slang code can be executed on CPUs when
// performance isn't critical (or when a JIT just isn't
// an option).
//
#include "../core/basic.h"
namespace Slang
{
template<typename T>
struct BytecodeGenerationPtr;
// A "pointer" stored in a serialized bytecode file, which
// is represented as a byte offset relative to itself.
//
template<typename T>
struct BCPtr
{
typedef int32_t RawVal;
RawVal rawVal;
BCPtr() : rawVal(0) {}
BCPtr(T* ptr)
: rawVal(0)
{
*this = ptr;
}
BCPtr(BCPtr<T> const& ptr)
: rawVal(0)
{
*this = ptr.getPtr();
}
void operator=(BCPtr<T> const& ptr)
{
*this = ptr.getPtr();
}
void operator=(T* ptr)
{
if (ptr)
{
rawVal = (char*)ptr - (char*)this;
}
else
{
rawVal = 0;
}
}
operator T*() const { return getPtr(); }
T* getPtr() const
{
if(!rawVal) return nullptr;
return (T*)((char const*)this + rawVal);
}
};
struct BCType
{
uint32_t op;
};
struct BCSymbol
{
// The opcode that was used to define
// this symbol; used to categorize things
uint32_t op;
// The type of the symbol is represent
// as an index into the global-scope symbol
// list of the module.
//
// Note: This currently precludes having
// a register with a type that is not
// statically determined, but that is
// probably okay.
uint32_t typeGlobalID;
// The name of this symbol (which might
// be a mangled name at some point,
// so it is really only meant to be
// used for linkage...)
BCPtr<char> name;
};
typedef uint8_t BCOp;
struct BCReg : BCSymbol
{
// The index of the variable/register
// that should be stored immediately
// preceding this one.
uint32_t previousVarIndexPlusOne;
};
struct BCConst
{
// The ID of the symbol in the global
// scope that we are trying to refer
// to.
//
// TODO: eventually, if we have general
// nesting, then this might be the
// entry in the outer scope that
// is being referenced.
uint32_t globalID;
};
struct BCBlock
{
// The start of the bytecode for this block
BCPtr<BCOp> code;
// The list of parameters of the block
uint32_t paramCount;
BCPtr<BCReg> params;
};
struct BCFunc : BCSymbol
{
// A list of "registers" used to hold
// intermediate values during execution
// of this function.
uint32_t regCount;
BCPtr<BCReg> regs;
// The basic blocks of the function
uint32_t blockCount;
BCPtr<BCBlock> blocks;
// A list of "constants" which are values
// from the global scope that this function
// wants to be able to refer to. We could
// just encode global values directly,
// but this would make the encoding less dense.
uint32_t constCount;
BCPtr<BCConst> consts;
// Data for "nested" symbols (e.g., a function
// nested inside this function).
uint32_t nestedSymbolCount;
BCPtr<BCPtr<BCSymbol>> nestedSymbols;
};
// A module is encoded more or less like a function.
struct BCModule : BCFunc
{
};
struct BCHeader
{
char magic[8];
uint32_t version;
// TODO: probably want a section-based file
// format so that we can add/remove different
// kinds of data without having to revise
// the schema here.
// The bytecode representation of the module
BCPtr<BCModule> module;
};
}
#endif
|