Files
compiler-explorer/static/assembly-syntax.ts
Sean Garwood bb3a5d26d0 Fix AT&T warning showing for native langs w/ undefined intelAsm (#8576)
Fixes #8567 

Fix bug where `asmSyntax` returned 'att' for compilers that do not
define
`intelAsm` (e.g. Rust, Spice), but that do support Intel:

- The base compiler defaults `supportsIntel` property to
`!!this.compiler.intelAsm`
- Aforementioned compilers override this in [optionsForFilter
override](6b1ff666f2/lib/compilers/rust.ts (L272))

Before:

```typescript
// bug
const isAtt = /*...*/ !(this.filters.isSet('intel') && this.compiler.intelAsm.includes('intel'));
/*                            1                                 0                           */
/*                                                   0                                      */
/*                               !0 => 1                                                    */
```

Removing the check for intelAsm resolves this.

- Extracted the logic that determines the assembly syntax and added unit
tests
  that cover all possible inputs.
- Verified with local Rust.

## Miscellany

- **rm handling of 'last operand'**
- **fix: order-agnostic refCardinalityOfSrcDstOperands**
- Marked TODO: only show warnings where relevant, i.e. if the
cardinality of
src/dst operands is not shown in tooltip text but is shown in context
menu,
then the warning should only display in the context menu. Beyond scope
of
    this PR.
- **optional supportsIntel arg, test undefined supportsIntel**

<!-- THIS COMMENT IS INVISIBLE IN THE FINAL PR, BUT FEEL FREE TO REMOVE
IT
Thanks for taking the time to improve CE. We really appreciate it.
Before opening the PR, please make sure that the tests & linter pass
their checks,
  by running `make check`.
In the best case scenario, you are also adding tests to back up your
changes,
  but don't sweat it if you don't. We can discuss them at a later date.
Feel free to append your name to the CONTRIBUTORS.md file
Thanks again, we really appreciate this!
-->
2026-04-14 20:49:30 -05:00

57 lines
2.8 KiB
TypeScript

// Copyright (c) 2025, 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 {type AssemblyInstructionInfo} from '../types/assembly-docs.interfaces.js';
const AssemblySyntaxesList = ['att', 'intel'] as const;
export type AssemblySyntax = (typeof AssemblySyntaxesList)[number];
export const ATT_SYNTAX_WARNING = 'WARNING: The information shown pertains to Intel syntax.';
const CARDINALITY_REGEX = /\b(?:first|second|third|fourth)\b/i;
const OPERAND_REGEX = /\boperands?\b/i;
const SOURCE_DEST_REGEX = /\b(?:source|destination)\b/i;
export function addAttSyntaxWarningIfNeeded(
data: AssemblyInstructionInfo,
syntax: AssemblySyntax,
): AssemblyInstructionInfo {
if (syntax !== 'att') return data;
const referencesCardinalityOfSrcDstOperands = (text: string): boolean =>
CARDINALITY_REGEX.test(text) && OPERAND_REGEX.test(text) && SOURCE_DEST_REGEX.test(text);
const tooltipRefs = referencesCardinalityOfSrcDstOperands(data.tooltip);
const htmlRefs = referencesCardinalityOfSrcDstOperands(data.html);
if (!tooltipRefs && !htmlRefs) return data;
return {
...data,
...(tooltipRefs && {tooltip: '***' + ATT_SYNTAX_WARNING + '***\n\n' + data.tooltip}),
...(htmlRefs && {html: '<b><em>' + ATT_SYNTAX_WARNING + '</em></b><br><br>' + data.html}),
};
}
export function determineAssemblySyntax(supportsIntel: boolean = false, intelFilterEnabled: boolean): AssemblySyntax {
return supportsIntel && !intelFilterEnabled ? 'att' : 'intel';
}