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
|
// slang-emit-cpp.cpp
#include "slang-emit-cpp.h"
#include "../core/slang-writer.h"
#include "slang-emit-source-writer.h"
#include "slang-mangled-lexer.h"
#include <assert.h>
namespace Slang {
static IROp _getCType(IROp op)
{
switch (op)
{
case kIROp_VoidType:
case kIROp_BoolType:
{
return op;
}
case kIROp_Int8Type:
case kIROp_Int16Type:
case kIROp_IntType:
case kIROp_UInt8Type:
case kIROp_UInt16Type:
case kIROp_UIntType:
{
// Promote all these to Int
return kIROp_IntType;
}
case kIROp_Int64Type:
case kIROp_UInt64Type:
{
// Promote all these to Int16, we can just vary the call to make these work
return kIROp_Int64Type;
}
case kIROp_DoubleType:
{
return kIROp_DoubleType;
}
case kIROp_HalfType:
case kIROp_FloatType:
{
// Promote both to float
return kIROp_FloatType;
}
default:
{
SLANG_ASSERT(!"Unhandled type");
return kIROp_undefined;
}
}
}
static UnownedStringSlice _getCTypeVecPostFix(IROp op)
{
switch (op)
{
case kIROp_BoolType: return UnownedStringSlice::fromLiteral("B");
case kIROp_IntType: return UnownedStringSlice::fromLiteral("I");
case kIROp_FloatType: return UnownedStringSlice::fromLiteral("F");
case kIROp_Int64Type: return UnownedStringSlice::fromLiteral("I64");
case kIROp_DoubleType: return UnownedStringSlice::fromLiteral("F64");
default: return UnownedStringSlice::fromLiteral("?");
}
}
#if 0
static UnownedStringSlice _getCTypeName(IROp op)
{
switch (op)
{
case kIROp_BoolType: return UnownedStringSlice::fromLiteral("Bool");
case kIROp_IntType: return UnownedStringSlice::fromLiteral("I32");
case kIROp_FloatType: return UnownedStringSlice::fromLiteral("F32");
case kIROp_Int64Type: return UnownedStringSlice::fromLiteral("I64");
case kIROp_DoubleType: return UnownedStringSlice::fromLiteral("F64");
default: return UnownedStringSlice::fromLiteral("?");
}
}
#endif
void CPPSourceEmitter::_emitCVecType(IROp op, Int size)
{
m_writer->emit("Vec");
const UnownedStringSlice postFix = _getCTypeVecPostFix(_getCType(op));
m_writer->emit(postFix);
if (postFix.size() > 1)
{
m_writer->emit("_");
}
m_writer->emit(size);
}
void CPPSourceEmitter::_emitCMatType(IROp op, IRIntegerValue rowCount, IRIntegerValue colCount)
{
m_writer->emit("Mat");
const UnownedStringSlice postFix = _getCTypeVecPostFix(_getCType(op));
m_writer->emit(postFix);
if (postFix.size() > 1)
{
m_writer->emit("_");
}
m_writer->emit(rowCount);
m_writer->emit(colCount);
}
void CPPSourceEmitter::_emitCFunc(BuiltInCOp cop, IRType* type)
{
emitSimpleType(type);
m_writer->emit("_");
switch (cop)
{
case BuiltInCOp::Init: m_writer->emit("init");
case BuiltInCOp::Splat: m_writer->emit("splat"); break;
}
}
void CPPSourceEmitter::emitParameterGroupImpl(IRGlobalParam* varDecl, IRUniformParameterGroupType* type)
{
SLANG_UNUSED(varDecl);
SLANG_UNUSED(type);
SLANG_ASSERT(!"Not implemented");
}
void CPPSourceEmitter::emitEntryPointAttributesImpl(IRFunc* irFunc, EntryPointLayout* entryPointLayout)
{
SLANG_UNUSED(irFunc);
SLANG_UNUSED(entryPointLayout);
SLANG_ASSERT(!"Not implemented");
}
void CPPSourceEmitter::emitVectorTypeNameImpl(IRType* elementType, IRIntegerValue elementCount)
{
_emitCVecType(elementType->op, Int(elementCount));
}
void CPPSourceEmitter::emitSimpleTypeImpl(IRType* type)
{
switch (type->op)
{
case kIROp_VoidType:
case kIROp_BoolType:
case kIROp_Int8Type:
case kIROp_Int16Type:
case kIROp_IntType:
case kIROp_Int64Type:
case kIROp_UInt8Type:
case kIROp_UInt16Type:
case kIROp_UIntType:
case kIROp_UInt64Type:
case kIROp_FloatType:
case kIROp_DoubleType:
{
m_writer->emit(getDefaultBuiltinTypeName(type->op));
return;
}
case kIROp_HalfType:
{
m_writer->emit("float");
return;
}
case kIROp_StructType:
m_writer->emit(getName(type));
return;
case kIROp_VectorType:
{
auto vecType = (IRVectorType*)type;
emitVectorTypeNameImpl(vecType->getElementType(), GetIntVal(vecType->getElementCount()));
return;
}
case kIROp_MatrixType:
{
auto matType = (IRMatrixType*)type;
const auto rowCount = GetIntVal(matType->getRowCount());
const auto colCount = GetIntVal(matType->getColumnCount());
_emitCMatType(matType->getElementType()->op, rowCount, colCount);
return;
}
}
SLANG_DIAGNOSE_UNEXPECTED(getSink(), SourceLoc(), "unhandled type for cpp target");
}
bool CPPSourceEmitter::tryEmitInstExprImpl(IRInst* inst, IREmitMode mode, const EmitOpInfo& inOuterPrec)
{
SLANG_UNUSED(inOuterPrec);
//EmitOpInfo outerPrec = inOuterPrec;
//bool needClose = false;
switch (inst->op)
{
case kIROp_Construct:
case kIROp_makeVector:
case kIROp_MakeMatrix:
// Simple constructor call
if (inst->getOperandCount() == 1)
{
_emitCFunc(BuiltInCOp::Splat, inst->getDataType());
emitArgs(inst, mode);
}
else
{
_emitCFunc(BuiltInCOp::Init, inst->getDataType());
emitArgs(inst, mode);
}
return true;
default:
return false;
}
}
} // namespace Slang
|