aboutsummaryrefslogtreecommitdiff
path: root/static/panes/gccdump-view.js
blob: b98d9d1d39094e65eb52d222bfae6ca1095e3655 (plain)
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
// Copyright (c) 2017, Marc Poulhiès - Kalray Inc.
// Copyright (c) 2021, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

'use strict';

var FontScale = require('../widgets/fontscale').FontScale;
var monaco = require('monaco-editor');
var Toggles = require('../widgets/toggles').Toggles;
require('../modes/gccdump-rtl-gimple-mode');
var _ = require('underscore');
var $ = require('jquery');
var ga = require('../analytics').ga;
var monacoConfig = require('../monaco-config');
var TomSelect = require('tom-select');
var utils = require('../utils');
var PaneRenaming = require('../widgets/pane-renaming').PaneRenaming;


function GccDump(hub, container, state) {
    this.container = container;
    this.eventHub = hub.createEventHub();
    this.domRoot = container.getElement();
    this.domRoot.html($('#gccdump').html());
    var root = this.domRoot.find('.monaco-placeholder');

    this.gccDumpEditor = monaco.editor.create(root[0], monacoConfig.extendConfig({
        readOnly: true,
        glyphMargin: true,
        lineNumbersMinChars: 3,
        dropdownParent: 'body',
    }));

    this.initButtons(state);

    var gccdump_picker = this.domRoot[0].querySelector('.gccdump-pass-picker');
    this.selectize = new TomSelect(gccdump_picker, {
        sortField: -1, // do not sort
        valueField: 'name',
        labelField: 'name',
        searchField: ['name'],
        options: [],
        items: [],
        plugins: ['input_autogrow'],
        maxOptions: 500,
    });

    // this is used to save internal state.
    this.state = {};

    this.state._compilerid = state._compilerid;
    this.state._editorid = state._editorid;
    this.state._treeid = state._treeid;
    this._compilerName = state._compilerName;

    this.awaitingInitialResults = false;
    this.selection = state.selection;

    this.paneRenaming = new PaneRenaming(this, state);

    this.initCallbacks();

    if (state && state.selectedPass) {
        this.state.selectedPass = state.selectedPass;

        // To keep URL format stable wrt GccDump, only a string of the form 'r.expand' is stored.
        // Old links also have the pass number prefixed but this can be ignored.
        // Create the object that will be used instead of this bare string.
        var selectedPassRe = /[0-9]*(i|t|r)\.([\w-_]*)/;
        var passType = {
            i: 'ipa',
            r: 'rtl',
            t: 'tree',
        };
        var match = state.selectedPass.match(selectedPassRe);
        var selectedPassO = {
            filename_suffix: match[1] + '.' + match[2],
            name: match[2] + ' (' + passType[match[1]] + ')',
            command_prefix: '-fdump-' + passType[match[1]] + '-' + match[2],
        };

        this.eventHub.emit('gccDumpPassSelected', this.state._compilerid, selectedPassO, false);
    }

    // until we get our first result from compilation backend with all fields,
    // disable UI callbacks.
    this.uiIsReady = false;
    this.onUiNotReady();

    this.eventHub.emit('gccDumpFiltersChanged', this.state._compilerid, this.getEffectiveFilters(), false);

    this.updateButtons();
    this.saveState();

    // UI is ready, request compilation to get passes list and
    // current output (if any)
    this.eventHub.emit('gccDumpUIInit', this.state._compilerid);
    ga.proxy('send', {
        hitType: 'event',
        eventCategory: 'OpenViewPane',
        eventAction: 'GccDump',
    });
}

