Skip to content

Commit 6dfc15e

Browse files
authored
refactor: lighten cfg stack burden (#2201)
1 parent 55e549d commit 6dfc15e

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

src/control-flow/extract-cfg.ts

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,10 @@ function cfgIfThenElse(ifNode: RNodeWithParent, condition: ControlFlowInformatio
190190
graph.addEdge(entryPoint, ifId, { label: CfgEdgeType.Fd });
191191
}
192192

193-
for(const exit of [...then.exitPoints, ...otherwise?.exitPoints ?? []]) {
194-
graph.addEdge(ifId + '-exit', exit, { label: CfgEdgeType.Fd });
193+
for(const exits of [then.exitPoints, otherwise?.exitPoints ?? []]) {
194+
for(const exit of exits) {
195+
graph.addEdge(ifId + '-exit', exit, { label: CfgEdgeType.Fd });
196+
}
195197
}
196198
if(!otherwise) {
197199
for(const e of condition.exitPoints) {
@@ -219,8 +221,10 @@ function cfgRepeat(repeat: RRepeatLoop<ParentInformation>, body: ControlFlowInfo
219221
}
220222

221223
// loops automatically
222-
for(const next of [...body.nexts, ...body.exitPoints]) {
223-
graph.addEdge(repeat.info.id, next, { label: CfgEdgeType.Fd });
224+
for(const nexts of [body.nexts, body.exitPoints]) {
225+
for(const next of nexts) {
226+
graph.addEdge(repeat.info.id, next, { label: CfgEdgeType.Fd });
227+
}
224228
}
225229

226230
for(const breakPoint of body.breaks) {
@@ -248,8 +252,10 @@ function cfgWhile(whileLoop: RWhileLoop<ParentInformation>, condition: ControlFl
248252
}
249253
}
250254

251-
for(const next of [...body.nexts, ...body.exitPoints]) {
252-
graph.addEdge(whileId, next, { label: CfgEdgeType.Fd });
255+
for(const nexts of [body.nexts, body.exitPoints]) {
256+
for(const next of nexts) {
257+
graph.addEdge(whileId, next, { label: CfgEdgeType.Fd });
258+
}
253259
}
254260

255261
for(const breakPoint of body.breaks) {
@@ -292,8 +298,10 @@ function cfgFor(forLoop: RForLoop<ParentInformation>, variable: ControlFlowInfor
292298
}
293299
}
294300

295-
for(const next of [...body.nexts, ...body.exitPoints]) {
296-
graph.addEdge(forLoopId, next, { label: CfgEdgeType.Fd });
301+
for(const points of [body.nexts, body.exitPoints]) {
302+
for(const next of points) {
303+
graph.addEdge(forLoopId, next, { label: CfgEdgeType.Fd });
304+
}
297305
}
298306

299307
for(const breakPoint of body.breaks) {
@@ -324,11 +332,15 @@ function cfgFunctionDefinition(fn: RFunctionDefinition<ParentInformation>, param
324332
graph.addVertex({ id: fnId, children, type: identifyMayStatementType(fn), mid: paramExits, end: [fnId + '-exit'] });
325333

326334
graph.mergeWith(body.graph, true);
327-
children.push(...body.graph.rootIds());
335+
for(const r of body.graph.rootIds()) {
336+
children.push(r);
337+
}
328338

329339
for(const param of params) {
330340
graph.mergeWith(param.graph, true);
331-
children.push(...param.graph.rootIds());
341+
for(const r of param.graph.rootIds()) {
342+
children.push(r);
343+
}
332344
for(const entry of param.entryPoints) {
333345
graph.addEdge(entry, fnId, { label: CfgEdgeType.Fd });
334346
}
@@ -354,7 +366,14 @@ function cfgFunctionDefinition(fn: RFunctionDefinition<ParentInformation>, param
354366
function cfgFunctionCall(call: RFunctionCall<ParentInformation>, name: ControlFlowInformation, args: (ControlFlowInformation | typeof EmptyArgument)[], exit = 'exit'): ControlFlowInformation {
355367
const callId = call.info.id;
356368
const graph = name.graph;
357-
const info = { graph, breaks: [...name.breaks], nexts: [...name.nexts], returns: [...name.returns], exitPoints: [callId + '-' + exit], entryPoints: [callId] };
369+
const info = {
370+
graph,
371+
breaks: Array.from(name.breaks),
372+
nexts: Array.from(name.nexts),
373+
returns: Array.from(name.returns),
374+
exitPoints: [callId + '-' + exit],
375+
entryPoints: [callId]
376+
};
358377

359378
graph.addVertex({ id: callId, type: identifyMayStatementType(call), mid: name.exitPoints, end: [callId + '-' + exit] });
360379

@@ -420,8 +439,10 @@ function cfgFunctionCallWithDataflow(graph: DataflowGraph): typeof cfgFunctionCa
420439
root: call.info.id
421440
});
422441

423-
for(const exit of [...baseCfg.exitPoints, ...exits]) {
424-
baseCfg.graph.addEdge(call.info.id + ResolvedCallSuffix, exit, { label: CfgEdgeType.Fd });
442+
for(const col of [baseCfg.exitPoints, exits]) {
443+
for(const exit of col) {
444+
baseCfg.graph.addEdge(call.info.id + ResolvedCallSuffix, exit, { label: CfgEdgeType.Fd });
445+
}
425446
}
426447

427448
return {
@@ -452,7 +473,6 @@ function cfgArgumentOrParameter(node: RNodeWithParent, name: ControlFlowInformat
452473
}
453474
}
454475

455-
456476
if(value) {
457477
graph.mergeWith(value.graph);
458478
info.breaks = info.breaks.concat(value.breaks);
@@ -473,12 +493,11 @@ function cfgArgumentOrParameter(node: RNodeWithParent, name: ControlFlowInformat
473493
}
474494

475495
return info;
476-
477496
}
478497

479498
function cfgBinaryOp(binOp: RBinaryOp<ParentInformation> | RPipe<ParentInformation>, lhs: ControlFlowInformation, rhs: ControlFlowInformation): ControlFlowInformation {
480499
const graph = new ControlFlowGraph().mergeWith(lhs.graph).mergeWith(rhs.graph);
481-
const result: ControlFlowInformation = { graph, breaks: [...lhs.breaks, ...rhs.breaks], nexts: [...lhs.nexts, ...rhs.nexts], returns: [...lhs.returns, ...rhs.returns], entryPoints: [binOp.info.id], exitPoints: [binOp.info.id + '-exit'] };
500+
const result: ControlFlowInformation = { graph, breaks: lhs.breaks.concat(rhs.breaks), nexts: lhs.nexts.concat(rhs.nexts), returns: lhs.returns.concat(rhs.returns), entryPoints: [binOp.info.id], exitPoints: [binOp.info.id + '-exit'] };
482501

483502
graph.addVertex({ id: binOp.info.id, type: binOp.flavor === 'assignment' ? CfgVertexType.Statement : CfgVertexType.Expression, end: [binOp.info.id + '-exit'] });
484503
graph.addVertex({ id: binOp.info.id + '-exit', type: CfgVertexType.EndMarker, root: binOp.info.id });

src/dataflow/internal/process/functions/call/built-in/built-in-assignment.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,20 @@ function extractSourceAndTarget<OtherInfo>(args: readonly RFunctionArgument<Othe
296296
* Promotes the ingoing/unknown references of target (an assignment) to definitions
297297
*/
298298
function produceWrittenNodes<OtherInfo>(rootId: NodeId, target: DataflowInformation, referenceType: InGraphReferenceType, data: DataflowProcessorInformation<OtherInfo>, makeMaybe: boolean, value: NodeId[] | undefined): (InGraphIdentifierDefinition & { name: string })[] {
299-
return target.in.concat(target.unknownReferences).map(ref => ({
300-
...ref,
301-
type: referenceType,
302-
definedAt: rootId,
303-
controlDependencies: data.controlDependencies ?? (makeMaybe ? [] : undefined),
304-
value
305-
}) as InGraphIdentifierDefinition & { name: string });
299+
const written: (InGraphIdentifierDefinition & { name: string })[] = [];
300+
for(const refs of [target.in, target.unknownReferences]) {
301+
for(const ref of refs) {
302+
written.push({
303+
nodeId: ref.nodeId,
304+
name: ref.name as string,
305+
type: referenceType,
306+
definedAt: rootId,
307+
controlDependencies: data.controlDependencies ?? (makeMaybe ? [] : undefined),
308+
value
309+
});
310+
}
311+
}
312+
return written;
306313
}
307314

308315
function processAssignmentToString<OtherInfo>(

src/dataflow/internal/process/functions/call/built-in/built-in-expression-list.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,10 @@ export function processExpressionList<OtherInfo>(
163163
out = out.concat(processed.out);
164164

165165
// all inputs that have not been written until now are read!
166-
for(const read of processed.in.concat(processed.unknownReferences)) {
167-
linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph);
166+
for(const ls of [processed.in, processed.unknownReferences]) {
167+
for(const read of ls) {
168+
linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph);
169+
}
168170
}
169171

170172
const calledEnvs = linkFunctionCalls(nextGraph, data.completeAst.idMap, processed.graph);

0 commit comments

Comments
 (0)