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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
// 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 = (RawVal)((char*)ptr - (char*)this);
}
else
{
rawVal = 0;
}
}
operator T*() const { return getPtr(); }
T* operator->() const { return getPtr(); }
T* getPtr() const
{
if(!rawVal) return nullptr;
return (T*)((char const*)this + rawVal);
}
};
// Representation of a "type-level" value in
// the bytecode fiel. This corresponds to
// the AST-level notion of a `Val`
struct BCVal
{
// The opcode used to define this value
uint32_t op;
// The ID of the type within its module
uint32_t id;
};
struct BCType : BCVal
{
// TODO: avoid having to encode this?
uint32_t argCount;
// type-specific operands follow
//
BCPtr<BCVal>* getArgs() { return (BCPtr<BCVal>*) (this +1); }
BCVal* getArg(UInt index) { return getArgs()[index]; }
};
struct BCPtrType : BCType
{
BCPtr<BCType> valueType;
};
struct BCFuncType : BCType
{
BCPtr<BCType> resultType;
BCPtr<BCType> paramTypes[1];
BCType* getResultType() { return resultType; }
UInt getParamCount() { return argCount - 1; }
BCType* getParamType(UInt index) { return paramTypes[index]; }
};
struct BCConstant : BCVal
{
uint32_t typeID;
BCPtr<uint8_t> ptr;
};
struct BCSymbol
{
// The opcode that was used to define
// this symbol; used to categorize things
uint32_t op;
// The index (in the module's type table)
// of the type of the symbol:
uint32_t typeID;
// 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;
};
enum BCConstFlavor
{
kBCConstFlavor_GlobalSymbol,
kBCConstFlavor_Constant,
};
struct BCConst
{
// The flavor of bytecode constant we
// are dealing with.
uint32_t flavor;
uint32_t id;
};
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;
};
struct BCModule
{
// The symbols (functions, global variables, etc.)
// that have been declared in the module.
uint32_t symbolCount;
BCPtr<BCPtr<BCSymbol>> symbols;
// The types that are used by this module, stored
// in a single array so that they can be conveniently
// mapped to another representation in one go.
//
// Instructions in a bytecode instruction sequence
// might reference these types by index.
uint32_t typeCount;
BCPtr<BCPtr<BCType>> types;
// True compile-time constants go here:
uint32_t constantCount;
BCPtr<BCConstant> constants;
};
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.
// TODO: should include AST declaration structure
// here, which can be used for refleciton, and
// also loaded to resolve dependencies when
// compiling other modules.
// TODO: Include the original entry point requests?
// Zero or more IR modules, corresponding to
// the translation units of the original compile
// request.
uint32_t moduleCount;
BCPtr<BCPtr<BCModule>> modules;
// TODO: should enumerate targets here, and
// include reflection layout info + compiled
// entry points for each target.
};
class CompileRequest;
void generateBytecodeForCompileRequest(
CompileRequest* compileReq);
}
#endif
|