GccDump.prototype.initButtons = function (state) {
    this.filters = new Toggles(this.domRoot.find('.dump-filters'), state);
    this.fontScale = new FontScale(this.domRoot, state, this.gccDumpEditor);

    this.topBar = this.domRoot.find('.top-bar');
    this.dumpFiltersButtons = this.domRoot.find('.dump-filters .btn');

    this.dumpTreesButton = this.domRoot.find('[data-bind=\'treeDump\']');
    this.dumpTreesTitle = this.dumpTreesButton.prop('title');

    this.dumpRtlButton = this.domRoot.find('[data-bind=\'rtlDump\']');
    this.dumpRtlTitle = this.dumpRtlButton.prop('title');

    this.dumpIpaButton = this.domRoot.find('[data-bind=\'ipaDump\']');
    this.dumpIpaTitle = this.dumpIpaButton.prop('title');

    this.optionAddressButton = this.domRoot.find('[data-bind=\'addressOption\']');
    this.optionAddressTitle = this.optionAddressButton.prop('title');

    this.optionSlimButton = this.domRoot.find('[data-bind=\'slimOption\']');
    this.optionSlimTitle = this.optionSlimButton.prop('title');

    this.optionRawButton = this.domRoot.find('[data-bind=\'rawOption\']');
    this.optionRawTitle = this.optionRawButton.prop('title');

    this.optionDetailsButton = this.domRoot.find('[data-bind=\'detailsOption\']');
    this.optionDetailsTitle = this.optionDetailsButton.prop('title');

    this.optionStatsButton = this.domRoot.find('[data-bind=\'statsOption\']');
    this.optionStatsTitle = this.optionStatsButton.prop('title');

    this.optionBlocksButton = this.domRoot.find('[data-bind=\'blocksOption\']');
    this.optionBlocksTitle = this.optionBlocksButton.prop('title');

    this.optionVopsButton = this.domRoot.find('[data-bind=\'vopsOption\']');
    this.optionVopsTitle = this.optionVopsButton.prop('title');

    this.optionLinenoButton = this.domRoot.find('[data-bind=\'linenoOption\']');
    this.optionLinenoTitle = this.optionLinenoButton.prop('title');

    this.optionUidButton = this.domRoot.find('[data-bind=\'uidOption\']');
    this.optionUidTitle = this.optionUidButton.prop('title');

    this.optionAllButton = this.domRoot.find('[data-bind=\'allOption\']');
    this.optionAllTitle = this.optionAllButton.prop('title');

    this.hideable = this.domRoot.find('.hideable');
};

GccDump.prototype.initCallbacks = function () {
    this.filters.on('change', _.bind(this.onFilterChange, this));
    this.selectize.on('change', _.bind(this.onPassSelect, this));

    this.fontScale.on('change', _.bind(this.saveState, this));
    this.paneRenaming.on('renamePane', this.saveState.bind(this));

    this.eventHub.on('compileResult', this.onCompileResult, this);
    this.eventHub.on('compiler', this.onCompiler, this);
    this.eventHub.on('compilerClose', this.onCompilerClose, this);
    this.eventHub.on('settingsChange', this.onSettingsChange, this);

    this.eventHub.emit('gccDumpViewOpened', this.state._compilerid);
    this.eventHub.emit('requestSettings');
    this.container.on('destroy', this.close, this);

    this.container.on('resize', this.resize, this);
    this.container.on('shown', this.resize, this);

    this.cursorSelectionThrottledFunction =
        _.throttle(_.bind(this.onDidChangeCursorSelection, this), 500);
    this.gccDumpEditor.onDidChangeCursorSelection(_.bind(function (e) {
        this.cursorSelectionThrottledFunction(e);
    }, this));
};

GccDump.prototype.updateButtons = function () {
    var formatButtonTitle = function (button, title) {
        button.prop('title', '[' + (button.hasClass('active') ? 'ON' : 'OFF') + '] ' + title);
    };
    formatButtonTitle(this.dumpTreesButton, this.dumpTreesTitle);
    formatButtonTitle(this.dumpRtlButton, this.dumpRtlTitle);
    formatButtonTitle(this.dumpIpaButton, this.dumpIpaTitle);
    formatButtonTitle(this.optionAddressButton, this.optionAddressTitle);
    formatButtonTitle(this.optionSlimButton, this.optionSlimTitle);
    formatButtonTitle(this.optionRawButton, this.optionRawTitle);
    formatButtonTitle(this.optionDetailsButton, this.optionDetailsTitle);
    formatButtonTitle(this.optionStatsButton, this.optionStatsTitle);
    formatButtonTitle(this.optionBlocksButton, this.optionBlocksTitle);
    formatButtonTitle(this.optionVopsButton, this.optionVopsTitle);
    formatButtonTitle(this.optionLinenoButton, this.optionLinenoTitle);
    formatButtonTitle(this.optionUidButton, this.optionUidTitle);
    formatButtonTitle(this.optionAllButton, this.optionAllTitle);
};

