154 Commits

Author SHA1 Message Date
Fangrui Song
e9de1ee9f5 MC: Move useCodeAlign from MCSection to MCAsmInfo
To centralize assembly-related virtual functions to MCAsmInfo and move
toward making MCSection non-virtual.
2025-07-26 11:34:10 -07:00
Fangrui Song
e878b7e349 MCParsedAsmOperand::print: Add MCAsmInfo parameter
so that subclasses can provide the appropriate MCAsmInfo to print
MCExpr objects.

At present, llvm/utils/TableGen/AsmMatcherEmitter.cpp constucts a
generic MCAsmInfo.
2025-06-28 12:05:33 -07:00
Fangrui Song
e015626f18 MC: Allow .set to reassign non-MCConstantExpr expressions
GNU Assembler supports symbol reassignment via .set, .equ, or =.
However, LLVM's integrated assembler only allows reassignment for
MCConstantExpr cases, as it struggles with scenarios like:

```
.data
.set x, 0
.long x         // reference the first instance
x = .-.data
.long x         // reference the second instance
.set x,.-.data
.long x         // reference the third instance
```

Between two assignments binds, we cannot ensure that a reference binds
to the earlier assignment. We use MCSymbol::IsUsed and other conditions
to reject potentially unsafe reassignments, but certain MCConstantExpr
uses could be unsafe as well.

