-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsession.ts
566 lines (514 loc) · 23.6 KB
/
session.ts
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
import * as vscode from 'vscode';
import * as proto from '../cxxrtl/proto';
import * as link from '../cxxrtl/link';
import { Connection } from '../cxxrtl/client';
import { TimeInterval, TimePoint } from '../model/time';
import { Diagnostic, DiagnosticType, Reference, Sample, UnboundReference } from '../model/sample';
import { Variable } from '../model/variable';
import { Scope } from '../model/scope';
import { Location } from '../model/source';
function lazy<T>(thunk: () => Thenable<T>): Thenable<T> {
return { then: (onfulfilled, onrejected) => thunk().then(onfulfilled, onrejected) };
}
function matchLocation(location: Location, filename: string, position: vscode.Position) {
if (location.file !== filename) {
return false;
}
if (location.startLine !== position.line) {
return false;
}
if (location.startColumn !== undefined && location.startColumn !== position.character) {
return false;
}
return true;
}
export interface ISimulationStatus {
status: 'running' | 'paused' | 'finished';
latestTime: TimePoint;
nextSampleTime?: TimePoint;
}
export enum SimulationPauseReason {
TimeReached,
DiagnosticsReached,
}
export class Session {
private connection: Connection;
private secondaryLinks: link.ILink[] = [];
private greetingPacketPromise: Promise<proto.ServerGreeting>;
constructor(link: link.ILink) {
this.connection = new Connection(link);
this.greetingPacketPromise = new Promise((resolve, _reject) => {
this.connection.onConnected = async (greetingPacket) => resolve(greetingPacket);
});
this.connection.onDisconnected = async () => {
for (const secondaryLink of this.secondaryLinks) {
secondaryLink.onDone();
}
};
this.connection.onEvent = async (linkEvent) => {
for (const secondaryLink of this.secondaryLinks) {
secondaryLink.onRecv(linkEvent);
}
const event = linkEvent.asObject();
if (event.event === 'simulation_paused') {
await this.handleSimulationPausedEvent(event.cause);
} else if (event.event === 'simulation_finished') {
await this.handleSimulationFinishedEvent();
}
};
this.querySimulationStatus(); // populate nextSampleTime
}
dispose() {
this.connection.dispose();
}
createSecondaryLink(): link.ILink {
const secondaryLink: link.ILink = {
dispose: () => {
this.secondaryLinks.splice(this.secondaryLinks.indexOf(secondaryLink));
},
send: async (linkCommandPacket) => {
const packet = linkCommandPacket.asObject();
if (packet.type === 'greeting') {
const serverGreetingPacket = await this.greetingPacketPromise;
if (packet.version === serverGreetingPacket.version) {
await secondaryLink.onRecv(link.Packet.fromObject(serverGreetingPacket));
} else {
throw new Error(
`Secondary link requested greeting version ${packet.version}, ` +
`but server greeting version is ${serverGreetingPacket.version}`
);
}
} else {
const linkResponsePacket = await this.connection.exchange(
linkCommandPacket.cast<proto.AnyCommand>());
await secondaryLink.onRecv(linkResponsePacket);
}
},
onRecv: async (serverPacket) => {},
onDone: async () => {},
};
this.secondaryLinks.push(secondaryLink);
return secondaryLink;
}
// ======================================== Inspecting the design
private itemCache: Map<string, proto.ItemDescriptionMap> = new Map();
private scopeCache: Map<string, proto.ScopeDescriptionMap> = new Map();
private rootScopeDesc: proto.ScopeDescription | undefined;
private async listItemsInScope(scopeIdentifier: string): Promise<proto.ItemDescriptionMap> {
let itemDescriptionMap = this.itemCache.get(scopeIdentifier);
if (itemDescriptionMap === undefined) {
const response = await this.connection.listItems({
type: 'command',
command: 'list_items',
scope: scopeIdentifier,
});
itemDescriptionMap = response.items;
this.itemCache.set(scopeIdentifier, itemDescriptionMap);
}
return itemDescriptionMap;
}
private async listScopesInScope(scopeIdentifier: string): Promise<proto.ScopeDescriptionMap> {
let scopeDescriptionMap = this.scopeCache.get(scopeIdentifier);
if (scopeDescriptionMap === undefined) {
const response = await this.connection.listScopes({
type: 'command',
command: 'list_scopes',
scope: scopeIdentifier,
});
const filteredScopes = Object.keys(response.scopes).filter((scopeName) => {
if (scopeIdentifier === '') {
return scopeName.length > 0 && scopeName.indexOf(' ') === -1;
} else {
return (scopeName.startsWith(scopeIdentifier + ' ') &&
scopeName.indexOf(' ', scopeIdentifier.length + 1) === -1);
}
});
scopeDescriptionMap = Object.fromEntries(filteredScopes.map((scopeName) =>
[scopeName, response.scopes[scopeName]]));
this.scopeCache.set(scopeIdentifier, scopeDescriptionMap);
}
return scopeDescriptionMap;
}
async getVariablesIn(scopeIdentifier: string): Promise<Variable[]> {
const items = await this.listItemsInScope(scopeIdentifier);
return Object.entries(items).map(([itemName, itemDesc]) =>
Variable.fromCXXRTL(itemName, itemDesc));
}
async getScopesIn(scopeIdentifier: string): Promise<Scope[]> {
const scopes = await this.listScopesInScope(scopeIdentifier);
return Object.entries(scopes).map(([scopeName, scopeDesc]) =>
Scope.fromCXXRTL(
scopeName,
scopeDesc,
lazy(() => this.getScopesIn(scopeName)),
lazy(() => this.getVariablesIn(scopeName)),
));
}
async getRootScope(): Promise<Scope> {
const scopeName = '';
if (this.rootScopeDesc === undefined) {
const response = await this.connection.listScopes({
type: 'command',
command: 'list_scopes',
scope: scopeName,
});
this.rootScopeDesc = response.scopes[scopeName];
if (this.rootScopeDesc === undefined) {
// This can happen if the root scope has never been defined anywhere, i.e. if it
// is synthesized for the simulation, e.g. by passing `"top "` as the last argument
// to the CXXRTL agent constructor.
this.rootScopeDesc = {
type: 'module',
definition: {
src: null,
name: null,
attributes: {}
},
instantiation: {
src: null,
attributes: {}
},
};
}
}
return Scope.fromCXXRTL(
scopeName,
this.rootScopeDesc,
lazy(() => this.getScopesIn(scopeName)),
lazy(() => this.getVariablesIn(scopeName))
);
}
async getVariable(variableIdentifier: string): Promise<Variable | null> {
const identifierParts = variableIdentifier.split(' ');
const scopeIdentifier = identifierParts.slice(0, identifierParts.length - 1).join(' ');
const items = await this.listItemsInScope(scopeIdentifier);
if (variableIdentifier in items) {
return Variable.fromCXXRTL(variableIdentifier, items[variableIdentifier]);
} else {
return null;
}
}
async getVariablesForLocation(filename: string, position: vscode.Position): Promise<Variable[]> {
const variables: Variable[] = [];
const extractVariablesForLocationFromScope = async (scope: string) => {
const items = await this.listItemsInScope(scope);
for (const [itemName, itemDesc] of Object.entries(items)) {
const itemLocation = Location.fromCXXRTL(itemDesc.src);
if (itemLocation !== null && matchLocation(itemLocation, filename, position)) {
variables.push(Variable.fromCXXRTL(itemName, itemDesc));
}
}
const subScopes = await this.listScopesInScope(scope);
for (const subScopeName of Object.keys(subScopes)) {
await extractVariablesForLocationFromScope(subScopeName);
}
return null;
};
await extractVariablesForLocationFromScope('');
return variables;
}
// ======================================== Querying the database
private referenceEpochs: Map<string, number> = new Map();
private advanceReferenceEpoch(name: string): number {
const epoch = (this.referenceEpochs.get(name) || 0) + 1;
this.referenceEpochs.set(name, epoch);
return epoch;
}
private checkReferenceEpoch(name: string, requestedEpoch: number) {
const currentEpoch = this.referenceEpochs.get(name);
if (currentEpoch === undefined) {
throw new ReferenceError(
`Querying dangling reference ${name}#${requestedEpoch}`);
} else if (currentEpoch !== requestedEpoch) {
throw new ReferenceError(
`Querying out-of-date reference ${name}#${requestedEpoch}; ` +
`the current binding is ${name}#${currentEpoch}`);
}
}
bindReference(name: string, reference: UnboundReference): Reference {
const epoch = this.advanceReferenceEpoch(name);
// Note that we do not wait for the command to complete. Although it is possible for
// the command to fail, this would only happen if one of the designations is invalid,
// which should never happen absent bugs. We still report the error in that case.
this.connection.referenceItems({
type: 'command',
command: 'reference_items',
reference: name,
items: reference.cxxrtlItemDesignations()
}).catch((error) => {
console.error('[CXXRTL] Invalid designation while binding reference', `${name}#${epoch}`, error);
});
return new Reference(name, epoch, reference);
}
async queryInterval(
interval: TimeInterval,
options: {
reference?: Reference,
diagnostics?: boolean
collapse?: boolean,
} = {}
): Promise<Sample[]> {
const reference = options.reference;
if (reference !== undefined) {
this.checkReferenceEpoch(reference.name, reference.epoch);
}
const response = await this.connection.queryInterval({
type: 'command',
command: 'query_interval',
interval: interval.toCXXRTL(),
items: reference?.name ?? null,
item_values_encoding: reference ? 'base64(u32)' : null,
diagnostics: options.diagnostics ?? false,
collapse: options.collapse ?? true,
});
return response.samples.map((cxxrtlSample) => {
let itemValuesArray = null;
let diagnosticsArray = null;
if (cxxrtlSample.item_values !== undefined) {
const itemValuesBuffer = Buffer.from(cxxrtlSample.item_values, 'base64');
itemValuesArray = new Uint32Array(
itemValuesBuffer.buffer,
itemValuesBuffer.byteOffset,
itemValuesBuffer.length / Uint32Array.BYTES_PER_ELEMENT
);
}
if (cxxrtlSample.diagnostics !== undefined) {
diagnosticsArray = Array.from(cxxrtlSample.diagnostics, Diagnostic.fromCXXRTL);
}
return new Sample(
TimePoint.fromCXXRTL(cxxrtlSample.time),
reference?.unbound ?? null,
itemValuesArray,
diagnosticsArray,
);
});
}
async queryAtCursor(options: {
reference?: Reference,
diagnostics?: boolean
}): Promise<Sample> {
const interval = new TimeInterval(this.timeCursor, this.timeCursor);
const [sample] = await this.queryInterval(interval, options);
return sample;
}
// ======================================== Manipulating the simulation
private simulationStatusTimeout: NodeJS.Timeout | null = null;
private _onDidChangeSimulationStatus: vscode.EventEmitter<ISimulationStatus> = new vscode.EventEmitter();
readonly onDidChangeSimulationStatus: vscode.Event<ISimulationStatus> = this._onDidChangeSimulationStatus.event;
private _onDidRunSimulation: vscode.EventEmitter<void> = new vscode.EventEmitter();
readonly onDidRunSimulation: vscode.Event<void> = this._onDidRunSimulation.event;
private _onDidPauseSimulation: vscode.EventEmitter<SimulationPauseReason> = new vscode.EventEmitter();
readonly onDidPauseSimulation: vscode.Event<SimulationPauseReason> = this._onDidPauseSimulation.event;
private _onDidFinishSimulation: vscode.EventEmitter<void> = new vscode.EventEmitter();
readonly onDidFinishSimulation: vscode.Event<void> = this._onDidFinishSimulation.event;
private _simulationStatus: ISimulationStatus = {
status: 'paused',
latestTime: TimePoint.ZERO,
};
get simulationStatus() {
return this._simulationStatus;
}
private async querySimulationStatus(): Promise<void> {
if (this.simulationStatusTimeout !== null) {
clearTimeout(this.simulationStatusTimeout);
this.simulationStatusTimeout = null;
}
const response = await this.connection.getSimulationStatus({
type: 'command',
command: 'get_simulation_status'
});
const currentSimulationStatus = {
status: response.status,
latestTime: TimePoint.fromCXXRTL(response.latest_time),
nextSampleTime: (response.status === 'paused')
? TimePoint.fromCXXRTL(response.next_sample_time)
: undefined,
};
if ((this._simulationStatus.status !== currentSimulationStatus.status) ||
!this._simulationStatus.latestTime.equals(currentSimulationStatus.latestTime) ||
// This part of the condition only fires once, when the initial status is updated.
(this._simulationStatus.nextSampleTime === undefined &&
currentSimulationStatus.nextSampleTime !== undefined)) {
this._simulationStatus = currentSimulationStatus;
this._onDidChangeSimulationStatus.fire(this._simulationStatus);
}
if (currentSimulationStatus.status === 'running') {
this.simulationStatusTimeout = setTimeout(() => this.querySimulationStatus(), 100);
}
}
async runSimulation(options: {
untilTime?: TimePoint,
untilDiagnostics?: DiagnosticType[]
} = {}): Promise<void> {
await this.connection.runSimulation({
type: 'command',
command: 'run_simulation',
until_time: options.untilTime?.toCXXRTL() ?? null,
until_diagnostics: options.untilDiagnostics?.map((type) => <DiagnosticType>type) ?? [],
sample_item_values: true
});
await this.querySimulationStatus();
this._onDidRunSimulation.fire();
}
async pauseSimulation(): Promise<void> {
await this.connection.pauseSimulation({
type: 'command',
command: 'pause_simulation'
});
}
private async handleSimulationPausedEvent(cause: proto.PauseCause): Promise<void> {
if (cause === 'until_time') {
await this.querySimulationStatus();
this._onDidPauseSimulation.fire(SimulationPauseReason.TimeReached);
} else if (cause === 'until_diagnostics') {
// The `until_diagnostics` cause is a little cursed. For `always @(posedge clk)`
// assertions, the pause time will be two steps ahead, and for `always @(*)` ones,
// it will usually be one step ahead. This is because of several fencepost issues with
// both the storage and the retrieval of diagnostics, which are baked into the CXXRTL
// execution and replay model. (Diagnostics recorded from C++ are fine.)
//
// To handle this, rather than relying on the event time, we scan the database for any
// diagnostics since the last time the simulation state was updated. (A diagnostic that
// caused the simulation to be paused must be somewhere between that and the latest
// sample in the database at the time of pausing.) This avoids the need to simulate
// the entire interval twice, as would happen if querying the interval between the "Run
// Simulation Until Diagnostics" command and the time of pausing.
const latestTimeBeforePause = this.simulationStatus.latestTime;
await this.querySimulationStatus();
const latestTimeAfterPause = this.simulationStatus.latestTime;
const diagnosticAt = await this.searchIntervalForDiagnostics(
new TimeInterval(latestTimeBeforePause, latestTimeAfterPause));
if (diagnosticAt === null) {
console.error('[CXXRTL] Paused on diagnostic but no such diagnostics found');
return;
}
this.timeCursor = diagnosticAt;
this._onDidPauseSimulation.fire(SimulationPauseReason.DiagnosticsReached);
}
}
private async handleSimulationFinishedEvent(): Promise<void> {
await this.querySimulationStatus();
this._onDidFinishSimulation.fire();
}
get isSimulationRunning(): boolean {
return this._simulationStatus.status === 'running';
}
// ======================================== Manipulating the time cursor
private _onDidChangeTimeCursor: vscode.EventEmitter<TimePoint> = new vscode.EventEmitter<TimePoint>();
readonly onDidChangeTimeCursor: vscode.Event<TimePoint> = this._onDidChangeTimeCursor.event;
private _timeCursor: TimePoint = TimePoint.ZERO;
// We don't know how far forward the next time step will be; the server doesn't provide this
// information. A typical simulation has a consistent time step, or at least a time step of
// a consistent order of magnitude; we guess this time step using binary search and then look
// ahead to find out the actual next cursor position. The advantage of this approach is that
// the simulation can advance its timeline however it wants.
private _forwardTimeStep: bigint = 1n; // in femtos
get timeCursor() {
return this._timeCursor;
}
set timeCursor(newTimeCursor: TimePoint) {
if (newTimeCursor.lessThan(TimePoint.ZERO) ||
newTimeCursor.greaterThan(this.simulationStatus.latestTime)) {
throw new RangeError('Time cursor out of range');
}
this._timeCursor = newTimeCursor;
this._onDidChangeTimeCursor.fire(this._timeCursor);
}
async stepForward(): Promise<TimePoint> {
if (this.timeCursor.equals(this.simulationStatus.latestTime)) {
if (this.simulationStatus.status === 'paused') {
const nextSampleTime = this.simulationStatus.nextSampleTime!;
await this.runSimulation({ untilTime: nextSampleTime });
if (!nextSampleTime.greaterThan(this.simulationStatus.latestTime)) {
this.timeCursor = nextSampleTime;
}
}
} else {
let ordersOfMagnitude = 0;
while (true) {
const followingTimePoint = this.timeCursor.offsetByFemtos(this._forwardTimeStep);
const response = await this.connection.queryInterval({
type: 'command',
command: 'query_interval',
interval: new TimeInterval(this._timeCursor, followingTimePoint).toCXXRTL(),
collapse: true,
items: null,
item_values_encoding: null,
diagnostics: false
});
if (response.samples.length > 1) {
this.timeCursor = TimePoint.fromCXXRTL(response.samples.at(1)!.time);
break;
} else if (ordersOfMagnitude < 30 /* femto -> peta */) {
ordersOfMagnitude += 1;
this._forwardTimeStep = this._forwardTimeStep * 10n;
} else {
throw new RangeError('Could not find a sample to step forward to');
}
}
}
return this.timeCursor;
}
async stepBackward(): Promise<TimePoint> {
if (!this.timeCursor.equals(TimePoint.ZERO)) {
const precedingTimePoint = this.timeCursor.offsetByFemtos(-1n);
const response = await this.connection.queryInterval({
type: 'command',
command: 'query_interval',
interval: new TimeInterval(precedingTimePoint, precedingTimePoint).toCXXRTL(),
collapse: true,
items: null,
item_values_encoding: null,
diagnostics: false
});
this.timeCursor = TimePoint.fromCXXRTL(response.samples.at(0)!.time);
}
return this.timeCursor;
}
async continueForward(): Promise<void> {
if (this.timeCursor.lessThan(this.simulationStatus.latestTime)) {
const diagnosticAt = await this.searchIntervalForDiagnostics(
new TimeInterval(this.timeCursor, this.simulationStatus.latestTime));
if (diagnosticAt !== null) {
this.timeCursor = diagnosticAt;
return;
}
}
// No diagnostics between time cursor and end of database; run the simulation.
if (this.simulationStatus.status === 'paused') {
// The pause handler will run `searchIntervalForDiagnostics`.
await this.runSimulation({
untilDiagnostics: [
DiagnosticType.Assert,
DiagnosticType.Assume,
DiagnosticType.Break
]
});
} else if (this.simulationStatus.status === 'finished') {
this.timeCursor = this.simulationStatus.latestTime;
}
}
private async searchIntervalForDiagnostics(interval: TimeInterval): Promise<TimePoint | null> {
const response = await this.connection.queryInterval({
type: 'command',
command: 'query_interval',
interval: interval.toCXXRTL(),
collapse: true,
items: null,
item_values_encoding: null,
diagnostics: true
});
for (const sample of response.samples) {
const sampleTime = TimePoint.fromCXXRTL(sample.time);
if (!sampleTime.greaterThan(interval.begin)) {
continue;
}
for (const diagnostic of sample.diagnostics!) {
if (['break', 'assert', 'assume'].includes(diagnostic.type)) {
return sampleTime;
}
}
}
return null;
}
}