// Disable view's menu when invalid compiler has been
// selected after view is opened.
GccDump.prototype.onUiNotReady = function () {
    // disable drop down menu and buttons
    this.selectize.disable();
    this.dumpFiltersButtons.prop('disabled', true);
};

GccDump.prototype.onUiReady = function () {
    // enable drop down menu and buttons
    this.selectize.enable();

    this.dumpFiltersButtons.prop('disabled', false);
};

GccDump.prototype.onPassSelect = function (passId) {
    var selectedPass = this.selectize.options[passId];

    if (this.inhibitPassSelect !== true) {
        this.eventHub.emit('gccDumpPassSelected', this.state._compilerid, selectedPass, true);
    }

    // To keep shared URL compatible, we keep on storing only a string in the
    // state and stick to the original format.
    // Previously, we were simply storing the full file suffix (the part after [...]):
    //    [file.c.]123t.expand
    // We don't have the number now, but we can store the file suffix without this number
    // (the number is useless and should probably have never been there in the
    // first place).

    this.state.selectedPass = selectedPass.filename_suffix;
    this.saveState();
};

GccDump.prototype.resize = function () {
    var topBarHeight = utils.updateAndCalcTopBarHeight(this.domRoot, this.topBar, this.hideable);
    this.gccDumpEditor.layout({
        width: this.domRoot.width(),
        height: this.domRoot.height() - topBarHeight,
    });
};

// Called after result from new compilation received
// if gccDumpOutput is false, cleans the select menu
GccDump.prototype.updatePass = function (filters, selectize, gccDumpOutput) {
    var passes = gccDumpOutput ? gccDumpOutput.all : [];

    // we are changing selectize but don't want any callback to
    // trigger new compilation
    this.inhibitPassSelect = true;

    selectize.clear(true);
    selectize.clearOptions(true);

    _.each(passes, function (p) {
        selectize.addOption(p);
    }, this);

    if (gccDumpOutput.selectedPass)
        selectize.addItem(gccDumpOutput.selectedPass.name, true);
    else
        selectize.clear(true);

    this.eventHub.emit('gccDumpPassSelected', this.state._compilerid, gccDumpOutput.selectedPass, false);

    this.inhibitPassSelect = false;
};

GccDump.prototype.onCompileResult = function (id, compiler, result) {
    if (this.state._compilerid !== id || !compiler) return;

    if (result.gccDumpOutput && result.gccDumpOutput.syntaxHighlight) {
        monaco.editor.setModelLanguage(this.gccDumpEditor.getModel(), 'gccdump-rtl-gimple');
    } else {
        monaco.editor.setModelLanguage(this.gccDumpEditor.getModel(), 'plaintext');
    }
    if (compiler.supportsGccDump && result.gccDumpOutput) {
        var currOutput = result.gccDumpOutput.currentPassOutput;

        // if result contains empty selected pass, probably means
        // we requested an invalid/outdated pass.
        if (!result.gccDumpOutput.selectedPass) {
            this.selectize.clear(true);
            this.state.selectedPass = null;
        }
        this.updatePass(this.filters, this.selectize, result.gccDumpOutput);
        this.showGccDumpResults(currOutput);

        // enable UI on first successful compilation or after an invalid compiler selection (eg. clang)
        if (!this.uiIsReady) {
            this.uiIsReady = true;
            this.onUiReady();
        }
    } else {
        this.selectize.clear(true);
        this.state.selectedPass = null;
        this.updatePass(this.filters, this.selectize, false);
        this.uiIsReady = false;
        this.onUiNotReady();
        if (!compiler.supportsGccDump) {
            this.showGccDumpResults('<Tree/RTL output is not supported for this compiler (GCC only)>');
        } else {
            this.showGccDumpResults('<Tree/RTL output is empty>');
        }
    }
    this.saveState();
};