This patch enables reassignment by cloning the symbol upon reassignment
and updating the symbol table. Existing references to the original
symbol remain unchanged, and the original symbol is excluded from the
emitted symbol table.
2025-05-26 21:58:18 -07:00
Fangrui Song
a0901a2f87 Replace #include MCAsmLexer.h with AsmLexer.h
MCAsmLexer.h has been made a forwarder header since #134207
2025-05-25 11:57:29 -07:00
Kazu Hirata
2faa45c63e
[MC] Remove unused includes (NFC) (#141321)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-05-24 00:00:04 -07:00
Eric Astor
692f832b19
Revert "[ms] [llvm-ml] Implement support for PROC NEAR/FAR" (#138353)
Reverts llvm/llvm-project#131707 - apparently it had gotten into a bad
state, and will need relanding.
2025-05-02 18:16:59 -04:00
Eric Astor
9b4f747827
[ms] [llvm-ml] Implement support for PROC NEAR/FAR (#131707)
Matches ML.EXE by translating "ret" instructions inside a `PROC FAR` to "retf", and automatically prepending a `push cs` to all near calls to a `PROC FAR`.
2025-05-02 18:09:43 -04:00
Eric Astor
0ab330b344
[ms] [llvm-ml] Add support for @CatStr built-in function symbol (#130781)
MASM supports some built-in macro-type functions.

We start our support for these with `@CatStr`, one of the more commonly used.
2025-04-24 12:16:16 -04:00
Kazu Hirata
c4e9901b5b
[llvm] Use llvm::append_range (NFC) (#135931) 2025-04-16 12:28:47 -07:00
Fangrui Song
ca5b3a0f51 [MC] Remove SetUsed on isUndefined and getFragment
Due to the known limitations of .set reassignment (see
https://sourceware.org/PR288), we use diagnostics to reject patterns
that could lead to errors (ae7ac010594f693fdf7b3ab879e196428d961e75 2009-06)).

This code gets refined multiple times, see:

* 9b4a824217f1fe23f83045afe7521acb791bc2d0 (2010-05) `IsUsedInExpr`
* 46c79ef1132607aead144dfda0f26aa8b065214f (2010-11) renamed `IsUsedInExpr` to `IsUsed`

The related `SetUsed` bit seems unnecessary nowadays.
2025-04-13 00:53:29 -07:00
Fangrui Song
b6a9618301 [MCParser] Rename confusing variable names
https://reviews.llvm.org/D24047 added `IsAtStartOfStatement` to
MCAsmLexer, while its subclass AsmLexer had a variable of the same name.
The assignment in `UnLex` is unnecessary, which is now removed.

60b403e75cd25a0c76aaaf4e6b176923acf49443 (2019) named the result
`parseStatement` `Parsed`. `HasError` is a clearer name.
2025-04-04 21:04:06 -07:00
Fangrui Song
b6e2df54c4 [MC] Move some member variables from AsmParser to MCAsmParser
to eliminate some virtual functions and avoid duplication
between AsmParser/MasmParser.
2025-04-02 09:59:18 -07:00
Fangrui Song
a30f102253 [llvm-ml] Remove unused function after #132750 2025-03-24 09:53:08 -07:00
Eric Astor
e2c5b95da1
[ms] [llvm-ml] Remove space-separated argument support (#132750)
This leads to errors when parsing MASM macro calls, and was retained
from AsmParser by mistake.

Fixes #132074
2025-03-24 12:41:33 -04:00
Eric Astor
d48a36f583
[ms] [llvm-ml] Allow optional parenthesized arguments for macros (#129905)
We match ML64.EXE, which allows optional parentheses around a macro's arguments.
2025-03-11 10:32:21 -04:00
Fangrui Song
fe56c4c019 [MC] Remove unneeded VK_None argument from MCSymbolRefExpr::create. NFC 2025-03-05 23:14:04 -08:00
Fangrui Song
b02cfbd73c [llvm-ml] Remove unused VariantKind parsing code 2025-03-02 15:54:45 -08:00
Fangrui Song
18e09da255 [Mips] Rework relocation expression parsing
A relocation expression might be used in an immediate operand or a
memory offset. https://reviews.llvm.org/D23110 , which intended to
generalize chained relocation operators (%hi(%neg(%gp_rel(x)))),
inappropriated introduced intrusive changes to the generic code. This
patch drops the intrusive changes and significantly simplifies the code.
The new style is similar to pre-D23110 but much cleaner.

Some weird expressions allowed by gas are not supported for simplicity,
e.g. "%lo foo", "(%lo(foo))", "%lo(foo)+1".
"(%lo(foo))", while previously parsed, is not used in practice.
"%lo(foo)+1" and "%lo(2*4)+foo" were previously parsed but would lead to
an error anyway as the expression is not relocatable
(`evaluateSymbolicAdd` does not fold the Add when RefKind are
different).
2025-03-02 11:27:15 -08:00
Fangrui Song
b6d5fa05ad [llvm-ml] Remove unused DWARF/Mach-O/ARM If-Then functions 2025-03-01 17:41:32 -08:00
Fangrui Song
43c3014ec1 [llvm-ml] Remove unused parser functions 2025-03-01 17:19:39 -08:00
Fangrui Song
077497d180 [MCParser] Remove parseParenExprOfDepth
Introduced by http://reviews.llvm.org/D9742 as a hack, which then became
unneeded.

Primary test: llvm/test/MC/Mips/memory-offsets.s
2025-03-01 16:52:45 -08:00
Eric Astor
a94226f9e6
[llvm-ml] Remove unsafe getCurrentSegmentOnly() call (#123355)
This call was made unsafe recently, but was not fixed in
db48f1a1764023f8efeb055e343b967d1eb37d19 (the commit that fixed the
parallel code in AsmParser.cpp).

Fixes #123189
2025-01-24 10:30:10 -05:00
Jay Foad
6cb1847815 Fix typo "necessarilly" 2024-11-14 17:01:17 +00:00
Daniel Sanders
74003f11b3
[mc] Add CFI directive to emit val_offset() rules (#113971)
These specify that the value of the given register in the previous frame
is the CFA plus some offset. This isn't very common but can be necessary
if the original value is normally reconstructed from the stack/frame
pointer instead of being saved on the stack and reloaded from there.
2024-11-11 11:38:36 -08:00
Fangrui Song
facdae62b7 [MCInstPrinter] Make printRegName non-const
Similar to printInst. printRegName may change states (e.g. #113834).
2024-10-29 19:14:54 -07:00
Kazu Hirata
5bc673d371
[MCParser] Avoid repeated hash lookups (NFC) (#110204) 2024-09-28 10:05:18 -07:00
Craig Topper
605420e0a5 [MC] Use MCRegister and remove implicit casts from MCRegister to unsigned. NFC 2024-09-20 11:33:47 -07:00
Jay Foad
e03f427196
[LLVM] Use {} instead of std::nullopt to initialize empty ArrayRef (#109133)
It is almost always simpler to use {} instead of std::nullopt to
initialize an empty ArrayRef. This patch changes all occurrences I could
find in LLVM itself. In future the ArrayRef(std::nullopt_t) constructor
could be deprecated or removed.
2024-09-19 16:16:38 +01:00
Lei Huang
4b524088a8
[NFC] Update function names in MCTargetAsmParser.h (#108643)
Update function names to adhere to LLVM coding standard.
2024-09-18 11:43:49 -04:00
Kazu Hirata
71eebe9daa
[llvm] Prefer StringRef::substr to StringRef::slice (NFC) (#106190)
S.substr(N, M) is simpler than S.slice(N, N + M).  Also, substr is
probably better recognizable than slice thanks to
std::string_view::substr.
2024-08-27 06:46:20 -07:00
Dmitriy Chestnykh
345861b186
[MC] Optimize loops in MC (#98114)
https://llvm.org/docs/CodingStandards.html tells us that we should avoid
evaluating `.end()` each time if possible.
2024-07-12 09:48:49 +02:00
Youngsuk Kim
82f9a5ba96 [llvm] Avoid 'raw_string_ostream::str' (NFC)
Since `raw_string_ostream` doesn't own the string buffer, it is
desirable (in terms of memory safety) for users to directly reference
the string buffer rather than use `raw_string_ostream::str()`.

Work towards TODO comment to remove `raw_string_ostream::str()`.
2024-07-03 06:37:48 -05:00
aengelke
c1a7c5ac73
[MC] Eliminate two symbol-related hash maps (#95464)
Previously, a symbol insertion requires (at least) three hash table
operations:

- Lookup/create entry in Symbols (main symbol table)
- Lookup NextUniqueID to deduplicate identical temporary labels
- Add entry to UsedNames, which is also used to serve as storage for the
symbol name in the MCSymbol.

All three lookups are done with the same name, so combining these into a
single table reduces the number of lookups to one. Thus, a pointer to a
symbol table entry can be passed to createSymbol to avoid a duplicate
lookup of the same name.

The new symbol table entry value is placed in a separate header to avoid
including MCContext in MCSymbol or vice versa.
2024-06-20 11:36:11 +02:00
Kazu Hirata
7c6d0d26b1
[llvm] Use llvm::unique (NFC) (#95628) 2024-06-14 22:49:36 -07:00
Kazu Hirata
586ecdf205
[llvm] Use StringRef::{starts,ends}_with (NFC) (#74956)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-11 21:01:36 -08:00
Sergei Barannikov
a479be0f39 [MC] Change tryParseRegister to return ParseStatus (NFC)
This finishes the work of replacing OperandMatchResultTy with
ParseStatus, started in D154101.
As a drive-by change, rename some RegNo variables to just Reg
(a leftover from the days when RegNo had 'unsigned' type).
2023-09-06 10:28:12 +03:00
Sergei Barannikov
af20c1c129 [MC] Add three-state parseDirective as a replacement for ParseDirective
Conventionally, parsing methods return false on success and true on
error. However, directive parsing methods need a third state: the
directive is not target specific. AsmParser::parseStatement detected
this case by using a fragile heuristic: if the target parser did not
consume any tokens, the directive is assumed to be not target-specific.

Some targets fail to follow the convention: they return success after
emitting an error or do not consume the entire line and return failure
on successful parsing. This was partially worked around by checking for
pending errors in parseStatement.

This patch tries to improve the situation by introducing parseDirective
method that returns ParseStatus -- three-state class. The new method
should eventually replace the old one returning bool.

ParseStatus is intentionally implicitly constructible from bool to allow
uses like `return Error(Loc, "message")`. It also has a potential to
replace OperandMatchResulTy as it is more convenient to use due to the
implicit construction from bool and more type safe.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D154101
2023-07-01 04:33:28 +03:00
Fangrui Song
665ccc19d3 [MC] Add SMLoc to MCCFIInstruction
to help debug and report better diagnostics for functions like
relaxDwarfCallFrameFragment (D153167).

In MCStreamer, some emitCFI* functions already take a SMLoc argument. Add a
SMLoc argument to the remaining functions that generate a MCCFIInstruction.
2023-06-26 17:58:29 -07:00
Kazu Hirata
88ba56c5b8 Revert "[MCParser] Modernize CppHashInfoTy (NFC)"
This reverts commit c0f9d7b0e6199ca3bde5a6d0036a8ada0bb6253b.

A build error has been reported with gcc-7.

This patch fixes:

https://github.com/llvm/llvm-project/issues/63240
2023-06-10 13:34:31 -07:00
Kazu Hirata
c0f9d7b0e6 [MCParser] Modernize CppHashInfoTy (NFC) 2023-06-10 09:02:26 -07:00
Fangrui Song
e416515115 MC: Use parseEOL
The diagnostics have changed from "unexpected token" to clearer "expected newline"
2023-04-28 00:52:33 -07:00
Wang, Xin10
76cc949212 Clean come dead code
These codes deleted are dead code, we never go into it.
1. In AggressiveAntiDepBreaker.cpp, have assert AntiDepReg != 0.
2. IfConversion.cpp, Kind can only be one unique value, so isFalse && isRev
    can never be true.
3. DAGCombiner.cpp, at line 3675, we have considered the condition like
```
  // fold (sub x, c) -> (add x, -c)
  if (N1C) {
    return DAG.getNode(ISD::ADD, DL, VT, N0,
                       DAG.getConstant(-N1C->getAPIntValue(), DL, VT));
  }
```
4. ScheduleDAGSDNodes.cpp, we have Latency > 1 at line 663
5. MasmParser.cpp, code exists in a switch-case block which decided by
    the value FirstTokenKind, at line 1621, FirstTokenKind could only be
    one of AsmToken::Dollar, AsmToken::At and AsmToken::Identifier.

Reviewed By: skan

Differential Revision: https://reviews.llvm.org/D148610
2023-04-23 20:46:34 -04:00
Luo, Yuanke
b63b77a1e0 [Coverity] Add assert for assumption. 2023-04-17 08:52:46 +08:00
Kazu Hirata
b595eb83e5 [llvm] Use *{Set,Map}::contains (NFC) 2023-03-14 18:56:07 -07:00
Chris Cotter
6e3d12951d [llvm][NFC] Use move instead of copy
Summary: For functions that accept an rvalue reference type
parameter, use move to avoid copying the parameter.

These were found when implementing CppCoreGuideline F.18 in
clang-tidy.

Committed on behalf of ccotter (Chris Cotter)

Reviewers: Michael137 thieta

Differential Revision: https://reviews.llvm.org/D142825
2023-02-01 17:40:27 +00:00
Sergei Barannikov
1799a714a7 [llvm-ml] Disallow '{' and '}' at the start of a statement
'{', '}' and (conditionally) '*' were allowed at the start of a
statement. This behavior was copied from AsmParser, where they were
added to support Hexagon bundles (braces) and BFP memory instructions
(the star).
MASM dialect is x86-specific and does not allow these symbols to be
used at the beginning of an instruction.

Worth noting that '{' is a valid first character in AT&T syntax
(e.g. "{vex} vpmadd52huq ..."); MASM variant is to omit the braces.

Reviewed By: epastor

Differential Revision: https://reviews.llvm.org/D142321
2023-01-25 20:44:00 +03:00
Jay Foad
073401e59c [MC] Define and use MCInstrDesc implicit_uses and implicit_defs. NFC.
The new methods return a range for easier iteration. Use them everywhere
instead of getImplicitUses, getNumImplicitUses, getImplicitDefs and
getNumImplicitDefs. A future patch will remove the old methods.

In some use cases the new methods are less efficient because they always
have to scan the whole uses/defs array to count its length, but that
will be fixed in a future patch by storing the number of implicit
uses/defs explicitly in MCInstrDesc. At that point there will be no need
to 0-terminate the arrays.

Differential Revision: https://reviews.llvm.org/D142215
2023-01-23 14:44:58 +00:00
Jay Foad
768aed1378 [MC] Make more use of MCInstrDesc::operands. NFC.
Change MCInstrDesc::operands to return an ArrayRef so we can easily use
it everywhere instead of the (IMHO ugly) opInfo_begin and opInfo_end.
A future patch will remove opInfo_begin and opInfo_end.

Also use it instead of raw access to the OpInfo pointer. A future patch
will remove this pointer.

Differential Revision: https://reviews.llvm.org/D142213
2023-01-23 11:31:41 +00:00
Sergei Barannikov
b47cdc6ce8 [MC] Replace single-case switch with an if (NFC)
Same as e5f746e9 but for MasmParser.
2023-01-23 04:09:07 +03:00
Mike Hommey
a0b7bd4b07 [MC] [llvm-ml] Add support for the extrn keyword
It is the same as the already supported `extern` keyword.
https://learn.microsoft.com/en-us/cpp/assembler/masm/extrn?view=msvc-170

Fixes: https://github.com/llvm/llvm-project/issues/59712

Reviewed By: epastor

Differential Revision: https://reviews.llvm.org/D140679
2022-12-29 14:47:24 +00:00