mirror of
https://github.com/compiler-explorer/compiler-explorer.git
synced 2026-12-02 02:09:27 -06:00
Compilers like to encode short-ish strings into immediate arguments to various assembly instructions. This makes it hard to read the assembly. To improve readability, show a string representation of a number’s bytes when they’re valid UTF-8. This now also shows a string representation for *unprintable* ASCII characters, such as `10` being shown as `"\n"` or `1` as `"\u0001"`. Resolves #6220. ## Examples ([link](https://godbolt.org/z/7hzn6b8YG)) * 8583909746840200552: `8'583'909'746'840'200'552 = 0x7720'2C6F'6C6C'6568 = 6.5188685003648344e+265 = "hello, w"` * 1684828783: `1'684'828'783 = 0x646C'726F = 1.74467096e+22f = "orld"` * -6934491452449512253: `-6'934'491'452'449'512'253 = 0x9FC3'BCC3'B6C3'A4C3 = -1.1500622354593239e-155 = "äöüß"` * 1633837924: `1'633'837'924 = 0x6162'6364 = 2.61007876e+20f = "dcba"` * 97: `97 = 0x61 = 1.35925951e-43f = "a"` * 10: `10 = 0xA = 1.40129846e-44f = "\n"` * 92: `92 = 0x5C = 1.28919459e-43f = "\\"` ## Open questions * The code assumes little-endian encoding, so the string representation is reversed compared to the numeric representation (see `integer` example). Should this be configurable or should the bytes not be reversed? * Negative numbers are masked to 64-bit unsigned ints (same as in the hex representation), so if the number is shorter than 8 bytes, it has some leading `ff` bytes and is not valid UTF-8 (see `small_negative_number` in the example link). Is this an acceptable limitation? * I'd like to add some tests for `getNumericToolTip`, but I’m not really familiar with TypeScript. I tried to importing `static/panes/compiler.ts` in a test file, but ([after](https://github.com/vitest-dev/vitest/discussions/1806#discussioncomment-3570047) some [struggling](https://vitest.dev/config/#environment)) it seems that that file needs a [specific DOM element](https://github.com/compiler-explorer/compiler-explorer/blob/3c26c64ca320853cb383b3751d3e08a86b581639/static/options.ts#L27) that is not present during testing. The simplest solution I could come up with is moving `getNumericToolTip` into another file (such as `static/utils.ts`). Is that okay or is is it possible test `static/panes/compiler.ts` some other way? --------- Co-authored-by: Patrick Quist <partouf@gmail.com>
163 lines
6.2 KiB
TypeScript
163 lines
6.2 KiB
TypeScript
// 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.
|
|
|
|
import bigInt from 'big-integer';
|
|
import {addDigitSeparator} from '../shared/common-utils.js';
|
|
|
|
export function updateAndCalcTopBarHeight(domRoot: JQuery, topBar: JQuery, hideable: JQuery): number {
|
|
let topBarHeight = 0;
|
|
if (!topBar.hasClass('d-none')) {
|
|
hideable.show();
|
|
const topBarHeightMax = topBar.outerHeight(true) ?? 0;
|
|
hideable.hide();
|
|
const topBarHeightMin = topBar.outerHeight(true) ?? 0;
|
|
topBarHeight = topBarHeightMin;
|
|
if (topBarHeightMin === topBarHeightMax) {
|
|
hideable.show();
|
|
}
|
|
}
|
|
return topBarHeight;
|
|
}
|
|
|
|
/**
|
|
* Subscribe and unsubscribe the event listener.
|
|
*
|
|
* @param {JQuery} element
|
|
* @param {string} eventName
|
|
* @param {(event:JQuery.Event)=>void} callback
|
|
* @returns void
|
|
*/
|
|
export function toggleEventListener(element: JQuery, eventName: string, callback: (event: JQuery.Event) => void): void {
|
|
element.on(eventName, (event: JQuery.Event) => {
|
|
callback(event);
|
|
element.off(eventName);
|
|
});
|
|
}
|
|
|
|
export function formatDateTimeWithSpaces(d: Date) {
|
|
const t = x => x.slice(-2);
|
|
// Hopefully some day we can use the temporal api to make this less of a pain
|
|
return (
|
|
`${d.getFullYear()} ${t('0' + (d.getMonth() + 1))} ${t('0' + d.getDate())}` +
|
|
`${t('0' + d.getHours())} ${t('0' + d.getMinutes())} ${t('0' + d.getSeconds())}`
|
|
);
|
|
}
|
|
|
|
export function formatISODate(dt: Date, full = false) {
|
|
const month = '' + (dt.getUTCMonth() + 1);
|
|
const day = '' + dt.getUTCDate();
|
|
const hrs = '' + dt.getUTCHours();
|
|
const min = '' + dt.getUTCMinutes();
|
|
const today = new Date(Date.now());
|
|
if (full || dt.toDateString() === today.toDateString()) {
|
|
return (
|
|
dt.getUTCFullYear() +
|
|
'-' +
|
|
month.padStart(2, '0') +
|
|
'-' +
|
|
day.padStart(2, '0') +
|
|
' ' +
|
|
hrs.padStart(2, '0') +
|
|
':' +
|
|
min.padStart(2, '0')
|
|
);
|
|
} else {
|
|
return dt.getUTCFullYear() + '-' + month.padStart(2, '0') + '-' + day.padStart(2, '0');
|
|
}
|
|
}
|
|
|
|
const hexLike = /^(#?[$]|0x)([0-9a-fA-F]+)$/;
|
|
const hexLike2 = /^(#?)([0-9a-fA-F]+)H$/;
|
|
const decimalLike = /^(#?)(-?[0-9]+)$/;
|
|
const ptxFloat32 = /^0[fF]([0-9a-fA-F]{8})$/;
|
|
const ptxFloat64 = /^0[dD]([0-9a-fA-F]{16})$/;
|
|
|
|
function parseNumericValue(value: string): bigInt.BigInteger | null {
|
|
const hexMatch = hexLike.exec(value) || hexLike2.exec(value);
|
|
if (hexMatch) return bigInt(hexMatch[2], 16);
|
|
|
|
const hexMatchPTX = ptxFloat32.exec(value) ?? ptxFloat64.exec(value);
|
|
if (hexMatchPTX) return bigInt(hexMatchPTX[1], 16);
|
|
|
|
const decMatch = decimalLike.exec(value);
|
|
if (decMatch) return bigInt(decMatch[2]);
|
|
|
|
return null;
|
|
}
|
|
|
|
export function getNumericToolTip(value: string, digitSeparator?: string): string | null {
|
|
const formatNumber = (number, base, chunkSize) => {
|
|
const numberString = number.toString(base).toUpperCase();
|
|
if (digitSeparator !== undefined) {
|
|
return addDigitSeparator(numberString, digitSeparator, chunkSize);
|
|
} else {
|
|
return numberString;
|
|
}
|
|
};
|
|
const numericValue = parseNumericValue(value);
|
|
if (numericValue === null) return null;
|
|
|
|
// PTX floats
|
|
const view = new DataView(new ArrayBuffer(8));
|
|
view.setBigUint64(0, BigInt(numericValue.toString()), true);
|
|
if (ptxFloat32.test(value)) return view.getFloat32(0, true).toPrecision(9) + 'f';
|
|
if (ptxFloat64.test(value)) return view.getFloat64(0, true).toPrecision(17);
|
|
|
|
// Decimal representation.
|
|
let result = formatNumber(numericValue, 10, 3);
|
|
|
|
// Hexadecimal representation.
|
|
if (numericValue.isNegative()) {
|
|
const masked = bigInt('ffffffffffffffff', 16).and(numericValue);
|
|
result += ' = 0x' + formatNumber(masked, 16, 4);
|
|
} else {
|
|
result += ' = 0x' + formatNumber(numericValue, 16, 4);
|
|
}
|
|
|
|
// Float32/64 representation.
|
|
view.setBigUint64(0, BigInt(numericValue.toString()), true);
|
|
if (numericValue.bitLength().lesserOrEquals(32)) result += ' = ' + view.getFloat32(0, true).toPrecision(9) + 'f';
|
|
// only subnormal doubles and zero may have upper 32 bits all 0, assume unlikely to be double
|
|
else result += ' = ' + view.getFloat64(0, true).toPrecision(17);
|
|
|
|
// Printable UTF-8 characters.
|
|
const bytes = numericValue.isNegative()
|
|
? // bytes of negative number without sign extension
|
|
numericValue
|
|
.add(1)
|
|
.toArray(256)
|
|
.value.map(byte => byte ^ 0xff)
|
|
: numericValue.toArray(256).value;
|
|
// This assumes that `numericValue` is encoded as little-endian.
|
|
bytes.reverse();
|
|
const decoder = new TextDecoder('utf-8', {fatal: true});
|
|
try {
|
|
result += ' = ' + JSON.stringify(decoder.decode(Uint8Array.from(bytes)));
|
|
} catch (e) {
|
|
// ignore `TypeError` when the number is not valid UTF-8
|
|
}
|
|
|
|
return result;
|
|
}
|