826 Commits

Author SHA1 Message Date
Fangrui Song
56600c11ad MCAsmInfo: replace HLASM-specific variables with IsHLASM
HLASM is very different from the gas syntax. We don't expect other
targets to customize the differences. Unify the numerous variables.
2024-12-24 18:37:46 -08:00
Aleksei Vetrov
4a5f82b43b
[MC] Fix DWARF file table for files with empty DWARF (#119572)
Update root file in DWARF file/line table as soon as we see the first
"#line" directive.

This was moved from "enabledGenDwarfForAssembly", which is called right
before we emit DWARF information. But if the file is empty or contains
expressions that doesn't need DWARF, it is never called, leaving an
original root file and not the file in the "#line" directive.

Add a test checking for this case.

This is reapply of #119229 with the following fix:

"MCContext::setMCLineTableRootFile" has the effect of adding
".debug_line" section to the output, even if DWARF generation is
disabled. Add a check and a test for this case.

Fixes: #119020
Fixes: #119229
2024-12-12 08:51:52 -08:00
David Blaikie
0d59fc2761
Revert "[MC] Fix DWARF file table for files with empty DWARF (#119020)" (#119486)
Reverts llvm/llvm-project#119229

Causes debug info to be unconditionally emitted, regardless of whether
it's requested.
2024-12-10 18:14:53 -08:00
Aleksei Vetrov
5041d06730
[MC] Fix DWARF file table for files with empty DWARF (#119020) (#119229)
Update root file in DWARF file/line table as soon as we see the first
"#line" directive.

This was moved from "enabledGenDwarfForAssembly", which is called right
before we emit DWARF information. But if the file is empty or contains
expressions that doesn't need DWARF, it is never called, leaving an
original root file and not the file in the "#line" directive.

Add a test checking for this case.

Fixes: #119020
2024-12-10 09:29:25 -08:00
abhishek-kaushik22
d20731ce6b
[CGData][GlobalIsel][Legalizer][DAG][MC][AsmParser][X86][AMX] Use std::move to avoid copy (#118068) 2024-12-06 09:46:15 +08:00
Janek van Oirschot
bd9145c8c2
Reapply [AMDGPU] Avoid resource propagation for recursion through multiple functions (#112251)
I was wrong last patch. I viewed the `Visited` set purely as a possible
recursion deterrent where functions calling a callee multiple times are
handled elsewhere. This doesn't consider cases where a function is
called multiple times by different callers still part of the same call
graph. New test shows the aforementioned case.

Reapplies #111004, fixes #115562.
2024-11-15 18:40:05 +00: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
Daniel Paoliello
8ae39c8e34
[MC] Fix llvm-mc unterminated string constants warning for Windows (#112995)
#98060 introduced a warning for unterminated string constants, however
it was only checking for `\n` which means that it produced strange
results on Windows (always blaming column 1) including having the
[associated test
fail](https://buildkite.com/llvm-project/github-pull-requests/builds/111277#0192a122-c5c9-4e4e-bc5b-7532fec99ae4)
if Git happened to use Windows newlines when creating the file.

This fix for this is to detect both `\r` and `\n`, but don't double-warn
for Windows newlines.
2024-10-21 10:02: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
alx32
28646d0cc1
[MC] Add .loc_label instruction (#99710)
As discussed in [the
RFC](https://discourse.llvm.org/t/rfc-extending-llvm-mc-loc-directive-with-labeling-support/79608)
we need a way to create labels in the assembler-generated line section
in order to support the future addition of the
[DW_AT_LLVM_stmt_sequence](https://discourse.llvm.org/t/rfc-new-dwarf-attribute-for-symbolication-of-merged-functions/79434)
attribute.

We have a similar precedent for such behavior with the
[.cfi_label](https://github.com/llvm/llvm-project/pull/97922)
instruction - so we add the `.loc_label THE_LABEL_NAME` instruction
which:
- Terminates the current line sequence in the line section
- Creates a new label with the specified label name in the `.debug_line`
section
2024-09-20 06:32:34 -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
Fangrui Song
e9f6deaa8f [MCAsmParser] .irpc: correctly handle quoted string
The quotes should be stripped.

Improve the test to check all of Identifier, Integer, and String.

GNU assembler also supports `.irpc c,"" a` but there is no real world
use case. So don't implement it.
2024-08-13 19:22:31 -07:00
Sam Elliott
ea222be0d9
[MC] Honour alignment directive fill value for non-intel (#100136)
As reported in https://llvm.org/PR30955, `.balign` with a fill-value of 0 did
not actually align using zeroes, on non-x86 targets.

This is because the check of whether to use the code alignment routines
or whether to just use the fill value was checking whether the fill
value was equal to `TextAlignFillValue`, which has not been changed from
its default of 0 on most targets (it has been changed for x86). However,
most targets do not set the fill value because it doesn't entirely make
sense -- i.e. on AArch64 there's no reasonable byte value to use for
alignment, as instructions are word-sized and have to be well-aligned.

I think the check at the end `AsmParser::parseDirectiveAlign` is
suspicious even on x86 - if you use `.balign <align>, 0x90` in a code
section, you don't end up with a block of `0x90` repeated, you end up
with a block of NOPs of various widths. This functionality is never
tested.

The fix here is to modify the check to ignore the default text align
fill value when choosing to do code alignment or not.

Fixes #30303
2024-07-24 18:24:39 +01:00
Dmitriy Chestnykh
cc2fb58639
[MC,ELF] Use loc from the directive for .abort (#99648) 2024-07-20 22:33:39 +03:00
Dmitriy Chestnykh
bed625b0ad
[MC,ELF] Emit warning if a string constant contains newline char (#98060)
GAS emits warning about newline in the string constant so make the same
behaviour.
2024-07-17 02:34:25 +03: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
Fangrui Song
da368f2405 [MCParser] .altmacro: Support argument expansion not preceded by \
In the .altmacro mode, an argument can be expanded even if not preceded
by \. This behavior is not enabled for Darwin, which uses $
(`isIdentifierChar('$')` is true) for macro expansion.

This is f8b1ca4992a22b4b65282c09dd6f07a1a2839070 with a fix.
2024-07-11 22:11:46 -07:00
Fangrui Song
076ae58216 Revert "[MCParser] .altmacro: Support argument expansion not preceded by \"
This reverts commit f8b1ca4992a22b4b65282c09dd6f07a1a2839070.
It incorrectly replaces `t` in `blt` to `h`.

```
.altmacro
.macro gen t
  blt 2f
2:
.endm

gen h
```

Fix #98558
2024-07-11 21:16:35 -07:00
Fangrui Song
2718654c54
[MC] Support .cfi_label
GNU assembler 2.26 introduced the .cfi_label directive. It does not
expand to any CFI instructions, but defines a label in
.eh_frame/.debug_frame, which can be used by runtime patching code to
locate the FDE. .cfi_label is not allowed for CIE's initial
instructions, and can therefore be used to force the next instruction to
be placed in a FDE instead of a CIE.

In glibc since 2018, sysdeps/riscv/start.S utilizes .cfi_label to force
DW_CFA_undefined to be placed in a FDE. arc/csky/loongarch ports have
copied this use.
```
.cfi_startproc
// DW_CFA_undefined is allowed for CIE's initial instructions.
// Without .cfi_label, gas would place DW_CFA_undefined in a CIE.
.cfi_label .Ldummy
.cfi_undefined ra
.cfi_endproc
```

No CFI instruction is associated with .cfi_label, so the `case
MCCFIInstruction::OpLabel:` code in BOLT is unreachable and onlt to make
-Wswitch happy.

Close #97222

Pull Request: https://github.com/llvm/llvm-project/pull/97922
2024-07-07 12:41:13 -07:00
Fangrui Song
f8b1ca4992 [MCParser] .altmacro: Support argument expansion not preceded by \
In the .altmacro mode, an argument can be expanded even if not preceded
by \
2024-07-05 14:08:13 -07:00
Fangrui Song
812f9e81d2 [MCParser] .altmacro: ignore & after a token 2024-07-05 14:00:26 -07:00
Fangrui Song
e70f376b25 [MCParser] Simplify macro-like body expansion
Make it easy to support argument expansion in the altmacro mode.
2024-07-05 13:19:16 -07: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
Fangrui Song
db48f1a176 [MC] Remove nullable getCurrentSectionOnly use from AsmParser
We will implement getCurrentSectionOnly with `CurFrag->getParent()`,
which is non-null. Eliminate a nullable use.
2024-06-27 22:37:37 -07: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
Fangrui Song
70d6b8a358 MCAsmParser: Amend \+ expansion
Amend 7c956293d856224dd6a1b633820ef53009f7ef1c ("MCAsmParser: Support
\+") to increase Macro.Count per iteration to match the new gas feature
(milestone: 2.43).
2024-05-28 22:50:21 -07:00
Fangrui Song
195ba45721 [MCAsmParser] .macro/.rept/.irp/.irpc: remove excess \n after expansion
```
.irp foo,1
nop
.endr
nop
```

expands to an excess EOL between two nop lines. Other loop directives
and .macro have the same issue.

`Lex()` at "Jump to the macro instantiation and prime the lexer"
requires that there is one single \n token in CurTok. Therefore, we
cannot consume the trailing \n when parsing the macro(-like) body.
(commit c6e787f771d1f9d6a846b2d9b8db6adcd87e8dba (reverted by
1e5f29af81a5f6fda308074f6345b9fba4faa71c))

Instead, skip the potential \n after jumpToLoc at handleMacroExit.
2024-05-17 23:02:54 -07:00
Fangrui Song
1e5f29af81 Revert "[MCAsmParser] .rept/.irp/.irpc: remove excess tail EOL in expansion"
This reverts commit c6e787f771d1f9d6a846b2d9b8db6adcd87e8dba.

parseEOL() would remove \n # after .endr, not recognizing the line marker.

```
// reduced from Linux kernel arch/x86/crypto/sha1_avx2_x86_64_asm.S
.rept 1
nop
.endr
# 512 "a.s"
```
2024-05-17 11:55:21 -07:00
Fangrui Song
7c956293d8 MCAsmParser: Support \+
In .macro, \+ expands to the per-macro invocation count.
https://sourceware.org/pipermail/binutils/2024-May/134009.html

\+ counts from 0 for .irp/.irpc/.rept .

Note: We currently prints \q for `.print "\q"` while gas doesn't. This
patch does not change this behavior.
2024-05-16 00:00:58 -07:00
Fangrui Song
3cc445a660 [MCAsmParser] Simplify expandMacro
The error checking is only for .macro directives. Move it to the .macro
parser to remove one parameter.
2024-05-15 21:40:58 -07:00
Fangrui Song
90fbc5bbcd [MCAsmParser] Simplify. NFC 2024-05-15 20:38:55 -07:00
Fangrui Song
c6e787f771 [MCAsmParser] .rept/.irp/.irpc: remove excess tail EOL in expansion
```
.irp foo,1
nop
.endr
nop
```

expands to an excess EOL between two nop lines. Remove the excess EOL.
2024-05-15 17:52:59 -07:00
Kazu Hirata
bb6df0804b
[llvm] Use StringRef::operator== instead of StringRef::equals (NFC) (#91441)
I'm planning to remove StringRef::equals in favor of
StringRef::operator==.

- StringRef::operator==/!= outnumber StringRef::equals by a factor of
  70 under llvm/ in terms of their usage.

- The elimination of StringRef::equals brings StringRef closer to
  std::string_view, which has operator== but not equals.

- S == "foo" is more readable than S.equals("foo"), especially for
  !Long.Expression.equals("str") vs Long.Expression != "str".
2024-05-08 10:33:53 -07:00
Jon Roelofs
5b91647e3f
Allow .alt_entry symbols to pass the .cfi nesting check (#82268)
A symbol with an `N_ALT_ENTRY` attribute may be defined in the middle of
a subsection, so it is reasonable to opt them out of the
`.cfi_{start,end}proc` nesting check.

Fixes: https://github.com/llvm/llvm-project/issues/82261
2024-02-28 13:03:35 -08:00
Fangrui Song
2167881f51 [ARM,MC] Support FDPIC relocations
Linux kernel fs/binfmt_elf_fdpic.c supports FDPIC for MMU-less systems.
GCC/binutils/qemu support FDPIC ABI for ARM
(https://github.com/mickael-guene/fdpic_doc).
_ARM FDPIC Toolchain and ABI_ provides a summary.

This patch implements FDPIC relocations to the integrated assembler.
There are 6 static relocations and 2 dynamic relocations, with
R_ARM_FUNCDESC as both static and dynamic.

gas requires `--fdpic` to assemble data relocations like `.word f(FUNCDESC)`.
This patch adds `MCTargetOptions::FDPIC` and reports an error if FDPIC
is not set.

Pull Request: https://github.com/llvm/llvm-project/pull/82187
2024-02-21 10:13:26 -08: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
Jon Roelofs
d506aa4edf
Reland "[MC][AsmParser] Diagnose improperly nested .cfi frames"
This showed up when simplifying some large testcase, where the cfi directives
became out of sync with the proc's they enclose.

Now restricted to platforms that support .subsections_via_symbols.

This reverts commit 797b68c0ba699994e1038ac33d3083541482bf19.

Fixes: #72802

Differential revision: https://reviews.llvm.org/D153167

rdar://111459507
2023-11-21 10:33:11 -08:00
Martin Storsjö
797b68c0ba Revert "[MC][AsmParser] Diagnose improperly nested .cfi frames"
This reverts commit 4323da926f12672daec7f59384bd153a7cf28674.

This broke building libffi for ARM on Windows (and probably Darwin),
where one extern function intentionally falls through to another
one, while sharing one CFI region.

As long as one isn't using .subsections_via_symbols on MachO,
this probably shouldn't be a hard error.

Secondly, the tested pattern only produces an error on MachO and
COFF targets, but not for ELF, making the error case even more
inconsistent.

Reverting this commit for now, to figure out the best way forward.
2023-11-19 23:22:03 +02:00
Jon Roelofs
4323da926f
[MC][AsmParser] Diagnose improperly nested .cfi frames
This showed up when simplifying some large testcase, where the cfi directives
became out of sync with the proc's they enclose.

rdar://111459507

Differential revision: https://reviews.llvm.org/D155245

This reverts commit 4172fcc1ebbe0a7b699bfcbdaae9d5f688b62b09.
2023-11-17 14:33:21 -08:00
Arthur Eubanks
bd934fcbfd Revert "[MC][AsmParser] Diagnose improperly nested .cfi frames"
This reverts commit 2fd343e56c9fe3cdb1f9afe0fc9e1ec6d52d2f44.

Breaks building aarch64 builtins, see https://reviews.llvm.org/D155245.
2023-11-17 13:33:53 -08:00
Jon Roelofs
2fd343e56c
[MC][AsmParser] Diagnose improperly nested .cfi frames
This showed up when simplifying some large testcase, where the cfi directives
became out of sync with the proc's they enclose.

rdar://111459507

Differential revision: https://reviews.llvm.org/D155245

This reverts commit 4172fcc1ebbe0a7b699bfcbdaae9d5f688b62b09.
2023-11-17 10:36:38 -08:00
Jon Roelofs
4172fcc1eb
Revert "[MC][AsmParser] Diagnose improperly nested .cfi frames"
This reverts commit a4051932895d9ef6c4516c42309a49912f69f740.

It broke: lld/test/COFF/gc-dwarf-eh.s
2023-11-17 10:12:02 -08:00
Jon Roelofs
a405193289
[MC][AsmParser] Diagnose improperly nested .cfi frames
This showed up when simplifying some large testcase, where the cfi directives
became out of sync with the proc's they enclose.

rdar://111459507

Differential revision: https://reviews.llvm.org/D155245
2023-11-17 09:45:32 -08:00
Luís Marques
6e46545b98
Fix warning on align directives with non-zero fill value (#67237) 2023-09-27 14:35:26 +01:00
Luís Marques
40d4837037
Warn on align directive with non-zero fill value in virtual sections (#66792)
This patch warns when an align directive with a non-zero fill value is
used in a virtual section. The fill value is also set to zero,
preventing an assertion in `MCAssembler::writeSectionData` for the case
of `MCFragment::FT_Align` from tripping.
2023-09-20 15:17:50 +01:00