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
|
// main.cpp
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <slang.h>
int main(
int argc,
char** argv)
{
// TODO: parse arguments
assert(argc >= 2);
char const* inputPath = argv[1];
// Slurp in the input file, so that we can compile and run it
FILE* inputFile = fopen(inputPath, "rb");
assert(inputFile);
fseek(inputFile, 0, SEEK_END);
size_t inputSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);
char* inputText = (char*) malloc(inputSize + 1);
fread(inputText, inputSize, 1, inputFile);
inputText[inputSize] = 0;
fclose(inputFile);
// TODO: scan through the text to find comments,
// that instruct us how to generate input and
// consume output when running the test.
//
SlangSession* session = spCreateSession(nullptr);
SlangCompileRequest* request = spCreateCompileRequest(session);
spSetCodeGenTarget(
request,
SLANG_IR);
int translationUnitIndex = spAddTranslationUnit(
request,
SLANG_SOURCE_LANGUAGE_SLANG,
nullptr);
spAddTranslationUnitSourceString(
request,
translationUnitIndex,
inputPath,
inputText);
int entryPointIndex = spAddEntryPoint(
request,
translationUnitIndex,
"main",
spFindProfile(session, "cs_5_0"));
if( spCompile(request) != 0 )
{
char const* output = spGetDiagnosticOutput(request);
fputs(output, stderr);
exit(1);
}
// Things compiled, so now we need to run them...
// Extract the bytecode
size_t bytecodeSize = 0;
void const* bytecode = spGetEntryPointCode(request, entryPointIndex, &bytecodeSize);
// Now we need to create an execution context to go and run the bytecode we got
SlangVM* vm = SlangVM_create();
SlangVMModule* vmModule = SlangVMModule_load(
vm,
bytecode,
bytecodeSize);
SlangVMFunc* vmFunc = (SlangVMFunc*)SlangVMModule_findGlobalSymbolPtr(
vmModule,
"main");
int32_t*& inputArg = *(int32_t**)SlangVMModule_findGlobalSymbolPtr(
vmModule,
"input");
int32_t*& outputArg = *(int32_t**)SlangVMModule_findGlobalSymbolPtr(
vmModule,
"output");
SlangVMThread* vmThread = SlangVMThread_create(
vm);
int32_t inputData[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
int32_t outputData[8] = { 0 };
inputArg = inputData;
outputArg = outputData;
// TODO: set arguments based on specification from the user...
for (uint32_t threadID = 0; threadID < 8; ++threadID)
{
#if 0
fprintf(stderr, "\n\nthreadID = %u\n\n", threadID);
fflush(stderr);
#endif
SlangVMThread_beginCall(vmThread, vmFunc);
SlangVMThread_setArg(
vmThread,
0,
&threadID,
sizeof(threadID));
SlangVMThread_resume(vmThread);
}
for (uint32_t ii = 0; ii < 8; ++ii)
{
fprintf(stdout, "outputData[%u] = %d\n", ii, outputData[ii]);
}
spDestroyCompileRequest(request);
spDestroySession(session);
return 0;
}
|