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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
// capabilities-generator-main.cpp
#include <stdio.h>
#include "../../source/compiler-core/slang-lexer.h"
#include "../../source/compiler-core/slang-perfect-hash-codegen.h"
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-secure-crt.h"
#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-file-system.h"
using namespace Slang;
namespace Diagnostics
{
#define DIAGNOSTIC(id, severity, name, messageFormat) const DiagnosticInfo name = { id, Severity::severity, #name, messageFormat };
#include "slang-capability-diagnostic-defs.h"
#undef DIAGNOSTIC
}
enum class CapabilityFlavor
{
Normal,
Abstract,
Alias
};
struct CapabilityDef;
struct CapabilityConjunctionExpr
{
List<CapabilityDef*> atoms;
};
struct CapabilityDisjunctionExpr
{
List<CapabilityConjunctionExpr> conjunctions;
};
struct SerializedArrayView
{
Index first;
Index count;
};
struct CapabilityDef : public RefObject
{
Index enumValue;
String name;
CapabilityDisjunctionExpr expr;
CapabilityFlavor flavor;
int rank;
List<List<CapabilityDef*>> canonicalRepresentation;
SerializedArrayView serializedCanonicalRepresentation;
CapabilityDef* getAbstractBase()
{
if (flavor != CapabilityFlavor::Normal)
return nullptr;
if (expr.conjunctions.getCount() != 1)
return nullptr;
if (expr.conjunctions[0].atoms.getCount() == 0)
return nullptr;
if (expr.conjunctions[0].atoms[0]->flavor != CapabilityFlavor::Abstract)
return nullptr;
return expr.conjunctions[0].atoms[0];
}
};
struct CapabilityDefParser
{
CapabilityDefParser(Lexer* lexer, DiagnosticSink* sink)
: m_lexer(lexer)
, m_sink(sink)
{
}
Lexer* m_lexer;
DiagnosticSink* m_sink;
Dictionary<String, CapabilityDef*> m_mapNameToCapability;
List<RefPtr<CapabilityDef>> m_defs;
TokenReader m_tokenReader;
bool advanceIf(TokenType type)
{
if (m_tokenReader.peekTokenType() == type)
{
m_tokenReader.advanceToken();
return true;
}
return false;
}
SlangResult readToken(TokenType type, Token& nextToken)
{
nextToken = m_tokenReader.advanceToken();
if (nextToken.type != type)
{
m_sink->diagnose(nextToken.loc, Diagnostics::unexpectedTokenExpectedTokenType, nextToken, type);
return SLANG_FAIL;
}
return SLANG_OK;
}
SlangResult readToken(TokenType type)
{
Token nextToken;
return readToken(type, nextToken);
}
SlangResult parseConjunction(CapabilityConjunctionExpr& expr)
{
for (;;)
{
Token nameToken;
SLANG_RETURN_ON_FAIL(readToken(TokenType::Identifier, nameToken));
CapabilityDef* def = nullptr;
if (m_mapNameToCapability.tryGetValue(nameToken.getContent(), def))
{
expr.atoms.add(def);
}
else
{
m_sink->diagnose(nameToken.loc, Diagnostics::undefinedIdentifier, nameToken);
return SLANG_FAIL;
}
if (!(advanceIf(TokenType::OpAnd) || advanceIf(TokenType::OpAdd)))
break;
}
return SLANG_OK;
}
SlangResult parseExpr(CapabilityDisjunctionExpr& expr)
{
for (;;)
{
CapabilityConjunctionExpr conjunction;
SLANG_RETURN_ON_FAIL(parseConjunction(conjunction));
expr.conjunctions.add(conjunction);
if (!advanceIf(TokenType::OpBitOr))
break;
}
return SLANG_OK;
}
SlangResult parseDefs()
{
auto tokens = m_lexer->lexAllSemanticTokens();
m_tokenReader = TokenReader(tokens);
for (;;)
{
RefPtr<CapabilityDef> def = new CapabilityDef();
def->flavor = CapabilityFlavor::Normal;
auto nextToken = m_tokenReader.advanceToken();
if (nextToken.getContent() == "alias")
{
def->flavor = CapabilityFlavor::Alias;
}
else if (nextToken.getContent() == "abstract")
{
def->flavor = CapabilityFlavor::Abstract;
}
else if (nextToken.getContent() == "def")
{
def->flavor = CapabilityFlavor::Normal;
}
else if (nextToken.type == TokenType::EndOfFile)
{
break;
}
else
{
m_sink->diagnose(nextToken.loc, Diagnostics::unexpectedToken, nextToken);
return SLANG_FAIL;
}
Token nameToken;
SLANG_RETURN_ON_FAIL(readToken(TokenType::Identifier, nameToken));
def->name = nameToken.getContent();
if (def->flavor == CapabilityFlavor::Normal)
{
if (advanceIf(TokenType::Colon))
{
SLANG_RETURN_ON_FAIL(parseExpr(def->expr));
}
if (advanceIf(TokenType::OpAssign))
{
Token rankToken;
SLANG_RETURN_ON_FAIL(readToken(TokenType::IntegerLiteral, rankToken));
def->rank = stringToInt(rankToken.getContent());
}
}
else if (def->flavor == CapabilityFlavor::Alias)
{
SLANG_RETURN_ON_FAIL(readToken(TokenType::OpAssign));
SLANG_RETURN_ON_FAIL(parseExpr(def->expr));
}
else if (def->flavor == CapabilityFlavor::Abstract)
{
if (advanceIf(TokenType::Colon))
{
SLANG_RETURN_ON_FAIL(parseExpr(def->expr));
}
}
SLANG_RETURN_ON_FAIL(readToken(TokenType::Semicolon));
m_defs.add(def);
if (!m_mapNameToCapability.addIfNotExists(def->name, def))
{
m_sink->diagnose(nextToken.loc, Diagnostics::redefinition, def->name);
return SLANG_FAIL;
}
}
return SLANG_OK;
}
};
struct CapabilityConjunction
{
HashSet<CapabilityDef*> atoms;
bool implies(const CapabilityConjunction& c) const
{
for (auto& atom : c.atoms)
{
if (!atoms.contains(atom))
return false;
}
return true;
}
bool isImpossible() const
{
// Keep a map from an abstract base to the concrete atom defined in this conjunction that implements the base.
Dictionary<CapabilityDef*, CapabilityDef*> abstractKV;
for (auto& atom : atoms)
{
auto abstractBase = atom->getAbstractBase();
if (!abstractBase)
continue;
// Have we already seen another concrete atom that implements the same abstract base of the current atom?
// If so, we have a conflict and the conjunction is impossible.
//
CapabilityDef* value = nullptr;
if (abstractKV.tryGetValue(abstractBase, value))
{
if (value != atom)
return true;
}
else
{
abstractKV[abstractBase] = atom;
}
}
return false;
}
};
struct CapabilityDisjunction
{
List<CapabilityConjunction> conjunctions;
void addConjunction(const CapabilityConjunction& c)
{
if (c.isImpossible())
return;
for (auto& conjunction : conjunctions)
{
if (c.implies(conjunction))
return;
}
for (Index i = 0; i < conjunctions.getCount();)
{
if (conjunctions[i].implies(c))
{
conjunctions.fastRemoveAt(i);
}
else
{
i++;
}
}
conjunctions.add(_Move(c));
}
CapabilityDisjunction joinWith(const CapabilityDisjunction& other)
{
if (conjunctions.getCount() == 0)
{
return other;
}
if (other.conjunctions.getCount() == 0)
{
return *this;
}
CapabilityDisjunction result;
for (auto& thisC : conjunctions)
{
for (auto& thatC : other.conjunctions)
{
CapabilityConjunction newC;
for (auto atom : thisC.atoms)
newC.atoms.add(atom);
for (auto atom : thatC.atoms)
newC.atoms.add(atom);
result.addConjunction(_Move(newC));
}
}
return result;
}
List<List<CapabilityDef*>> canonicalize()
{
List<List<CapabilityDef*>> result;
for (auto& c : conjunctions)
{
List<CapabilityDef*> atoms;
for (auto& atom : c.atoms)
atoms.add(atom);
atoms.sort([](CapabilityDef* c1, CapabilityDef* c2) {return c1->enumValue < c2->enumValue; });
result.add(_Move(atoms));
}
result.sort([](const List<CapabilityDef*>& c1, const List<CapabilityDef*>& c2)
{
for (Index i = 0; i < Math::Min(c1.getCount(), c2.getCount()); i++)
{
if (c1[i]->enumValue < c2[i]->enumValue)
return true;
else if (c1[i]->enumValue > c2[i]->enumValue)
return false;
}
return c1.getCount() < c2.getCount();
});
return result;
}
};
CapabilityDisjunction getCanonicalRepresentation(CapabilityDef* def)
{
CapabilityDisjunction result;
for (auto& c : def->canonicalRepresentation)
{
CapabilityConjunction conj;
for (auto& atom : c)
conj.atoms.add(atom);
result.conjunctions.add(conj);
}
return result;
}
CapabilityDisjunction evaluateConjunction(const List<CapabilityDef*>& atoms)
{
CapabilityDisjunction result;
for (auto& def : atoms)
{
CapabilityDisjunction defCanonical = getCanonicalRepresentation(def);
result = result.joinWith(defCanonical);
}
return result;
}
void calcCanonicalRepresentation(CapabilityDef* def, const List<CapabilityDef*>& mapEnumValueToDef)
{
CapabilityDisjunction disjunction;
if (def->flavor == CapabilityFlavor::Normal)
{
CapabilityConjunction c;
c.atoms.add(def);
disjunction.conjunctions.add(c);
}
CapabilityDisjunction exprVal;
for (auto& c : def->expr.conjunctions)
{
CapabilityDisjunction evalD = evaluateConjunction(c.atoms);
for (auto& cc : evalD.conjunctions)
exprVal.addConjunction(cc);
}
disjunction = disjunction.joinWith(exprVal);
def->canonicalRepresentation = disjunction.canonicalize();
}
void calcCanonicalRepresentations(const List<RefPtr<CapabilityDef>>& defs, const List<CapabilityDef*>& mapEnumValueToDef)
{
for (auto def : defs)
calcCanonicalRepresentation(def, mapEnumValueToDef);
}
SlangResult generateDefinitions(const List<RefPtr<CapabilityDef>>& defs, StringBuilder& sbHeader, StringBuilder& sbCpp)
{
sbHeader << "enum class CapabilityAtom\n{\n";
sbHeader << " Invalid,\n";
for (auto def : defs)
{
if (def->flavor == CapabilityFlavor::Normal)
{
sbHeader << " " << def->name << ",\n";
}
}
sbHeader << " Count\n";
sbHeader << "};\n";
CapabilityDef* firstAbstractDef = nullptr;
CapabilityDef* firstAliasDef = nullptr;
sbHeader << "enum class CapabilityName\n{\n";
sbHeader << " Invalid,\n";
Index enumValueCounter = 1;
List<CapabilityDef*> mapEnumValueToDef;
mapEnumValueToDef.add(nullptr); // For Invalid.
for (auto def : defs)
{
if (def->flavor == CapabilityFlavor::Normal)
{
def->enumValue = enumValueCounter;
++enumValueCounter;
mapEnumValueToDef.add(def);
sbHeader << " " << def->name << " = (int)CapabilityAtom::" << def->name << ",\n";
}
}
for (auto def : defs)
{
if (def->flavor == CapabilityFlavor::Abstract)
{
if (firstAbstractDef == nullptr)
firstAbstractDef = def;
def->enumValue = enumValueCounter;
++enumValueCounter;
mapEnumValueToDef.add(def);
sbHeader << " " << def->name << ",\n";
}
}
for (auto def : defs)
{
if (def->flavor == CapabilityFlavor::Alias)
{
if (firstAliasDef == nullptr)
firstAliasDef = def;
def->enumValue = enumValueCounter;
++enumValueCounter;
mapEnumValueToDef.add(def);
sbHeader << " " << def->name << ",\n";
}
}
sbHeader << " Count\n";
sbHeader << "};\n";
calcCanonicalRepresentations(defs, mapEnumValueToDef);
List<String> capabiltiyNameArray;
List<SerializedArrayView> serializedCapabilityArrays;
List<SerializedArrayView> serializedAtomDisjunctions;
auto serializeConjunction = [&](const List<CapabilityDef*>& capabilities) -> SerializedArrayView
{
// Do we already have a serialized capability array that is the same the one we are trying to serialize?
for (auto existingArray : serializedCapabilityArrays)
{
if (existingArray.count == capabilities.getCount())
{
bool match = true;
for (Index i = 0; i < capabilities.getCount(); i++)
{
if (capabiltiyNameArray[existingArray.first+i] != capabilities[i]->name)
{
match = false;
break;
}
}
if (match)
return existingArray;
}
}
SerializedArrayView result;
result.first = capabiltiyNameArray.getCount();
for (auto capability : capabilities)
{
capabiltiyNameArray.add(capability->name);
}
result.count = capabilities.getCount();
serializedCapabilityArrays.add(result);
return result;
};
auto serializeDisjunction = [&](const List<SerializedArrayView>& conjunctions) -> SerializedArrayView
{
SerializedArrayView result;
result.first = serializedAtomDisjunctions.getCount();
for (auto c : conjunctions)
{
serializedAtomDisjunctions.add(c);
}
result.count = conjunctions.getCount();
return result;
};
for (auto& def : defs)
{
List<SerializedArrayView> conjunctions;
for (auto& c : def->canonicalRepresentation)
conjunctions.add(serializeConjunction(c));
def->serializedCanonicalRepresentation = serializeDisjunction(conjunctions);
}
sbCpp << "static CapabilityName kCapabilityArray[] = {\n";
Index arrayIndex = 0;
sbCpp << " /* [0] @0: */ ";
for (Index i = 0; i < capabiltiyNameArray.getCount(); ++i)
{
sbCpp << " CapabilityName::" << capabiltiyNameArray[i] << ",";
if (i + 1 == serializedCapabilityArrays[arrayIndex].first + serializedCapabilityArrays[arrayIndex].count)
{
arrayIndex++;
if (arrayIndex == serializedCapabilityArrays.getCount())
sbCpp << "\n";
else
sbCpp << "\n /* [" << arrayIndex << "] @" << serializedCapabilityArrays[arrayIndex].first <<": */ ";
}
}
sbCpp << "};\n";
sbCpp << "static ArrayView<CapabilityName> kCapabilityConjunctions[] = {\n";
for (auto c : serializedAtomDisjunctions)
{
sbCpp << " { kCapabilityArray + " << c.first << ", " << c.count << " },\n";
}
sbCpp << "};\n";
sbCpp << "static const CapabilityAtomInfo kCapabilityNameInfos[int(CapabilityName::Count)] = {\n";
for (auto def : mapEnumValueToDef)
{
if (!def)
{
sbCpp << R"( { "Invalid", CapabilityNameFlavor::Concrete, CapabilityName::Invalid, 0, {nullptr, 0} },)" << "\n";
continue;
}
// name.
sbCpp << " { \"" << def->name << "\", ";
// flavor.
switch (def->flavor)
{
case CapabilityFlavor::Normal:
sbCpp << "CapabilityNameFlavor::Concrete";
break;
case CapabilityFlavor::Abstract:
sbCpp << "CapabilityNameFlavor::Abstract";
break;
case CapabilityFlavor::Alias:
sbCpp << "CapabilityNameFlavor::Alias";
break;
}
sbCpp << ", ";
// abstract base.
auto abstractBase = def->getAbstractBase();
if (abstractBase)
{
sbCpp << "CapabilityName::" << abstractBase->name;
}
else
{
sbCpp << "CapabilityName::Invalid";
}
sbCpp << ", ";
// canonnical representation.
sbCpp << def->rank << ", { kCapabilityConjunctions + " << def->serializedCanonicalRepresentation.first << ", " << def->serializedCanonicalRepresentation.count << "} },\n";
}
sbCpp << "};\n";
return SLANG_OK;
}
SlangResult parseDefFile(DiagnosticSink* sink, String inputPath, List<RefPtr<CapabilityDef>>& outDefs)
{
auto sourceManager = sink->getSourceManager();
String contents;
SLANG_RETURN_ON_FAIL(File::readAllText(inputPath, contents));
PathInfo pathInfo = PathInfo::makeFromString(inputPath);
SourceFile* sourceFile = sourceManager->createSourceFileWithString(pathInfo, contents);
SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());
Lexer lexer;
NamePool namePool;
RootNamePool rootPool;
namePool.setRootNamePool(&rootPool);
lexer.initialize(sourceView, sink, &namePool, sourceManager->getMemoryArena());
CapabilityDefParser parser(&lexer, sink);
SLANG_RETURN_ON_FAIL(parser.parseDefs());
outDefs = _Move(parser.m_defs);
return SLANG_OK;
}
void printDiagnostics(DiagnosticSink* sink)
{
ComPtr<ISlangBlob> blob;
sink->getBlobIfNeeded(blob.writeRef());
if (blob)
{
fprintf(stderr, "%s", (const char*)blob->getBufferPointer());
}
}
void writeIfChanged(String fileName, String content)
{
if (File::exists(fileName))
{
String existingContent;
File::readAllText(fileName, existingContent);
if (existingContent.getUnownedSlice().trim() == content.getUnownedSlice().trim())
return;
}
File::writeAllText(fileName, content);
}
int main(int argc, const char* const* argv)
{
if (argc < 2)
{
fprintf(
stderr,
"Usage: %s\n",
argc >= 1 ? argv[0] : "slang-capabilities-generator");
return 1;
}
String targetDir;
for (int i = 0; i < argc - 1; i++)
{
if (strcmp(argv[i], "--target-directory") == 0)
targetDir = argv[i + 1];
}
String inPath = argv[1];
if (targetDir.getLength() == 0)
targetDir = Path::getParentDirectory(inPath);
auto outCppPath = Path::combine(targetDir, "slang-generated-capability-defs-impl.h");
auto outHeaderPath = Path::combine(targetDir, "slang-generated-capability-defs.h");
auto outLookupPath = Path::combine(targetDir, "slang-lookup-capability-defs.cpp");
SourceManager sourceManager;
sourceManager.initialize(nullptr, OSFileSystem::getExtSingleton());
DiagnosticSink sink(&sourceManager, nullptr);
List<RefPtr<CapabilityDef>> defs;
if (SLANG_FAILED(parseDefFile(&sink, inPath, defs)))
{
printDiagnostics(&sink);
return 1;
}
StringBuilder sbHeader, sbCpp;
if (SLANG_FAILED(generateDefinitions(defs, sbHeader, sbCpp)))
{
return 1;
}
writeIfChanged(outHeaderPath, sbHeader.produceString());
writeIfChanged(outCppPath, sbCpp.produceString());
List<String> opnames;
for (auto def : defs)
{
opnames.add(def->name);
}
if (SLANG_FAILED(writePerfectHashLookupCppFile(outLookupPath, opnames, "CapabilityName", "CapabilityName::", "slang-capability.h", &sink)))
{
printDiagnostics(&sink);
return 1;
}
return 0;
}
|