FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modelscript/packages/language/src/codegen/dataflow.ts at main · modelscript/modelscript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
modelscript
/
modelscript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
12
Code
Issues
1
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
modelscript
/
packages
/
language
/
src
/
codegen
/
dataflow.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
368 lines (320 loc) · 12.7 KB
Breadcrumbs
modelscript
/
packages
/
language
/
src
/
codegen
/
dataflow.ts
Copy path
File metadata and controls
368 lines (320 loc) · 12.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
{
LanguageOptions
}
from
"../dsl.js"
;
import
{
BLOCK_FALSE_BRANCH
,
BLOCK_FIRST_INSTR
,
BLOCK_NEXT
,
BLOCK_STATE_FALSE
,
BLOCK_STATE_IN
,
BLOCK_STATE_OUT
,
BLOCK_STATE_TRUE
,
BLOCK_TRUE_BRANCH
,
IR_INSTR_NEXT
,
}
from
"./ir_layout.js"
;
import
{
transpileQuery
}
from
"./transpiler.js"
;
/**
* Transpiles a DSL callback function body into AssemblyScript source code using the TypeScript Compiler API.
*/
function
transpileCallback
(
fn
:
(
...
args
:
any
[
]
)
=>
any
,
paramNames
:
string
[
]
,
fallbackExpr
:
string
)
:
string
{
try
{
const
res
=
transpileQuery
(
fn
,
"dataflow"
)
;
if
(
res
&&
res
.
body
)
{
let
body
=
res
.
body
.
trim
(
)
;
if
(
body
.
startsWith
(
"return "
)
&&
body
.
endsWith
(
";"
)
)
{
body
=
body
.
slice
(
7
,
-
1
)
.
trim
(
)
;
}
if
(
res
.
params
&&
res
.
params
.
length
>
0
)
{
for
(
let
i
=
0
;
i
<
Math
.
min
(
res
.
params
.
length
,
paramNames
.
length
)
;
i
++
)
{
const
original
=
res
.
params
[
i
]
;
const
target
=
paramNames
[
i
]
;
if
(
original
&&
original
!==
target
)
{
const
regex
=
new
RegExp
(
`\\b
${
original
}
\\b`
,
"g"
)
;
body
=
body
.
replace
(
regex
,
target
)
;
}
}
}
return
body
;
}
return
fallbackExpr
;
}
catch
{
return
fallbackExpr
;
}
}
export
function
generateDataflow
(
grammarDef
:
LanguageOptions
<
any
>
)
:
string
{
if
(
!
grammarDef
.
analysis
)
return
"// Dataflow Analysis Disabled\n"
;
let
out
=
`
import { S, getNodePadding, getNodeByteLength, allocGen0, getNodeFlags, FLAG_IS_SYNTHETIC } from "./arena";
import { allocDiagnostic } from "./graph";
import { globalAstRoot, lsp_findNodeOffset } from "./lsp";
import { firstBlock } from "./cfg";
import {
BLOCK_STATE_IN,
BLOCK_STATE_OUT,
BLOCK_TRUE_BRANCH,
BLOCK_FALSE_BRANCH,
BLOCK_NEXT,
BLOCK_FIRST_INSTR,
IR_INSTR_NEXT,
} from "./ir_layout";
// --- Auto-Generated Dataflow Analysis Engine ---
const DATAFLOW_MAX_ITERATIONS: u32 = 1000;
export function dataflowError(nodeId: u32, code: u32): void {
if (nodeId == 0 || (getNodeFlags(nodeId) & FLAG_IS_SYNTHETIC) != 0) return;
let absStart = lsp_findNodeOffset(globalAstRoot, nodeId);
let startByte: u32 = absStart >= 0 ? (absStart as u32) : getNodePadding(nodeId);
let endByte = startByte + getNodeByteLength(nodeId);
allocDiagnostic(startByte, endByte, code, 0);
}
`
;
let
latticeMap
:
Record
<
string
,
number
>
=
{
Bottom
:
0
,
Top
:
100
}
;
let
latticeCounter
=
1
;
for
(
const
[
analysisName
,
config
]
of
Object
.
entries
(
grammarDef
.
analysis
)
)
{
if
(
config
.
lattice
)
{
for
(
const
val
of
config
.
lattice
)
{
if
(
!
(
val
in
latticeMap
)
)
latticeMap
[
val
]
=
latticeCounter
++
;
out
+=
`export const LATTICE_
${
analysisName
.
toUpperCase
(
)
}
_
${
val
.
toUpperCase
(
)
}
: u32 =
${
latticeMap
[
val
]
}
;\n`
;
}
}
let
isBackward
=
(
config
as
any
)
.
direction
===
"backward"
;
const
joinFn
=
(
config
as
any
)
.
join
;
let
joinBody
:
string
;
if
(
joinFn
&&
typeof
joinFn
===
"function"
)
{
joinBody
=
transpileCallback
(
joinFn
,
[
"state1"
,
"state2"
]
,
"state1 > state2 ? state1 : state2"
)
;
}
else
{
joinBody
=
"state1 > state2 ? state1 : state2"
;
}
out
+=
`
export function dataflowJoin_
${
analysisName
}
(state1: u32, state2: u32): u32 {
if (state1 == state2) return state1;
return
${
joinBody
}
;
}
`
;
const
transferFn
=
(
config
as
any
)
.
transfer
;
if
(
transferFn
&&
typeof
transferFn
===
"function"
)
{
const
transferBody
=
transpileCallback
(
transferFn
,
[
"nodeId"
,
"stateIn"
]
,
"stateIn"
)
;
out
+=
`
export function dataflowTransfer_
${
analysisName
}
(blockPtr: u32, stateIn: u32): u32 {
let stateOut = stateIn;
let currInstr = load<u32>(blockPtr +
${
BLOCK_FIRST_INSTR
}
, 0);
while (currInstr != 0) {
let nodeId = load<u32>(currInstr + 4, 0);
stateOut =
${
transferBody
}
;
currInstr = load<u32>(currInstr +
${
IR_INSTR_NEXT
}
, 0);
}
return stateOut;
}
`
;
}
else
{
const
topLattice
=
latticeCounter
>
0
?
latticeCounter
-
1
:
1
;
out
+=
`
export function dataflowTransfer_
${
analysisName
}
(blockPtr: u32, stateIn: u32): u32 {
let stateOut = stateIn;
let currInstr = load<u32>(blockPtr +
${
BLOCK_FIRST_INSTR
}
, 0);
while (currInstr != 0) {
let opcode = load<u16>(currInstr, 0);
if (opcode >= 16) {
stateOut =
${
topLattice
}
;
}
currInstr = load<u32>(currInstr +
${
IR_INSTR_NEXT
}
, 0);
}
return stateOut;
}
`
;
}
out
+=
`
let rpo_
${
analysisName
}
_buf: u32 = 0;
let rpo_
${
analysisName
}
_count: u32 = 0;
function computeRPO_
${
analysisName
}
(): void {
let numBlocks: u32 = 0;
for (let ptr = firstBlock; ptr != 0; ptr = load<u32>(ptr +
${
BLOCK_NEXT
}
, 0)) {
numBlocks++;
}
if (numBlocks == 0) return;
let visitedOffset = allocGen0(numBlocks * 4);
let postOrderOffset = allocGen0(numBlocks * 4);
let blockIndexMap = allocGen0(numBlocks * 8);
let idx: u32 = 0;
for (let ptr = firstBlock; ptr != 0; ptr = load<u32>(ptr +
${
BLOCK_NEXT
}
, 0)) {
store<u32>(blockIndexMap + idx * 8, ptr);
store<u32>(blockIndexMap + idx * 8 + 4, idx);
store<u32>(visitedOffset + idx * 4, 0);
idx++;
}
let postIdx: u32 = 0;
let stackOffset = allocGen0(numBlocks * 8);
let stackTop: u32 = 0;
store<u32>(stackOffset, firstBlock);
store<u32>(stackOffset + 4, 0);
stackTop = 1;
while (stackTop > 0) {
stackTop--;
let blk = load<u32>(stackOffset + stackTop * 8);
let phase = load<u32>(stackOffset + stackTop * 8 + 4);
let blkIdx: u32 = 0xFFFFFFFF;
for (let i: u32 = 0; i < numBlocks; i++) {
if (load<u32>(blockIndexMap + i * 8) == blk) {
blkIdx = i;
break;
}
}
if (blkIdx == 0xFFFFFFFF) continue;
if (phase == 1) {
store<u32>(postOrderOffset + postIdx * 4, blk);
postIdx++;
continue;
}
if (load<u32>(visitedOffset + blkIdx * 4) != 0) continue;
store<u32>(visitedOffset + blkIdx * 4, 1);
store<u32>(stackOffset + stackTop * 8, blk);
store<u32>(stackOffset + stackTop * 8 + 4, 1);
stackTop++;
let fBranch = load<u32>(blk +
${
BLOCK_FALSE_BRANCH
}
, 0);
if (fBranch != 0) {
let fIdx: u32 = 0xFFFFFFFF;
for (let i: u32 = 0; i < numBlocks; i++) {
if (load<u32>(blockIndexMap + i * 8) == fBranch) { fIdx = i; break; }
}
if (fIdx != 0xFFFFFFFF && load<u32>(visitedOffset + fIdx * 4) == 0) {
store<u32>(stackOffset + stackTop * 8, fBranch);
store<u32>(stackOffset + stackTop * 8 + 4, 0);
stackTop++;
}
}
let tBranch = load<u32>(blk +
${
BLOCK_TRUE_BRANCH
}
, 0);
if (tBranch != 0) {
let tIdx: u32 = 0xFFFFFFFF;
for (let i: u32 = 0; i < numBlocks; i++) {
if (load<u32>(blockIndexMap + i * 8) == tBranch) { tIdx = i; break; }
}
if (tIdx != 0xFFFFFFFF && load<u32>(visitedOffset + tIdx * 4) == 0) {
store<u32>(stackOffset + stackTop * 8, tBranch);
store<u32>(stackOffset + stackTop * 8 + 4, 0);
stackTop++;
}
}
}
rpo_
${
analysisName
}
_count = postIdx;
rpo_
${
analysisName
}
_buf = allocGen0(postIdx * 4);
for (let i: u32 = 0; i < postIdx; i++) {
store<u32>(rpo_
${
analysisName
}
_buf + i * 4, load<u32>(postOrderOffset + (postIdx - 1 - i) * 4));
}
}
`
;
if
(
isBackward
)
{
out
+=
`
export function solveDataflow_
${
analysisName
}
(): void {
if (firstBlock == 0) return;
computeRPO_
${
analysisName
}
();
if (rpo_
${
analysisName
}
_count == 0) return;
let changed = true;
let iter: u32 = 0;
while (changed && iter < DATAFLOW_MAX_ITERATIONS) {
iter++;
changed = false;
for (let ri: u32 = 0; ri < rpo_
${
analysisName
}
_count; ri++) {
let rIdx = rpo_
${
analysisName
}
_count - 1 - ri;
let ptr = load<u32>(rpo_
${
analysisName
}
_buf + rIdx * 4);
let sIn = load<u32>(ptr +
${
BLOCK_STATE_IN
}
, 0);
let sOut = load<u32>(ptr +
${
BLOCK_STATE_OUT
}
, 0);
let tBranch = load<u32>(ptr +
${
BLOCK_TRUE_BRANCH
}
, 0);
let fBranch = load<u32>(ptr +
${
BLOCK_FALSE_BRANCH
}
, 0);
let joinedOut = sOut;
if (tBranch != 0) joinedOut = dataflowJoin_
${
analysisName
}
(joinedOut, load<u32>(tBranch +
${
BLOCK_STATE_IN
}
, 0));
if (fBranch != 0) joinedOut = dataflowJoin_
${
analysisName
}
(joinedOut, load<u32>(fBranch +
${
BLOCK_STATE_IN
}
, 0));
store<u32>(ptr +
${
BLOCK_STATE_OUT
}
, joinedOut, 0);
let newIn = dataflowTransfer_
${
analysisName
}
(ptr, joinedOut);
if (newIn != sIn) {
store<u32>(ptr +
${
BLOCK_STATE_IN
}
, newIn, 0);
changed = true;
}
}
}
}
`
;
}
else
{
out
+=
`
export function solveDataflow_
${
analysisName
}
(): void {
if (firstBlock == 0) return;
computeRPO_
${
analysisName
}
();
if (rpo_
${
analysisName
}
_count == 0) return;
let numBlocks = rpo_
${
analysisName
}
_count;
let worklistBuf = allocGen0((numBlocks + 1) * 4);
let maxPtr: u32 = 0;
for (let i: u32 = 0; i < numBlocks; i++) {
let blk = load<u32>(rpo_
${
analysisName
}
_buf + i * 4);
if (blk > maxPtr) maxPtr = blk;
}
let maxBitIdx = maxPtr >> 2;
let inWorklistBitset = allocGen0(((maxBitIdx + 32) >> 5) * 4);
let head: u32 = 0;
let tail: u32 = 0;
// Seed worklist with blocks in RPO order
for (let i: u32 = 0; i < numBlocks; i++) {
let blk = load<u32>(rpo_
${
analysisName
}
_buf + i * 4);
store<u32>(worklistBuf + tail * 4, blk);
tail++;
let bitIdx = blk >> 2;
let wordIdx = bitIdx >> 5;
let bitOffset = bitIdx & 31;
let wordVal = load<u32>(inWorklistBitset + wordIdx * 4);
store<u32>(inWorklistBitset + wordIdx * 4, wordVal | (1 << bitOffset));
}
let iter: u32 = 0;
while (head != tail && iter < DATAFLOW_MAX_ITERATIONS) {
iter++;
let ptr = load<u32>(worklistBuf + head * 4);
head = (head + 1) % (numBlocks + 1);
let popBitIdx = ptr >> 2;
let popWordIdx = popBitIdx >> 5;
let popBitOffset = popBitIdx & 31;
let popWordVal = load<u32>(inWorklistBitset + popWordIdx * 4);
store<u32>(inWorklistBitset + popWordIdx * 4, popWordVal & ~(1 << popBitOffset));
let sIn = load<u32>(ptr +
${
BLOCK_STATE_IN
}
, 0);
let sOut = load<u32>(ptr +
${
BLOCK_STATE_OUT
}
, 0);
let newOut = dataflowTransfer_
${
analysisName
}
(ptr, sIn);
if (newOut != sOut) {
store<u32>(ptr +
${
BLOCK_STATE_OUT
}
, newOut, 0);
// Path-sensitive edge branching
store<u32>(ptr +
${
BLOCK_STATE_TRUE
}
, newOut, 0);
store<u32>(ptr +
${
BLOCK_STATE_FALSE
}
, newOut, 0);
let tBranch = load<u32>(ptr +
${
BLOCK_TRUE_BRANCH
}
, 0);
let fBranch = load<u32>(ptr +
${
BLOCK_FALSE_BRANCH
}
, 0);
if (tBranch != 0) {
let oldIn = load<u32>(tBranch +
${
BLOCK_STATE_IN
}
, 0);
let nextIn = dataflowJoin_
${
analysisName
}
(oldIn, newOut);
if (nextIn != oldIn) {
store<u32>(tBranch +
${
BLOCK_STATE_IN
}
, nextIn, 0);
let tBitIdx = tBranch >> 2;
let tWordIdx = tBitIdx >> 5;
let tBitOffset = tBitIdx & 31;
let tWordVal = load<u32>(inWorklistBitset + tWordIdx * 4);
if ((tWordVal & (1 << tBitOffset)) == 0) {
store<u32>(inWorklistBitset + tWordIdx * 4, tWordVal | (1 << tBitOffset));
store<u32>(worklistBuf + tail * 4, tBranch);
tail = (tail + 1) % (numBlocks + 1);
}
}
}
if (fBranch != 0) {
let oldIn = load<u32>(fBranch +
${
BLOCK_STATE_IN
}
, 0);
let nextIn = dataflowJoin_
${
analysisName
}
(oldIn, newOut);
if (nextIn != oldIn) {
store<u32>(fBranch +
${
BLOCK_STATE_IN
}
, nextIn, 0);
let fBitIdx = fBranch >> 2;
let fWordIdx = fBitIdx >> 5;
let fBitOffset = fBitIdx & 31;
let fWordVal = load<u32>(inWorklistBitset + fWordIdx * 4);
if ((fWordVal & (1 << fBitOffset)) == 0) {
store<u32>(inWorklistBitset + fWordIdx * 4, fWordVal | (1 << fBitOffset));
store<u32>(worklistBuf + tail * 4, fBranch);
tail = (tail + 1) % (numBlocks + 1);
}
}
}
}
}
}
`
;
}
}
return
out
;
}
Back
|
FazBrowse Home
|
New Git URL