diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5dac97..b1139b56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,41 @@ platform API version. The ram is drawn as the stack of a load, longer, with its shape (`text`) after it, and it says `ram` when no shape is given. A product on an older platform gets no spec for the kind and draws its node as before. +- **Sixteen more flow symbols**, which makes 45 kinds. A product that names + none of them draws as before, and no drawing of the 29 kinds changes. + - Decisions, in the choice family: `first`, `priority` and `race` are a + rounded block with an italic F, P or R. A priority names its inputs `hi` + and `lo` at the edge beside their pins, in a block a little longer than a + frame. The flags of a race, its last operands, end in a dot as a + condition does. `share` is a mux over a branch, for a unit that several + operations take turns on: the unit hangs off the side at the waist. Each + operation is numbered in a band at either end, clear of the slopes and of + the pins of the unit. + - A pass that does one more thing, in the branch family: `tap` gives a + second result out of its side when a token passes (`variant: 'at-valid'` + taps the base, for one that says so when the token is first offered), + `stand-in` takes what stops it on the side and answers 0, and `stall` is + a die on the wire, for one that holds a token back at random. Several + signals that stop a stand-in meet on a rail beside it, which ends in the + dot one signal would. + - `dead-buffer`, in the storage family: a buffer crossed out, with an input + and no output. + - Channels, in the bundle family: `no-ready` draws a channel wide with its + ready coming back to a bar, and `valid-merge` two channels with the valid + of the first going over to the second. + - Speculation, a family of its own, `spec`: `speculator`, `spec-save`, + `spec-commit`, `spec-save-commit`, `spec-branch` and `spec-tag`. A token + that is a guess is a ring and one that is not a dot. The family is purple + (`flow-spec` in `flow-symbols.css`), the chart colour that forks and joins + have and show on no body. + - `FlowDepiction.group` is new: the flags of a race, and the operands of + the unit a share serves. A share needs it, since its pins follow from it + (`n` operations of `group` operands give `n × group + 1` operands and + `n + group` results), and gets no spec without it or with other counts. + A race whose `group` is no count of its operands draws no flag. + - A symbol with several pins on its side spreads them along it, a pin pitch + apart: the signals that stop a stand-in, the unit of a share, the three + answers of a speculator, the two controls of a save and commit. - **Find in the diagram.** The palette's loupe opens a box at the top of the palette that finds nodes in the model, not in the drawing, so a node off screen or too small to be drawn at the zoom is found too. It matches a diff --git a/packages/diagram-client/src/flow-symbols.css b/packages/diagram-client/src/flow-symbols.css index 5b324d5e..8a012f71 100644 --- a/packages/diagram-client/src/flow-symbols.css +++ b/packages/diagram-client/src/flow-symbols.css @@ -1,10 +1,10 @@ /* Flow symbols (flow-symbols.ts, flow-symbol-views.ts): dataflow pictograms in the theme's colours. A product that draws them imports this file. - The lines are the editor's foreground. The body of the six tinted families + The lines are the editor's foreground. The body of the seven tinted families is a quarter of a chart colour over the editor's background (buffers green, forks and joins purple, muxes and merges blue, branches yellow, memories - red, constants orange). The other families fill with the widget background, + red, constants orange, speculation purple). The other families fill with the widget background, as a box does. Every colour is a theme variable, so a dark, a light and a high contrast theme all draw them. The line widths go with the drawing's scale (FlowSymbols.SCALE, 2): a symbol's outline reads as heavy as the @@ -20,6 +20,9 @@ .flow-symbol.flow-branch { --flow-body: color-mix(in srgb, var(--vscode-charts-yellow) 24%, var(--flow-ground)); } .flow-symbol.flow-memory { --flow-body: color-mix(in srgb, var(--vscode-charts-red) 24%, var(--flow-ground)); } .flow-symbol.flow-constant { --flow-body: color-mix(in srgb, var(--vscode-charts-orange) 24%, var(--flow-ground)); } +/* Speculation takes purple too: a fork, a join and a blocker have no body, so + no other symbol shows it. */ +.flow-symbol.flow-spec { --flow-body: color-mix(in srgb, var(--vscode-charts-purple) 24%, var(--flow-ground)); } .flow-symbol .flow-hit { fill: transparent; stroke: none; } .flow-symbol .flow-line { @@ -51,6 +54,7 @@ pointer-events: none; } .flow-symbol .flow-glyph { font-weight: 600; } +.flow-symbol .flow-italic { font-style: italic; } .flow-symbol .flow-value { font-family: var(--vscode-editor-font-family); } .flow-symbol .flow-tiny { font-weight: 600; opacity: 0.72; } .flow-symbol .flow-mark { opacity: 0.8; } diff --git a/packages/diagram-client/test/flow-symbol-views.test.ts b/packages/diagram-client/test/flow-symbol-views.test.ts index 0201bb86..107dd87e 100644 --- a/packages/diagram-client/test/flow-symbol-views.test.ts +++ b/packages/diagram-client/test/flow-symbol-views.test.ts @@ -23,7 +23,7 @@ const css = readFileSync(path.join(here, '../src/flow-symbols.css'), 'utf8'); /** A count each kind draws, the least it takes but one result for a fork. */ function counts(kind: string): [number, number] { for (let ins = 0; ins < 6; ins++) { - for (let outs = 0; outs < 4; outs++) { + for (let outs = 0; outs < 6; outs++) { if (FlowSymbols.fits(kind as never, ins, outs)) { return [ins, outs]; } @@ -37,7 +37,8 @@ describe('flow symbol views', () => { for (const kind of FlowSymbols.KINDS) { for (const direction of ['DOWN', 'RIGHT'] as FlowDirection[]) { const [ins, outs] = counts(kind); - const spec = FlowSymbols.spec({ symbol: kind, glyph: '+', text: 'x' }, ins, outs, direction)!; + // A share says how many operands its unit takes: one, for two operations. + const spec = FlowSymbols.spec({ symbol: kind, glyph: '+', text: 'x', group: 1 }, ins, outs, direction)!; const g = renderFlowSymbol(spec) as unknown as Drawn; expect(g.tag).toBe('g'); expect(g.props.class).toMatchObject({ 'flow-symbol': true, [`flow-${spec.family}`]: true, [`flow-kind-${kind}`]: true }); @@ -72,9 +73,10 @@ describe('flow symbol views', () => { } }); - it('tint the six families from the theme chart colours', () => { + it('tint the seven families from the theme chart colours', () => { const tinted = [...css.matchAll(/\.flow-symbol\.flow-([a-z]+) \{ --flow-body: color-mix\(in srgb, var\(--vscode-charts-([a-z]+)\)/g)] .map(m => `${m[1]} ${m[2]}`); - expect(tinted).toEqual(['storage green', 'split purple', 'choice blue', 'branch yellow', 'memory red', 'constant orange']); + // Speculation shares purple with the forks and the joins, which have no body to tint. + expect(tinted).toEqual(['storage green', 'split purple', 'choice blue', 'branch yellow', 'memory red', 'constant orange', 'spec purple']); }); }); diff --git a/packages/diagram-client/test/flow-symbols.test.ts b/packages/diagram-client/test/flow-symbols.test.ts index 1dba8e38..43a7c29c 100644 --- a/packages/diagram-client/test/flow-symbols.test.ts +++ b/packages/diagram-client/test/flow-symbols.test.ts @@ -17,8 +17,15 @@ const COUNTS: Record = { end: [4, 0], instance: [2, 2], bundle: [2, 1], unbundle: [1, 2], load: [2, 2], store: [2, 2], memory: [4, 2], 'load-store-queue': [5, 3], ram: [0, 1], - operator: [2, 1], compare: [2, 1], width: [1, 1], not: [1, 1] + operator: [2, 1], compare: [2, 1], width: [1, 1], not: [1, 1], + first: [3, 1], priority: [2, 1], race: [3, 1], share: [5, 4], + tap: [1, 2], 'stand-in': [3, 1], stall: [1, 1], + 'dead-buffer': [1, 0], + 'no-ready': [1, 1], 'valid-merge': [2, 2], + speculator: [2, 4], 'spec-save': [2, 1], 'spec-commit': [2, 1], 'spec-save-commit': [3, 1], 'spec-branch': [2, 2], 'spec-tag': [1, 1] }; +/** What a kind needs said to be drawn with those counts: a race two flags, a share two operations of two operands. */ +const FIELDS: Partial>> = { race: { group: 2 }, share: { group: 2 } }; const DIRECTIONS: FlowDirection[] = ['DOWN', 'RIGHT']; /** Kinds whose text widens them across the flow: not drawn the same turned over. */ const TEXT_ROOM = new Set(['constant', 'end', 'instance', 'memory', 'load-store-queue', 'ram', 'width']); @@ -26,10 +33,18 @@ const TEXT_ROOM = new Set(['constant', 'end', 'instance', 'memor /** The frames in pixels: every symbol's, and a choice's, larger. */ const FRAME = FlowSymbols.FRAME * FlowSymbols.SCALE; const CHOICE = { along: FlowSymbols.CHOICE_ALONG * FlowSymbols.SCALE, across: FlowSymbols.CHOICE_ACROSS * FlowSymbols.SCALE }; -const CHOICES = new Set(['mux', 'merge', 'control-merge', 'select', 'branch']); +const CHOICES = new Set(['mux', 'merge', 'control-merge', 'select', 'branch', 'spec-branch']); +/** The pins each kind has on its side edge, in the order it gives them. */ +const SIDE: Partial> = { + mux: ['operand 0'], select: ['operand 0'], branch: ['operand 0'], 'control-merge': ['result 1'], + 'spec-branch': ['operand 0'], tap: ['result 1'], 'stand-in': ['operand 1', 'operand 2'], + share: ['operand 4', 'result 2', 'result 3'], + speculator: ['result 1', 'result 2', 'result 3'], + 'spec-save': ['operand 1'], 'spec-commit': ['operand 1'], 'spec-save-commit': ['operand 1', 'operand 2'] +}; const spec = (kind: FlowSymbolKind, direction: FlowDirection, extra: Record = {}) => - FlowSymbols.spec({ symbol: kind, ...extra }, COUNTS[kind][0], COUNTS[kind][1], direction)!; + FlowSymbols.spec({ symbol: kind, ...FIELDS[kind], ...extra }, COUNTS[kind][0], COUNTS[kind][1], direction)!; function pointsOf(s: FlowShape): Array<[number, number]> { switch (s.shape) { @@ -49,7 +64,7 @@ function pointsOf(s: FlowShape): Array<[number, number]> { describe('flow symbols', () => { it('list every kind, each in a family', () => { - expect(FlowSymbols.KINDS).toHaveLength(29); + expect(FlowSymbols.KINDS).toHaveLength(45); expect(Object.keys(COUNTS).sort()).toEqual([...FlowSymbols.KINDS].sort()); }); @@ -140,9 +155,14 @@ describe('flow symbols', () => { // Up to two pins a row, and the shortest text. const few: Partial> = { fork: [1, 2], 'lazy-fork': [1, 2], join: [2, 1], blocker: [2, 1], mux: [3, 1], select: [3, 1], merge: [2, 1], - end: [2, 0], instance: [2, 2] + end: [2, 0], instance: [2, 2], first: [2, 1], race: [2, 1], 'stand-in': [2, 1] }; - const grown = new Set(['memory', 'load-store-queue', 'ram', 'instance', 'end']); + // A share is a mux over a branch, a speculator and a save and commit + // have more pins on the side than one frame holds, and a priority + // names its inputs in a row of its block. + const grown = new Set([ + 'memory', 'load-store-queue', 'ram', 'instance', 'end', 'share', 'speculator', 'spec-save-commit', 'priority' + ]); for (const kind of FlowSymbols.KINDS) { if (grown.has(kind)) continue; const [ins, outs] = few[kind] ?? COUNTS[kind]; @@ -160,7 +180,7 @@ describe('flow symbols', () => { }); it('bring the select, the condition and the index in halfway along the side', () => { - for (const kind of ['mux', 'select', 'control-merge', 'branch'] as FlowSymbolKind[]) { + for (const kind of ['mux', 'select', 'control-merge', 'branch', 'spec-branch', 'tap', 'spec-save', 'spec-commit'] as FlowSymbolKind[]) { for (const direction of DIRECTIONS) { const s = spec(kind, direction); const side = s.pins.find(p => p.onSide)!; @@ -174,11 +194,312 @@ describe('flow symbols', () => { const sides = (kind: FlowSymbolKind, direction: FlowDirection) => spec(kind, direction).pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`); for (const kind of FlowSymbols.KINDS) { - const expected = kind === 'mux' || kind === 'select' || kind === 'branch' ? 'operand 0' - : kind === 'control-merge' ? 'result 1' : undefined; - expect(sides(kind, 'DOWN'), kind).toEqual(expected ? [`${expected} EAST`] : []); - expect(sides(kind, 'RIGHT'), kind).toEqual(expected ? [`${expected} SOUTH`] : []); + const expected = SIDE[kind] ?? []; + expect(sides(kind, 'DOWN'), kind).toEqual(expected.map(p => `${p} EAST`)); + expect(sides(kind, 'RIGHT'), kind).toEqual(expected.map(p => `${p} SOUTH`)); + } + }); + + it('spread several pins along the side, in their order, clear of the ends', () => { + for (const kind of ['stand-in', 'share', 'speculator', 'spec-save-commit'] as FlowSymbolKind[]) { + const s = spec(kind, 'DOWN'); + const along = s.pins.filter(p => p.onSide).map(p => p.y); + expect(along.length, kind).toBeGreaterThan(1); + expect([...along].sort((a, b) => a - b), kind).toEqual(kind === 'share' ? [along[1], along[2], along[0]] : along); + expect(Math.min(...along), kind).toBeGreaterThanOrEqual(FlowSymbols.PIN_MARGIN * FlowSymbols.SCALE); + expect(Math.max(...along), kind).toBeLessThanOrEqual(s.height - FlowSymbols.PIN_MARGIN * FlowSymbols.SCALE); + for (let k = 1; k < along.length; k++) { + const sorted = [...along].sort((a, b) => a - b); + expect(sorted[k] - sorted[k - 1], kind).toBeCloseTo(FlowSymbols.PIN_PITCH * FlowSymbols.SCALE); + } + } + }); + + it('draw a first, a priority and a race as a rounded block with its letter in italics', () => { + const letterOf = (kind: FlowSymbolKind) => spec(kind, 'DOWN').shapes.find(t => t.shape === 'text' && t.classes.includes('flow-italic')); + expect(letterOf('first')).toMatchObject({ text: 'F', level: 'name' }); + expect(letterOf('priority')).toMatchObject({ text: 'P', level: 'name' }); + expect(letterOf('race')).toMatchObject({ text: 'R', level: 'name' }); + for (const kind of ['first', 'priority', 'race'] as FlowSymbolKind[]) { + for (const direction of DIRECTIONS) { + const s = spec(kind, direction); + expect(s.family).toBe('choice'); + const body = s.shapes.find(t => t.shape === 'rect' && t.classes.includes('flow-body')) as Extract; + expect(body.rx, `${kind} ${direction}`).toBe(5 * FlowSymbols.SCALE); + // Every input on the input edge, the one output on the output edge. + expect(s.pins.filter(p => p.onSide), `${kind} ${direction}`).toHaveLength(0); + expect(s.pins.filter(p => p.of === 'result'), `${kind} ${direction}`).toHaveLength(1); + } + } + const marks = (kind: FlowSymbolKind, cls: string, extra = {}) => spec(kind, 'DOWN', extra).shapes.filter(t => t.classes.includes(cls)).length; + // The flags of a race, its last inputs, end in a dot. A first has none. + expect(marks('race', 'flow-dot')).toBe(2); + expect(marks('race', 'flow-dot', { group: 0 })).toBe(0); + // Every input of a race may be a flag. A group that is no count of + // its inputs draws none, as a producer that sent no group would. + expect(marks('race', 'flow-dot', { group: 3 })).toBe(3); + expect(marks('race', 'flow-dot', { group: 9 })).toBe(0); + expect(marks('race', 'flow-dot', { group: 1.5 })).toBe(0); + expect(marks('race', 'flow-dot', { group: -1 })).toBe(0); + expect(marks('race', 'flow-dot', { group: 'x' })).toBe(0); + expect(marks('first', 'flow-dot')).toBe(0); + // The dots sit on the input edge of the block, at the last two pins. + const race = spec('race', 'DOWN'); + const dots = race.shapes.filter(t => t.shape === 'circle' && t.classes.includes('flow-dot')) as Array>; + expect(dots.map(c => c.cx)).toEqual(race.pins.filter(p => p.of === 'operand' && p.index > 0).map(p => p.x)); + // A first widens across its flow for its inputs and keeps its length. + expect(FlowSymbols.spec({ symbol: 'first' }, 5, 1, 'DOWN')!.width).toBeGreaterThan(FRAME); + expect(FlowSymbols.spec({ symbol: 'first' }, 5, 1, 'DOWN')!.height).toBe(FRAME); + }); + + it('name the inputs of a priority at the edge, beside their pins', () => { + type Text = Extract; + const namesOf = (direction: FlowDirection) => spec('priority', direction).shapes + .filter(t => t.shape === 'text' && t.classes.includes('flow-tiny')) as Text[]; + const bodyOf = (direction: FlowDirection) => spec('priority', direction).shapes + .find(t => t.shape === 'rect' && t.classes.includes('flow-body')) as Extract; + // Down the page each name is centred under its pin, in a row below the input edge. + const down = spec('priority', 'DOWN'); + expect(namesOf('DOWN').map(t => `${t.text} ${t.anchor} ${t.level}`)).toEqual(['hi middle detail', 'lo middle detail']); + expect(namesOf('DOWN').map(t => t.x)).toEqual([0, 1].map(k => down.pins.find(p => p.of === 'operand' && p.index === k)!.x)); + expect(namesOf('DOWN')[0].y).toBe(namesOf('DOWN')[1].y); + // To the right each name starts inside the input edge, level with its pin. + const right = spec('priority', 'RIGHT'); + expect(namesOf('RIGHT').map(t => `${t.text} ${t.anchor}`)).toEqual(['hi start', 'lo start']); + expect(namesOf('RIGHT').map(t => t.y)).toEqual([0, 1].map(k => right.pins.find(p => p.of === 'operand' && p.index === k)!.y)); + expect(namesOf('RIGHT')[0].x).toBe(namesOf('RIGHT')[1].x); + expect(namesOf('RIGHT')[0].x).toBeGreaterThan(bodyOf('RIGHT').x); + // The letter stands clear of the names, at the size a first draws its own. + const letter = (direction: FlowDirection) => spec('priority', direction).shapes.find(t => t.shape === 'text' && t.classes.includes('flow-italic')) as Text; + expect(letter('DOWN').size).toBe((spec('first', 'DOWN').shapes.find(t => t.shape === 'text') as Text).size); + expect(letter('DOWN').y - letter('DOWN').size / 2).toBeGreaterThan(namesOf('DOWN')[0].y + namesOf('DOWN')[0].size / 2); + const nameEnd = namesOf('RIGHT')[0].x + 2 * namesOf('RIGHT')[0].size * 0.56; + expect(letter('RIGHT').x - letter('RIGHT').size * 0.3).toBeGreaterThan(nameEnd); + // The names take a row of the block: it is longer than a frame, and no wider. + expect([down.width, down.height]).toEqual([FRAME, FRAME + 4 * FlowSymbols.SCALE]); + expect([right.width, right.height]).toEqual([FRAME + 4 * FlowSymbols.SCALE, FRAME]); + // No bar and no arrow: the names alone tell the inputs apart. + expect(down.shapes.filter(t => t.classes.includes('flow-bar') || t.classes.includes('flow-fill'))).toHaveLength(0); + }); + + it('draw a tap and a stand-in as a pass with its signal on the side', () => { + type Circle = Extract; + const triangle = (kind: FlowSymbolKind, direction: FlowDirection, extra = {}) => + spec(kind, direction, extra).shapes.filter(t => t.shape === 'polygon' && t.classes.includes('flow-body')); + for (const direction of DIRECTIONS) { + const [side, along] = direction === 'DOWN' ? ['EAST', 'y'] as const : ['SOUTH', 'x'] as const; + // A tap gives its second result on the side, through a ring. + const tap = spec('tap', direction); + expect(tap.family).toBe('branch'); + expect(triangle('tap', direction)).toHaveLength(1); + expect(tap.pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`)).toEqual([`result 1 ${side}`]); + expect(tap.shapes.filter(t => t.classes.includes('flow-ring'))).toHaveLength(1); + expect(tap.shapes.filter(t => t.classes.includes('flow-dot'))).toHaveLength(0); + // One that says so when the token is first offered taps the base, nearer the input. + const early = spec('tap', direction, { variant: 'at-valid' }); + expect(early.pins.find(p => p.onSide)![along]).toBeLessThan(tap.pins.find(p => p.onSide)![along]); + expect([early.width, early.height]).toEqual([tap.width, tap.height]); + // A stand-in takes what stops it on the side and answers 0. The + // triangle and the dot on it are the same however many signals + // stop it: the body does not stretch, and no dot lands on its tip. + const one = FlowSymbols.spec({ symbol: 'stand-in' }, 2, 1, direction)!; + const bodyOf = (s: typeof one) => s.shapes.find(t => t.shape === 'polygon' && t.classes.includes('flow-body')) as Extract; + const dotsOf = (s: typeof one) => s.shapes.filter(t => t.classes.includes('flow-dot')) as Circle[]; + const linesOf = (s: typeof one) => s.shapes.filter(t => t.shape === 'line') as Array>; + // The one signal runs straight from its pin to the dot. + expect(dotsOf(one)[0][direction === 'DOWN' ? 'cy' : 'cx']).toBe(one.pins.find(p => p.onSide)![along]); + for (const kills of [1, 2, 3, 6]) { + const s = FlowSymbols.spec({ symbol: 'stand-in' }, 1 + kills, 1, direction)!; + expect(s.pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`), `${kills}`) + .toEqual(Array.from({ length: kills }, (_, j) => `operand ${1 + j} ${side}`)); + expect(s.shapes.filter(t => t.shape === 'text').map(t => (t as { text: string }).text)).toEqual(['0']); + expect(bodyOf(s).points, `${kills}`).toBe(bodyOf(one).points); + expect(dotsOf(s).map(c => [c.cx, c.cy, c.r]), `${kills}`).toEqual(dotsOf(one).map(c => [c.cx, c.cy, c.r])); + if (kills > 1) { + // Every signal runs from its pin to the rail, which spans them all. + const across = direction === 'DOWN' ? 'x' : 'y'; + const on = s.pins.filter(p => p.onSide); + const rails = linesOf(s).filter(l => l[`${across}1`] === l[`${across}2`] && l[`${across}1`] < on[0][across] && + Math.min(l[`${along}1`], l[`${along}2`]) === on[0][along] && Math.max(l[`${along}1`], l[`${along}2`]) === on[kills - 1][along]); + expect(rails, `${kills}`).toHaveLength(1); + const rail = rails[0][`${across}1`]; + for (const p of on) { + expect(linesOf(s).some(l => l[`${along}1`] === p[along] && l[`${along}2`] === p[along] && + Math.min(l[`${across}1`], l[`${across}2`]) === rail && Math.max(l[`${across}1`], l[`${across}2`]) === p[across]), `${kills} ${p.index}`).toBe(true); + } + // The rail stands clear of the body. + const far = Math.max(...bodyOf(s).points.split(' ').map(pt => Number(pt.split(',')[direction === 'DOWN' ? 0 : 1]))); + expect(rail, `${kills}`).toBeGreaterThan(far); + } + } + // Three of them need more than one frame along the flow. + const three = FlowSymbols.spec({ symbol: 'stand-in' }, 4, 1, direction)!; + expect(direction === 'DOWN' ? three.height : three.width).toBeGreaterThan(FRAME); + expect(direction === 'DOWN' ? three.width : three.height).toBe(FRAME); + } + expect(FlowSymbols.spec({ symbol: 'stand-in' }, 1, 1, 'DOWN')).toBeUndefined(); + expect(FlowSymbols.spec({ symbol: 'tap' }, 1, 1, 'DOWN')).toBeUndefined(); + }); + + it('draw a stall as a die on the wire and a dead buffer as a buffer crossed out', () => { + for (const direction of DIRECTIONS) { + const stall = spec('stall', direction); + expect(stall.shapes.filter(t => t.classes.includes('flow-dot'))).toHaveLength(5); + const die = stall.shapes.find(t => t.shape === 'rect') as Extract; + expect(die.width).toBe(die.height); + expect(die.rx).toBe(5 * FlowSymbols.SCALE); + expect(stall.pins.map(p => p.of)).toEqual(['operand', 'result']); + // A dead buffer has the body of a buffer, an input and no output. + const dead = spec('dead-buffer', direction); + const buffer = spec('buffer', direction); + const bodyOf = (s: typeof dead) => s.shapes.find(t => t.shape === 'rect' && t.classes.includes('flow-body')); + expect(dead.family).toBe('storage'); + expect(bodyOf(dead)).toEqual(bodyOf(buffer)); + expect(dead.pins.map(p => `${p.of} ${p.index}`)).toEqual(['operand 0']); + // The stub in and the two lines of the cross. + expect(dead.shapes.filter(t => t.shape === 'line')).toHaveLength(3); + } + expect(FlowSymbols.spec({ symbol: 'dead-buffer' }, 1, 1, 'DOWN')).toBeUndefined(); + }); + + it('draw a channel wide where its ready is stopped or its valid is shared', () => { + type Line = Extract; + for (const direction of DIRECTIONS) { + for (const kind of ['no-ready', 'valid-merge'] as FlowSymbolKind[]) { + const s = spec(kind, direction); + expect(s.family, kind).toBe('bundle'); + expect(s.shapes.filter(t => t.classes.includes('flow-body')), kind).toHaveLength(0); + // Each channel runs from its input pin to its output pin. + const channels = s.shapes.filter(t => t.shape === 'line' && t.classes.includes('flow-channel')) as Line[]; + const ins = s.pins.filter(p => p.of === 'operand'); + const outs = s.pins.filter(p => p.of === 'result'); + expect(channels, kind).toHaveLength(ins.length); + channels.forEach((c, k) => { + expect([c.x1, c.y1], `${kind} ${direction} ${k}`).toEqual([ins[k].x, ins[k].y]); + expect([c.x2, c.y2], `${kind} ${direction} ${k}`).toEqual([outs[k].x, outs[k].y]); + }); + // One arrow: the ready that comes back, the valid that goes over. + expect(s.shapes.filter(t => t.classes.includes('flow-fill')), kind).toHaveLength(1); + } + // The ready ends on a bar. The valid leaves the first channel at a dot. + expect(spec('no-ready', direction).shapes.filter(t => t.classes.includes('flow-bar'))).toHaveLength(1); + expect(spec('valid-merge', direction).shapes.filter(t => t.classes.includes('flow-dot'))).toHaveLength(1); + const merge = spec('valid-merge', direction); + const dot = merge.shapes.find(t => t.classes.includes('flow-dot')) as Extract; + const first = merge.pins.find(p => p.of === 'operand' && p.index === 0)!; + expect(direction === 'DOWN' ? dot.cx : dot.cy).toBe(direction === 'DOWN' ? first.x : first.y); + } + }); + + it('place the pins of a share from the operands its unit takes', () => { + // Two operations of two operands: four operands and the result of the + // unit come in, a result for each operation and two operands for the unit go out. + const pinsOf = (group: unknown, ins: number, outs: number) => FlowSymbols.spec({ symbol: 'share', group } as never, ins, outs, 'DOWN'); + const s = pinsOf(2, 5, 4)!; + const at = (of: string, index: number) => s.pins.find(p => p.of === of && p.index === index)!; + expect([0, 1, 2, 3].map(k => at('operand', k).side)).toEqual(['NORTH', 'NORTH', 'NORTH', 'NORTH']); + expect([0, 1].map(k => at('result', k).side)).toEqual(['SOUTH', 'SOUTH']); + // The result of an operation leaves under the middle of its operands. + expect(at('result', 0).x).toBeCloseTo((at('operand', 0).x + at('operand', 1).x) / 2); + expect(at('result', 1).x).toBeCloseTo((at('operand', 2).x + at('operand', 3).x) / 2); + // The unit hangs off the side: its operands leave first, its result comes back after them. + expect([at('result', 2), at('result', 3), at('operand', 4)].every(p => p.onSide)).toBe(true); + expect(at('result', 2).y).toBeLessThan(at('result', 3).y); + expect(at('result', 3).y).toBeLessThan(at('operand', 4).y); + // Three operations of one operand, and one operation of one. + expect(pinsOf(1, 4, 4)!.pins.filter(p => p.onSide)).toHaveLength(2); + expect(pinsOf(1, 2, 2)!.pins.filter(p => p.onSide)).toHaveLength(2); + // The same pins to the right, turned over. + const right = FlowSymbols.spec({ symbol: 'share', group: 2 }, 5, 4, 'RIGHT')!; + for (const p of s.pins) { + const q = right.pins.find(r => r.of === p.of && r.index === p.index)!; + expect([q.x, q.y, q.onSide], `${p.of} ${p.index}`).toEqual([p.y, p.x, p.onSide]); + } + // A ring where an operand leaves for the unit, a dot where its result comes back. + expect(s.shapes.filter(t => t.classes.includes('flow-ring'))).toHaveLength(2); + expect(s.shapes.filter(t => t.classes.includes('flow-dot'))).toHaveLength(1); + // Each operation is numbered where its operands come in and where its result leaves. + expect(s.shapes.filter(t => t.shape === 'text').map(t => (t as { text: string }).text)).toEqual(['0', '0', '1', '1']); + // Counts that are not n × k + 1 in and n + k out, or no group, draw no symbol. + expect(pinsOf(2, 6, 4)).toBeUndefined(); + expect(pinsOf(3, 5, 4)).toBeUndefined(); + expect(pinsOf(undefined, 5, 4)).toBeUndefined(); + expect(pinsOf(1.5, 5, 4)).toBeUndefined(); + expect(pinsOf('2', 5, 4)).toBeUndefined(); + }); + + it('keep the numbers of a share clear of its outline and of the pins of its unit', () => { + type Text = Extract; + type Circle = Extract; + // About how far a digit reaches from its middle, and the room it keeps. + const reach = (t: Text): number => t.size * 0.5; + for (const [group, n] of [[1, 1], [1, 5], [2, 3], [2, 4], [3, 2], [3, 3], [4, 2]]) { + for (const direction of DIRECTIONS) { + const s = FlowSymbols.spec({ symbol: 'share', group }, n * group + 1, n + group, direction)!; + const where = `${n} of ${group} ${direction}`; + const texts = s.shapes.filter(t => t.shape === 'text') as Text[]; + const marks = s.shapes.filter(t => t.shape === 'circle') as Circle[]; + const body = s.shapes.find(t => t.shape === 'polygon' && t.classes.includes('flow-body')) as Extract; + const points = body.points.split(' ').map(pt => pt.split(',').map(Number)); + expect(texts.map(t => t.text), where).toEqual(Array.from({ length: n }, (_, i) => [String(i), String(i)]).flat()); + expect(marks, where).toHaveLength(group + 1); + for (const t of texts) { + // No ring and no dot of the unit touches a number. + for (const m of marks) { + expect(Math.hypot(m.cx - t.x, m.cy - t.y), `${where} ${t.text}`).toBeGreaterThan(m.r + reach(t)); + } + // No slope of the outline cuts a number: where the + // outline crosses the line the number stands on, it is + // further out than the number reaches. + const along = direction === 'DOWN' ? 1 : 0, across = 1 - along; + const at = [t.x, t.y]; + const crossings: number[] = []; + points.forEach((a, i) => { + const b = points[(i + 1) % points.length]; + if ((a[along] - at[along]) * (b[along] - at[along]) < 0) { + crossings.push(a[across] + ((b[across] - a[across]) * (at[along] - a[along])) / (b[along] - a[along])); + } + }); + expect(crossings, `${where} ${t.text}`).toHaveLength(2); + expect(Math.min(...crossings) + reach(t), `${where} ${t.text}`).toBeLessThan(at[across]); + expect(Math.max(...crossings) - reach(t), `${where} ${t.text}`).toBeGreaterThan(at[across]); + } + } + } + }); + + it('draw a token that is a guess as a ring and one that is not as a dot, in every unit of a speculation', () => { + const count = (kind: FlowSymbolKind, cls: string) => spec(kind, 'DOWN').shapes.filter(t => t.shape === 'circle' && t.classes.includes(cls)).length; + for (const kind of ['speculator', 'spec-save', 'spec-commit', 'spec-save-commit', 'spec-branch', 'spec-tag'] as FlowSymbolKind[]) { + expect(spec(kind, 'DOWN').family, kind).toBe('spec'); + } + // A commit makes a dot of a ring. Its other dot is where the control comes in. + expect([count('spec-commit', 'flow-ring'), count('spec-commit', 'flow-dot')]).toEqual([1, 2]); + // A save keeps a dot, a save and commit a ring and a dot, each in a slot. + expect([count('spec-save', 'flow-ring'), count('spec-save', 'flow-dot')]).toEqual([0, 2]); + expect([count('spec-save-commit', 'flow-ring'), count('spec-save-commit', 'flow-dot')]).toEqual([1, 3]); + // A speculator gives a token that is a guess: a ring around a question mark, + // and a ring where each of its three answers leaves the side. + expect(count('speculator', 'flow-ring')).toBe(4); + expect(spec('speculator', 'DOWN').shapes.filter(t => t.shape === 'text').map(t => (t as { text: string }).text)).toEqual(['?']); + // A tag marks the tokens it passes as none: a dot, in the shape of a width change that widens. + expect([count('spec-tag', 'flow-ring'), count('spec-tag', 'flow-dot')]).toEqual([0, 1]); + const quad = (kind: FlowSymbolKind, extra = {}) => (spec(kind, 'DOWN', extra).shapes.find(t => t.shape === 'polygon') as { points: string }).points; + expect(quad('spec-tag')).toBe(quad('width', { variant: 'widen', to: '' })); + // The control of a save and of a commit comes in on the side, and the two of a save and commit. + for (const direction of DIRECTIONS) { + const side = direction === 'DOWN' ? 'EAST' : 'SOUTH'; + expect(spec('spec-save', direction).pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`)).toEqual([`operand 1 ${side}`]); + expect(spec('spec-commit', direction).pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`)).toEqual([`operand 1 ${side}`]); + expect(spec('spec-save-commit', direction).pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`)) + .toEqual([`operand 1 ${side}`, `operand 2 ${side}`]); + expect(spec('speculator', direction).pins.filter(p => p.onSide).map(p => `${p.of} ${p.index} ${p.side}`)) + .toEqual([1, 2, 3].map(k => `result ${k} ${side}`)); } + // A speculating branch sends the ring one way and the dot the other, where a branch writes T and F. + expect(spec('spec-branch', 'DOWN').shapes.filter(t => t.shape === 'text')).toHaveLength(0); + expect([count('spec-branch', 'flow-ring'), count('spec-branch', 'flow-dot')]).toEqual([1, 2]); + expect(spec('branch', 'DOWN').shapes.filter(t => t.shape === 'text').map(t => (t as { text: string }).text)).toEqual(['T', 'F']); }); it('draw a flow to the right as the flow down the page turned over', () => { @@ -358,7 +679,7 @@ describe('flow symbols', () => { }); it('draw no symbol for an unknown kind or for counts it cannot draw', () => { - expect(FlowSymbols.spec({ symbol: 'race' }, 2, 1, 'DOWN')).toBeUndefined(); + expect(FlowSymbols.spec({ symbol: 'no-such-kind' }, 2, 1, 'DOWN')).toBeUndefined(); expect(FlowSymbols.spec({ symbol: 'toString' }, 0, 0, 'DOWN')).toBeUndefined(); expect(FlowSymbols.spec({ symbol: 'mux' }, 1, 1, 'DOWN')).toBeUndefined(); expect(FlowSymbols.spec({ symbol: 'select' }, 2, 1, 'DOWN')).toBeUndefined(); diff --git a/packages/shared/src/flow-symbols.ts b/packages/shared/src/flow-symbols.ts index 9fdc8a15..e1368ef1 100644 --- a/packages/shared/src/flow-symbols.ts +++ b/packages/shared/src/flow-symbols.ts @@ -13,9 +13,11 @@ * A symbol is defined along its flow: `f` runs from the input edge (0) to the * output edge (F), `c` across it (0 to C). Inputs sit on the `f = 0` edge, * outputs on the `f = F` edge, and a side pin (a mux's select, a branch's - * condition, a control merge's index) halfway along the `c = C` edge. Down the - * page `f` is y, so the side pins are on the right. Across to the right `f` is - * x, and they are at the bottom. Text stays upright either way. + * condition, a control merge's index) halfway along the `c = C` edge. A symbol + * with several side pins (the signals that stop a stand-in, the unit a share + * serves) spreads them along that edge. Down the page `f` is y, so the side + * pins are on the right. Across to the right `f` is x, and they are at the + * bottom. Text stays upright either way. * * Every symbol fills one frame (`FRAME` along and across), so they stand the * same size in a diagram. A choice (a mux, a merge, a select) and the branch @@ -39,11 +41,16 @@ export type FlowSymbolKind = | 'end' | 'instance' | 'bundle' | 'unbundle' | 'load' | 'store' | 'memory' | 'load-store-queue' | 'ram' - | 'operator' | 'compare' | 'width' | 'not'; + | 'operator' | 'compare' | 'width' | 'not' + | 'first' | 'priority' | 'race' | 'share' + | 'tap' | 'stand-in' | 'stall' + | 'dead-buffer' + | 'no-ready' | 'valid-merge' + | 'speculator' | 'spec-save' | 'spec-commit' | 'spec-save-commit' | 'spec-branch' | 'spec-tag'; /** A family of symbols, drawn in one tint. */ export type FlowSymbolFamily = - | 'storage' | 'split' | 'choice' | 'branch' | 'token' | 'constant' | 'memory' | 'arith' | 'bundle' | 'terminal'; + | 'storage' | 'split' | 'choice' | 'branch' | 'token' | 'constant' | 'memory' | 'arith' | 'bundle' | 'terminal' | 'spec'; /** What a producer says about a node drawn as a flow symbol. */ export interface FlowDepiction { @@ -59,10 +66,18 @@ export interface FlowDepiction { slots?: number; /** Which signals a buffer registers: `dv` (data and valid), `r` (ready), `dvr`, or `none` (transparent). */ cut?: string; - /** A width change's way: `widen`, `narrow` or `convert`. */ + /** + * A width change's way: `widen`, `narrow` or `convert`. A tap that says so + * when the token is first offered, not when it passes: `at-valid`. + */ variant?: string; /** A width change's result type. */ to?: string; + /** + * How many pins form a group: the flags of a race (its last operands), + * the operands of the unit a share serves. + */ + group?: number; } export type FlowPortSide = 'NORTH' | 'SOUTH' | 'EAST' | 'WEST'; @@ -137,7 +152,12 @@ export namespace FlowSymbols { end: 'terminal', instance: 'terminal', bundle: 'bundle', unbundle: 'bundle', load: 'memory', store: 'memory', memory: 'memory', 'load-store-queue': 'memory', ram: 'memory', - operator: 'arith', compare: 'arith', width: 'arith', not: 'arith' + operator: 'arith', compare: 'arith', width: 'arith', not: 'arith', + first: 'choice', priority: 'choice', race: 'choice', share: 'choice', + tap: 'branch', 'stand-in': 'branch', stall: 'branch', + 'dead-buffer': 'storage', + 'no-ready': 'bundle', 'valid-merge': 'bundle', + speculator: 'spec', 'spec-save': 'spec', 'spec-commit': 'spec', 'spec-save-commit': 'spec', 'spec-branch': 'spec', 'spec-tag': 'spec' }; export const KINDS: readonly FlowSymbolKind[] = Object.keys(FAMILY) as FlowSymbolKind[]; @@ -153,14 +173,40 @@ export namespace FlowSymbols { end: [0, ANY, 0, 0], instance: [0, ANY, 0, ANY], bundle: [1, ANY, 1, ANY], unbundle: [1, ANY, 1, ANY], load: [2, 2, 2, 2], store: [2, 2, 2, 2], memory: [0, ANY, 0, ANY], 'load-store-queue': [0, ANY, 0, ANY], ram: [0, 0, 1, 1], - operator: [1, 2, 1, 1], compare: [2, 2, 1, 1], width: [1, 1, 1, 1], not: [1, 1, 1, 1] + operator: [1, 2, 1, 1], compare: [2, 2, 1, 1], width: [1, 1, 1, 1], not: [1, 1, 1, 1], + first: [1, ANY, 1, 1], priority: [2, 2, 1, 1], race: [1, ANY, 1, 1], share: [2, ANY, 2, ANY], + tap: [1, 1, 2, 2], 'stand-in': [2, ANY, 1, 1], stall: [1, 1, 1, 1], + 'dead-buffer': [1, 1, 0, 0], + 'no-ready': [1, 1, 1, 1], 'valid-merge': [2, 2, 2, 2], + speculator: [2, 2, 4, 4], 'spec-save': [2, 2, 1, 1], 'spec-commit': [2, 2, 1, 1], 'spec-save-commit': [3, 3, 1, 1], + 'spec-branch': [2, 2, 2, 2], 'spec-tag': [1, 1, 1, 1] }; + /** + * The operations a share serves and the operands each gives: `n` of `k`. + * A share takes the `k` operands of each operation and then the result of + * the unit they share, and gives a result to each operation and then the + * `k` operands it chose for the unit. Undefined when the counts are not + * such, or when the depiction does not say `k` (its `group`). + */ + function shared(d: FlowDepiction, operands: number, results: number): { n: number; k: number } | undefined { + const k = d.group; + if (typeof k !== 'number' || !Number.isInteger(k) || k < 1) { + return undefined; + } + const n = results - k; + return n >= 1 && n * k + 1 === operands ? { n, k } : undefined; + } + export function isKind(value: unknown): value is FlowSymbolKind { return typeof value === 'string' && Object.prototype.hasOwnProperty.call(FAMILY, value); } - /** Whether a kind draws this many operands and results. */ + /** + * Whether a kind draws this many operands and results. A share needs more + * than that: its counts must also follow from its `group`, which `spec` + * checks. + */ export function fits(kind: FlowSymbolKind, operands: number, results: number): boolean { const [inMin, inMax, outMin, outMax] = COUNTS[kind]; return operands >= inMin && operands <= inMax && results >= outMin && results <= outMax; @@ -177,6 +223,9 @@ export namespace FlowSymbols { if (!isKind(kind) || !fits(kind, operands, results)) { return undefined; } + if (kind === 'share' && !shared(depiction, operands, results)) { + return undefined; + } const down = direction !== 'RIGHT'; const def = define(kind, depiction, operands, results, down); const X = (f: number, c: number): number => SCALE * (down ? c : f); @@ -239,9 +288,10 @@ export namespace FlowSymbols { ring(f: number, c: number, r: number): void; arrow(f1: number, c1: number, f2: number, c2: number): void; glyph(f: number, c: number, text: string, size: number, mark?: string): void; + letter(f: number, c: number, text: string, size: number): void; value(f: number, c: number, text: string, size?: number): void; label(f: number, c: number, text: string, size?: number): void; - tiny(f: number, c: number, text: string): void; + tiny(f: number, c: number, text: string, anchor?: 'middle' | 'start'): void; } const P = PIN_PITCH; @@ -252,6 +302,13 @@ export namespace FlowSymbols { const across = (n: number, min: number): number => Math.max(min, 2 * M + (Math.max(n, 1) - 1) * P); /** Where pin `i` of a row of `n` sits across a symbol `C` wide, `pitch` apart. */ const at = (i: number, n: number, C: number, pitch = P): number => C / 2 + (i - (n - 1) / 2) * pitch; + /** + * How far along `n` pins on the side need, each at least `clear` from the + * ends of the body, and at least one frame. + */ + const alongSide = (n: number, clear = 3): number => Math.max(FRAME, 2 * (S + clear) + (Math.max(n, 1) - 1) * P); + /** Where side pin `j` of `n` sits along a symbol `F` long. */ + const sideAt = (j: number, n: number, F: number): number => F / 2 + (j - (n - 1) / 2) * P; /** The pitch of a row of `n` pins spread over the wide edge of a choice `C` across. */ const spread = (n: number, C: number): number => Math.max(P, C / (n + 1)); /** About how wide a text is drawn. */ @@ -417,7 +474,8 @@ export namespace FlowSymbols { } }; } - case 'branch': { + case 'branch': + case 'spec-branch': { const C = across(2, CHOICE_ACROSS), F = CHOICE_ALONG, narrow = C * 0.24; const out = (k: number): number => at(k, 2, C, spread(2, C)); const edge = (f: number): number => C - narrow + ((narrow - 1) * (f - S)) / (F - 2 * S); @@ -429,8 +487,15 @@ export namespace FlowSymbols { draw: g => { stubs(g, pins, F); g.poly([[S, narrow], [F - S, 1], [F - S, C - 1], [S, C - narrow]], 'flow-body'); - g.tiny(F - S - 4.5, out(0), 'T'); - g.tiny(F - S - 4.5, out(1), 'F'); + if (kind === 'spec-branch') { + // A token that is a guess leaves on the first + // output, one that is not on the second. + g.ring(F - S - 5, out(0), 2.6); + g.dot(F - S - 5, out(1), 2.6); + } else { + g.tiny(F - S - 4.5, out(0), 'T'); + g.tiny(F - S - 4.5, out(1), 'F'); + } g.line(condF, edge(condF), condF, C); g.dot(condF, edge(condF), 2.8); } @@ -665,6 +730,300 @@ export namespace FlowSymbols { } }; } + case 'first': + case 'race': { + // A decision among its inputs, in a rounded block that says + // which by its letter. The flags of a race, its last inputs, + // end in a dot as a condition does. + const C = across(ins, FRAME), F = FRAME; + // The dots of a race stand clear of the rounded corners. + const half = Math.max(10, ((ins - 1) * P) / 2 + (kind === 'race' ? 6 : 4)); + // A group that is no count of these operands draws no flag. + const flags = kind === 'race' && typeof d.group === 'number' && Number.isInteger(d.group) && d.group >= 0 && d.group <= ins + ? d.group : 0; + return { + F, C, pins: [...inputRow(ins, C), result(0, F, C / 2)], + draw: g => { + for (let k = 0; k < ins; k++) { + g.line(0, at(k, ins, C), S, at(k, ins, C)); + } + g.line(F - S, C / 2, F, C / 2); + g.rect(S, C / 2 - half, F - S, C / 2 + half, 'round', 'flow-body'); + for (let k = ins - flags; k < ins; k++) { + g.dot(S, at(k, ins, C), 2.4); + } + g.letter(F / 2, C / 2, kind === 'first' ? 'F' : 'R', 13); + } + }; + } + case 'priority': { + // The rounded block of a decision, with a P. Its two inputs + // are told apart by name at the edge, beside their pins: under + // them down the page, after them to the right. The names take + // a row of the block, so it is a little longer than a frame + // and the letter keeps its size. + const C = across(2, FRAME), F = FRAME + 4, half = 13; + const names = ['hi', 'lo']; + return { + F, C, pins: [...inputRow(2, C), result(0, F, C / 2)], + draw: g => { + for (let k = 0; k < 2; k++) { + g.line(0, at(k, 2, C), S, at(k, 2, C)); + } + g.line(F - S, C / 2, F, C / 2); + g.rect(S, C / 2 - half, F - S, C / 2 + half, 'round', 'flow-body'); + for (let k = 0; k < 2; k++) { + if (down) { + g.tiny(S + 4.8, at(k, 2, C), names[k]); + } else { + g.tiny(S + 2.4, at(k, 2, C), names[k], 'start'); + } + } + g.letter(S + 16.4, C / 2, 'P', 13); + } + }; + } + case 'share': { + // The operands of each operation narrow to the one unit they + // share, which hangs off the side at the waist, and its result + // widens back to them: a mux over a branch. + const { n, k } = shared(d, ins, outs)!; + const inputs = n * k; + // A band at each end, as wide as the body, holds the numbers of + // the operations. The unit hangs off the slopes between the + // bands, so no number meets a slope or a pin of the unit. + const band = 9; + const C = across(Math.max(inputs, 2), CHOICE_ACROSS), F = Math.max(56, 2 * (S + band + 3) + k * P); + const waist = C * 0.15; + const inC = (i: number): number => at(i, inputs, C, spread(inputs, C)); + // An operation's result leaves under the middle of its operands. + const opC = (i: number): number => (inC(i * k) + inC(i * k + k - 1)) / 2; + // The far edge of the body at f. + const slope = F / 2 - S - band; + const edge = (f: number): number => C / 2 + waist + (C / 2 - 1 - waist) * Math.min(1, Math.abs(f - F / 2) / slope); + const pins: PinAt[] = []; + for (let i = 0; i < inputs; i++) { + pins.push(operand(i, 0, inC(i))); + } + pins.push(operand(inputs, sideAt(k, k + 1, F), C, true)); + for (let i = 0; i < n; i++) { + pins.push(result(i, F, opC(i))); + } + for (let j = 0; j < k; j++) { + pins.push(result(n + j, sideAt(j, k + 1, F), C, true)); + } + return { + F, C, pins, + draw: g => { + stubs(g, pins, F); + g.poly([ + [S, 1], [S + band, 1], [F / 2, C / 2 - waist], [F - S - band, 1], [F - S, 1], + [F - S, C - 1], [F - S - band, C - 1], [F / 2, C / 2 + waist], [S + band, C - 1], [S, C - 1] + ], 'flow-body'); + for (let i = 0; i < n; i++) { + g.tiny(S + band / 2, opC(i), String(i)); + g.tiny(F - S - band / 2, opC(i), String(i)); + } + // To the unit through a ring, back from it through a dot. + for (let j = 0; j <= k; j++) { + const f = sideAt(j, k + 1, F); + g.line(f, edge(f), f, C); + if (j < k) { + g.ring(f, edge(f), 2.6); + } else { + g.dot(f, edge(f), 2.6); + } + } + } + }; + } + case 'tap': { + // A pass that says on its side when a token has passed. One + // that says it when the token is first offered taps the base. + const C = FRAME, F = FRAME; + const tapF = d.variant === 'at-valid' ? S + 3 : F / 2; + const edge = (f: number): number => C / 2 + 10 * (1 - (f - S) / (F - 2 * S)); + return { + F, C, pins: [operand(0, 0, C / 2), result(0, F, C / 2), result(1, tapF, C, true)], + draw: g => { + g.line(0, C / 2, S, C / 2); + g.poly([[S, C / 2 - 10], [F - S, C / 2], [S, C / 2 + 10]], 'flow-body'); + g.line(F - S, C / 2, F, C / 2); + g.line(tapF, edge(tapF), tapF, C); + g.ring(tapF, edge(tapF), 2.6); + } + }; + } + case 'stand-in': { + // A pass that answers zero for a producer that was stopped: + // the signals that stop it come in on the side. One ends in a + // dot on the body. Several meet on a rail beside it, which + // ends in that same dot. + const kills = ins - 1; + const C = FRAME, F = alongSide(kills); + // The triangle is a frame long however many signals there are. + const tip = FRAME - S, mid = FRAME / 2, rail = C - 3; + const edge = (f: number): number => C / 2 + 10 * (1 - (f - S) / (tip - S)); + const pins = [ + operand(0, 0, C / 2), + ...Array.from({ length: kills }, (_, j) => operand(1 + j, sideAt(j, kills, F), C, true)), + result(0, F, C / 2) + ]; + return { + F, C, pins, + draw: g => { + g.line(0, C / 2, S, C / 2); + g.poly([[S, C / 2 - 10], [tip, C / 2], [S, C / 2 + 10]], 'flow-body'); + g.line(tip, C / 2, F, C / 2); + if (kills === 1) { + g.line(mid, edge(mid), mid, C); + } else { + // Several signals meet on a rail beside the body, + // and the rail stops the pass where one would. + for (let j = 0; j < kills; j++) { + g.line(sideAt(j, kills, F), rail, sideAt(j, kills, F), C); + } + g.line(sideAt(0, kills, F), rail, sideAt(kills - 1, kills, F), rail); + g.line(mid, edge(mid), mid, rail); + } + g.dot(mid, edge(mid), 2.4); + g.value(S + 5, C / 2, '0', 8); + } + }; + } + case 'stall': { + // A wire that holds a token back at random: a die on the wire. + const C = FRAME, F = FRAME; + return { + F, C, pins: [operand(0, 0, C / 2), result(0, F, C / 2)], + draw: g => { + g.line(0, C / 2, S + 1, C / 2); + g.line(F - S - 1, C / 2, F, C / 2); + g.rect(S + 1, C / 2 - 9, F - S - 1, C / 2 + 9, 'round', 'flow-body'); + for (const [df, dc] of [[0, 0], [-4.6, -4.6], [4.6, 4.6], [-4.6, 4.6], [4.6, -4.6]]) { + g.dot(F / 2 + df, C / 2 + dc, 1.6); + } + } + }; + } + case 'dead-buffer': { + // A buffer with nothing after it: it keeps the one token it + // takes and takes no other. + const C = FRAME, F = FRAME, c0 = C / 2 - 9, c1 = C / 2 + 9; + return { + F, C, pins: [operand(0, 0, C / 2)], + draw: g => { + g.line(0, C / 2, S, C / 2); + g.rect(S, c0, F - S, c1, 'square', 'flow-body'); + g.line(S + 4, c0 + 3.5, F - S - 4, c1 - 3.5); + g.line(S + 4, c1 - 3.5, F - S - 4, c0 + 3.5); + } + }; + } + case 'no-ready': { + // A channel whose ready goes no further back: it comes off the + // channel at the output and ends on a bar. + const C = FRAME, F = FRAME, back = C / 2 + 9, stop = F / 2 - 5; + return { + F, C, pins: [operand(0, 0, C / 2), result(0, F, C / 2)], + draw: g => { + g.line(0, C / 2, F, C / 2, 'flow-channel'); + g.line(F - 6, C / 2, F - 6, back); + g.arrow(F - 6, back, stop + 3, back); + g.bar(stop - 1.4, back - 5, stop + 1.4, back + 5); + } + }; + } + case 'valid-merge': { + // Two channels side by side: the valid of the first is the + // valid of both. + const C = across(2, FRAME), F = FRAME, first = at(0, 2, C), second = at(1, 2, C); + return { + F, C, pins: [operand(0, 0, first), operand(1, 0, second), result(0, F, first), result(1, F, second)], + draw: g => { + g.line(0, first, F, first, 'flow-channel'); + g.line(0, second, F, second, 'flow-channel'); + g.arrow(F / 2, first, F / 2, second - 3.6); + g.dot(F / 2, first, 3.2); + } + }; + } + case 'speculator': { + // The unit that guesses: it gives a token before the real one + // has come, and tells the units around it on its side whether + // the guess held. + const C = across(2, FRAME), F = alongSide(3, 5), c0 = C / 2 - 11, c1 = C / 2 + 11; + const pins = [ + ...inputRow(2, C), + result(0, F, C / 2), + ...Array.from({ length: 3 }, (_, j) => result(1 + j, sideAt(j, 3, F), C, true)) + ]; + return { + F, C, pins, + draw: g => { + stubs(g, pins, F); + g.rect(S, c0, F - S, c1, 'square', 'flow-body'); + for (let j = 0; j < 3; j++) { + g.line(sideAt(j, 3, F), c1, sideAt(j, 3, F), C); + g.ring(sideAt(j, 3, F), c1, 2.4); + } + g.ring(F / 2, C / 2, 6.5); + g.glyph(F / 2, C / 2, '?', 8.5); + } + }; + } + case 'spec-save': + case 'spec-commit': + case 'spec-save-commit': { + // A token that is a guess is a ring, one that is not a dot. A + // commit makes the one of the other. A save keeps a copy, in a + // slot. A save and commit keeps both sorts. + const ctrls = ins - 1; + const C = FRAME, F = alongSide(ctrls, 5), c0 = C / 2 - 11, c1 = C / 2 + 11; + const pins = [ + operand(0, 0, C / 2), + ...Array.from({ length: ctrls }, (_, j) => operand(1 + j, sideAt(j, ctrls, F), C, true)), + result(0, F, C / 2) + ]; + return { + F, C, pins, + draw: g => { + stubs(g, pins, F); + g.rect(S, c0, F - S, c1, 'square', 'flow-body'); + for (let j = 0; j < ctrls; j++) { + g.line(sideAt(j, ctrls, F), c1, sideAt(j, ctrls, F), C); + g.dot(sideAt(j, ctrls, F), c1, 2.4); + } + if (kind === 'spec-commit') { + g.line(S + 5, C / 2, F - S - 5, C / 2); + g.ring(S + 5.5, C / 2, 3); + g.dot(F - S - 5.5, C / 2, 3); + } else if (kind === 'spec-save') { + g.rect(F / 2 - 5.5, C / 2 - 5.5, F / 2 + 5.5, C / 2 + 5.5, 'square', 'flow-line', 'flow-thin'); + g.dot(F / 2, C / 2, 3); + } else { + g.rect(F / 2 - 5.5, C / 2 - 9, F / 2 + 5.5, C / 2 + 9, 'square', 'flow-line', 'flow-thin'); + g.ring(F / 2, C / 2 - 4, 2.8); + g.dot(F / 2, C / 2 + 4, 2.8); + } + } + }; + } + case 'spec-tag': { + // The channel gets the bit that says whether a token is a + // guess, and its tokens are marked as none: it widens, as a + // width change does, around a dot. + const C = FRAME, F = FRAME; + return { + F, C, pins: [operand(0, 0, C / 2), result(0, F, C / 2)], + draw: g => { + g.line(0, C / 2, S - 2, C / 2); + g.line(F - S + 2, C / 2, F, C / 2); + g.poly([[S - 2, 9], [F - S + 2, 3], [F - S + 2, C - 3], [S - 2, C - 9]], 'flow-body'); + g.dot(F / 2, C / 2, 3); + } + }; + } case 'operator': { const glyph = textOf(d.glyph, '?'); const n = ins, C = across(n, FRAME), F = FRAME, R = 12; @@ -761,6 +1120,12 @@ export namespace FlowSymbols { ring: (f, c, r) => g.circle(f, c, r, 'flow-ring'), arrow: (f1, c1, f2, c2) => { g.line(f1, c1, f2, c2); + if (f1 === f2) { + // Across the flow, from one channel to the one beside it. + const over = Math.sign(c2 - c1); + g.poly([[f2, c2 + over * 1.5], [f2 - 3, c2 - over * 3.5], [f2 + 3, c2 - over * 3.5]], 'flow-fill'); + return; + } const way = Math.sign(f2 - f1); g.poly([[f2 + way * 1.5, c2], [f2 - way * 3.5, c2 - 3], [f2 - way * 3.5, c2 + 3]], 'flow-fill'); }, @@ -781,9 +1146,10 @@ export namespace FlowSymbols { text(x + (size * 0.3 + 1.6) * k, y + (size * 0.3 + 1.4) * k, mark, 6.5 * k, 'start', 'detail', 'flow-mark'); } }, + letter: (f, c, value, size) => text(X(f, c), Y(f, c), value, size * SCALE, 'middle', 'name', 'flow-glyph', 'flow-italic'), value: (f, c, value, size = 10) => text(X(f, c), Y(f, c), value, size * SCALE, 'middle', 'name', 'flow-value'), label: (f, c, value, size = 10) => text(X(f, c), Y(f, c), value, size * SCALE, 'middle', 'name', 'flow-label'), - tiny: (f, c, value) => text(X(f, c), Y(f, c), value, 6.5 * SCALE, 'middle', 'detail', 'flow-tiny') + tiny: (f, c, value, anchor = 'middle') => text(X(f, c), Y(f, c), value, 6.5 * SCALE, anchor, 'detail', 'flow-tiny') }; return g; }