From f4d0eb01f1d0bbefafb988aaac476fa81ce241f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Csord=C3=A1s?= Date: Wed, 20 Nov 2019 22:02:26 +0100 Subject: [PATCH] Jump to labels in assembly - Parse the labels asynchronously when a new assembly result is set to the compiler view. - `Ctrl + click` on a label will bring us to the label definition. Create new keyboard and mouse events to catch this. - Create a new context sensitive option in the context menu. If no label can be found in the clicked position, the menu option will not be shown. - Select the range of the label definition when jumping to a label. --- static/panes/compiler.js | 133 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/static/panes/compiler.js b/static/panes/compiler.js index 358df4087..bf2695f1f 100644 --- a/static/panes/compiler.js +++ b/static/panes/compiler.js @@ -94,6 +94,7 @@ function Compiler(hub, container, state) { this.wantOptInfo = state.wantOptInfo; this.decorations = {}; this.prevDecorations = []; + this.labels = {}; this.alertSystem = new Alert(); this.alertSystem.prefixMessage = "Compiler #" + this.id + ": "; @@ -103,6 +104,33 @@ function Compiler(hub, container, state) { this.linkedFadeTimeoutId = -1; this.toolsMenu = null; + var labelUsageInstructions = [ + "call", + "callq", + "jmp", + "je", "jz", + "jne", "jnz", + "jg", "jnle", + "jge", "jnl", + "jl", "jnge", + "jle", "jng", + "ja", "jnbe", + "jae", "jnb", + "jb", "jnae", + "jbe", "jna", + "jxcz", + "jc", + "jnc", + "jo", + "jno", + "jp", "jpe", + "jnp", "jpo", + "js", + "jns" + ]; + this.labelUsageRegex = new RegExp( + '\\s+(' + labelUsageInstructions.join('|') + ')\\s+([^#]+)'); + this.initButtons(state); this.outputEditor = monaco.editor.create(this.monacoPlaceholder[0], { @@ -324,7 +352,72 @@ Compiler.prototype.resize = function () { }); }; +// Returns a label name if it can be found in the given position, otherwise +// returns null. +Compiler.prototype.getLabelAtPosition = function (position) { + var line = this.outputEditor.getModel().getLineContent(position.lineNumber); + var match = line.match(this.labelUsageRegex); + if (match) { + return match[2].trim(); + } + + return null; +}; + +// Jumps to a label definition related to a label which was found in the +// given position and highlights the given range. If no label can be found in +// the given positon it do nothing. +Compiler.prototype.jumpToLabel = function (position) { + var label = this.getLabelAtPosition(position); + + if (!label) { + return; + } + + var range = this.labels[label]; + if (!range) { + return; + } + + // Highlight the new range. + var endLineContent = + this.outputEditor.getModel().getLineContent(range.endLineNumber); + + this.outputEditor.setSelection(new monaco.Selection( + range.startLineNumber, 0, + range.endLineNumber, endLineContent.length + 1)); + + // Jump to the given line. + this.outputEditor.revealLineInCenter(range.startLineNumber); +}; + Compiler.prototype.initEditorActions = function () { + this.isLabelCtxKey = this.outputEditor.createContextKey('isLabel', true); + + this.outputEditor.addAction({ + id: 'jumptolabel', + label: 'Jump to label', + keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter], + precondition: 'isLabel', + contextMenuGroupId: 'navigation', + contextMenuOrder: 1.5, + run: _.bind(function (ed) { + this.jumpToLabel(ed.getPosition()); + }, this) + }); + + // Hiding the 'Jump to label' context menu option if no label can be found + // in the clicked position. + var contextmenu = this.outputEditor.getContribution('editor.contrib.contextmenu'); + var realMethod = contextmenu._onContextMenu; + contextmenu._onContextMenu = _.bind(function (e) { + if (this.isLabelCtxKey && e.target.position) { + var label = this.getLabelAtPosition(e.target.position); + this.isLabelCtxKey.set(label && this.labels[label]); + } + realMethod.apply(contextmenu, arguments); + }, this); + this.outputEditor.addAction({ id: 'viewsource', label: 'Scroll to source', @@ -560,6 +653,33 @@ Compiler.prototype.setAssembly = function (asm) { this.outputEditor.revealLine(currentTopLine); } + // Set the labels which will be used at Jump to label feature. + setTimeout(_.bind(function () { + this.labels = {}; + for (var i = 0; i < this.assembly.length; ++i) { + var text = this.assembly[i].text; + if (/^\S/.test(text)) { + var match = text.match(/(^\S+[^#]*):/); + if (match) { + var startLineNumber = i; + + // Find the end of the label definition. + while (i + 1 < this.assembly.length && + /^[\s#]/.test(this.assembly[i + 1].text) + ) { + i = i + 1; + } + + this.labels[match[1]] = { + startLineNumber: startLineNumber + 1, + endLineNumber: i + 1 + }; + } + } + } + }, this), 0); + + this.outputEditor.revealLine(currentTopLine); if (this.getEffectiveFilters().binary) { this.setBinaryMargin(); if (this.codeLensProvider !== null) { @@ -1195,6 +1315,11 @@ Compiler.prototype.initCallbacks = function () { this.cursorSelectionThrottledFunction(e); }, this)); + this.mouseUpThrottledFunction = _.throttle(_.bind(this.onMouseUp, this), 50); + this.outputEditor.onMouseUp(_.bind(function (e) { + this.mouseUpThrottledFunction(e); + }, this)); + this.compileClearCache.on('click', _.bind(function () { this.compilerService.cache.reset(); this.compile(true); @@ -1527,6 +1652,14 @@ Compiler.prototype.onDidChangeCursorSelection = function (e) { } }; +Compiler.prototype.onMouseUp = function (e) { + if (e === null || e.target === null || e.target.position === null) return; + + if (e.event.ctrlKey && e.event.leftButton) { + this.jumpToLabel(e.target.position); + } +}; + Compiler.prototype.onMouseMove = function (e) { if (e === null || e.target === null || e.target.position === null) return; if (this.settings.hoverShowSource === true && this.assembly) {