GccDump.prototype.getDefaultPaneName = function () {
    return 'GCC Tree/RTL Viewer';
};

GccDump.prototype.getPaneTag = function () {
    if(this.state._editorid !== false) {
        return this._compilerName
                + ' (Editor #' + this.state._editorid + ', Compiler #' + this.state._compilerid + ')';
    } else {
        return this._compilerName
                + ' (Tree #' + this.state._treeid + ', Compiler #' + this.state._compilerid + ')';
    }
};

GccDump.prototype.getPaneName = function () {
    return this.paneName ? this.paneName : this.getDefaultPaneName() + ' ' + this.getPaneTag();
};

GccDump.prototype.updateTitle = function () {
    this.container.setTitle(_.escape(this.getPaneName()));
};

GccDump.prototype.showGccDumpResults = function (results) {
    this.gccDumpEditor.setValue(results);

    if (!this.awaitingInitialResults) {
        if (this.selection) {
            this.gccDumpEditor.setSelection(this.selection);
            this.gccDumpEditor.revealLinesInCenter(this.selection.startLineNumber,
                this.selection.endLineNumber);
        }
        this.awaitingInitialResults = true;
    }
};

GccDump.prototype.onCompiler = function (id, compiler, options, editorid, treeid) {
    if (id === this.state._compilerid) {
        this._compilerName = compiler ? compiler.name : '';
        this.state._editorid = editorid;
        this.state._treeid = treeid;
        this.updateTitle();
    }
};

GccDump.prototype.onCompilerClose = function (id) {
    if (id === this.state._compilerid) {
        // We can't immediately close as an outer loop somewhere in GoldenLayout is iterating over
        // the hierarchy. We can't modify while it's being iterated over.
        this.close();
        _.defer(function (self) {
            self.container.close();
        }, this);
    }
};

GccDump.prototype.getEffectiveFilters = function () {
    return this.filters.get();
};

GccDump.prototype.onFilterChange = function () {
    this.saveState();
    this.updateButtons();

    if (this.inhibitPassSelect !== true) {
        this.eventHub.emit('gccDumpFiltersChanged', this.state._compilerid, this.getEffectiveFilters(), true);
    }
};

GccDump.prototype.saveState = function () {
    var state = this.currentState();
    this.container.setState(state);
    this.fontScale.addState(state);
};

GccDump.prototype.currentState = function () {
    var filters = this.getEffectiveFilters();
    var state =  {
        _compilerid: this.state._compilerid,
        _editorid: this.state._editorid,
        _treeid: this.state._treeid,
        selectedPass: this.state.selectedPass,
        treeDump: filters.treeDump,
        rtlDump: filters.rtlDump,
        ipaDump: filters.ipaDump,
        addressOption: filters.addressOption,
        slimOption: filters.slimOption,
        rawOption: filters.rawOption,
        detailsOption: filters.detailsOption,
        statsOption: filters.statsOption,
        blocksOption: filters.blocksOption,
        vopsOption: filters.vopsOption,
        linenoOption: filters.linenoOption,
        uidOption: filters.uidOption,
        allOption: filters.allOption,
        selection: this.selection,
    };
    this.paneRenaming.addState(state);
    return state;
};

GccDump.prototype.onSettingsChange = function (newSettings) {
    this.gccDumpEditor.updateOptions({
        contextmenu: newSettings.useCustomContextMenu,
        minimap: {
            enabled: newSettings.showMinimap,
        },
        fontFamily: newSettings.editorsFFont,
        fontLigatures: newSettings.editorsFLigatures,
    });
};

GccDump.prototype.onDidChangeCursorSelection = function (e) {
    if (this.awaitingInitialResults) {
        this.selection = e.selection;
        this.saveState();
    }
};

GccDump.prototype.close = function () {
    this.eventHub.unsubscribe();
    this.eventHub.emit('gccDumpViewClosed', this.state._compilerid);
    this.gccDumpEditor.dispose();
};

module.exports = {
    GccDump: GccDump,
};