153 Commits

Author SHA1 Message Date
Fangrui Song
9753ea83dd MC,test: Migrate away from the .reloc constant hack
The initial .reloc support for MIPS incorrectly interpreted .reloc 0 as .reloc .+0 .
I was misled when porting .reloc to other targets in 2019 and 2020.

Many PRINT: prefixes are unnecessary. The MCAsmStreamer implementation
is generic, and it is unnecessary to test too many variants.
2025-07-16 22:53:18 -07:00
Koakuma
d86c63d8e3
[SPARC][IAS] Add definitions for cryptographic instructions
Reviewers: brad0, rorth, s-barannikov

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/139451
2025-07-13 15:14:51 +07:00
Koakuma
dfb5cadf5e
[SPARC][IAS] Properly set implied feature sets for ISA levels/extensions (#143232)
Some SPARC ISA levels and/or extensions are defined in a way such that
the availability of it implies the availability of other, more fundamental
ISA features (for example, targeting 64-bit environment implies that
V9 instructions are available).
Properly set those in the TableGen definitions.

Fixes https://github.com/llvm/llvm-project/issues/142388.
2025-06-20 05:12:51 +07:00
Fangrui Song
469d1034dc SPARC,test: Refactor error checking
* Test line/column
* Remove copied source lines from diagnostics
2025-06-07 09:21:02 -07:00
Koakuma
4c88873be8
[SPARC][IAS] Add definitions for OSA 2011 instructions
Reviewers: rorth, brad0, s-barannikov

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/138403
2025-05-29 22:16:58 +07:00
Koakuma
21b4059b3c
[SPARC] Print target address when disassembling branches and calls (#140340)
Similar to https://reviews.llvm.org/D93241, print target addresses
instead of raw offset values when disassembling branches and calls.

Should fix https://github.com/llvm/llvm-project/issues/122196 and
https://github.com/llvm/llvm-project/issues/139284.
2025-05-19 22:59:59 +07:00
Koakuma
cf19c6b5e2
[SPARC][IAS] Add definitions for UA 2007 instructions
Reviewers: rorth, s-barannikov, brad0

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/138401
2025-05-12 13:26:56 +07:00
Koakuma
2e1809f94a
[SPARC][IAS] Add definitions for UA 2005 instructions
Reviewers: rorth, s-barannikov, brad0

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/138400
2025-05-12 07:45:49 +07:00
Fangrui Song
f14a4ac4d6 Sparc: Suppress applyFixup when a relocation is generated
The computed R_SPARC_HIX22/R_SPARC_LOX10 value is non-zero even when the
input is 0. We should suppress applyFixup when a relocation is
generated.
2025-05-05 09:19:00 -07:00
Fangrui Song
d0096e8f96 Sparc: Improve SETHI and R_SPARC_DISP32 tests
* adjustFixupValue is called even when a R_SPARC_HIX22/R_SPARC_LOX10
  relocation is generated. This will be fixed shortly.
* Enhanced the %h44 test to show that we don't check overflow.
* Test R_SPARC_DISP32 in .gcc_except_table and .eh_frame . The original
  support did not test -filetype=obj output.
2025-05-05 09:17:27 -07:00
Fangrui Song
f27dc23869 Sparc: Test WDISP16/WDISP19/WDISP22 and data relocations 2025-05-04 21:36:00 -07:00
Fangrui Song
b975487ce9 Sparc: Reorganize relocation and ELF tests
Following the subdirectory schemes of newer ports (LoongArch, M68k).
2025-05-04 21:07:53 -07:00
Fangrui Song
b3e8b21c57 Sparc: Remove fixup kinds and specifiers for GOT10/GOT13/GOT22/PC10/PC22
Similar to f39696e7dee4f1dce8c10d2b17f987643c480895
2025-05-04 20:21:46 -07:00
Alex Rønne Petersen
c3ff3b2ba3
[SPARC][MC] Fix %gdop_hix22() and %gdop_lox10() to use correct relocations (#137915)
1bfc5e7 introduced support for `%gdop_hix22()` and `%gdop_lox10()`.
However, it incorrectly mapped them to `R_SPARC_GOTDATA_HIX22` and
`R_SPARC_GOTDATA_LOX10`. They should in fact emit
`R_SPARC_GOTDATA_OP_HIX22` and `R_SPARC_GOTDATA_OP_LOX10`.

This became a problem when assembling glibc's PIC startup code:

```asm
sethi %gdop_hix22(main), %o0
xor %o0, %gdop_lox10(main), %o0
ldx [%l7 + %o0], %o0, %gdop(main)
```

After the `xor`, `%o0` should contain the GOT offset for `main`, but
because of the incorrect relocations, it actually ends up containing the
address of `main`, which of course makes the following `ldx` fail.
2025-05-04 12:32:17 -07:00
Fangrui Song
1ec9dd256e Sparc: Refactor R_SPARC_13/R_SPARC_GOT13 handling and fix a bug referencing absolute symbol
https://reviews.llvm.org/D47136 did not correctly handle `ld [%i0 + abs], %o0; abs = 7`

To fix it and make fixup handling less hacky,

* Change TableGen MEMri to use simm13Op instead of i32imm
* Emit a fixup of kind fixup_sparc_13 in SparcMCCodeEmitter::getSImm13OpValue
* Convert fixup_sparc_13 to either R_SPARC_13/R_SPARC_GOT13 in getRelocType

This postpones 13/GOT13 decision to relocation generation, ensuring that
we suppress the relocation when referencing an absolute symbol, matching
gas.
2025-05-03 22:50:28 -07:00
Fangrui Song
de7e23f0e8 Sparc: Improve tests
* Remove unuseful -filetype=asm -show-encoding output from relocation tests.
* Test STT_TLS for TLS relocations
* Test r_offset for a few relocations
* Test ASM output for many relocations
2025-05-03 20:26:37 -07:00
Fangrui Song
e276216221 Sparc: Remove incorrect STT_SECTION adjustment special case for WPLT30
`call local` should perform STT_SECTION adjustment as well as `call .Ltmp0`.
The early support 9fc29098dfa684de53dd180aa93e94c39c388631 from 2014 was
confused.
2025-05-03 19:07:57 -07:00
Fangrui Song
c32571287e Sparc: Remove TLS fixup kinds that map to relocationtypes
As described in #136088 (for RISC-V), the `llvm-mc -show-encoding`
output no longer displays descriptive fixup names. Just remove
-show-encoding.
2025-05-03 18:35:15 -07:00
Fangrui Song
bb1e3df700 SparcInstPrinter: Support llvm-objdump --print-imm-hex
... to align with other targets, e.g., https://reviews.llvm.org/D77853
(AArch64) and https://reviews.llvm.org/D83634 (AVR).

binutils's sparc port uses %d when imm<=9, diverging from other ports.
We do not follow the binutils sparc port behavior.
2025-05-03 16:57:52 -07:00
Fangrui Song
f28408f3af [test] Remove CHECK lines for MCAsmStreamer's fixup output
The fixup output is a debug aid and should not be used to test
target-specific relocation generation implementation. The llvm-mc
-filetype=obj output is what truly matters.
2025-04-17 22:29:42 -07:00
Koakuma
ebacd46996
[SPARC][MC] Add tests for VIS family instructions
Also fix up any mistakes/typos in instruction definitions.

Reviewers: rorth, s-barannikov, brad0, MaskRay

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/130967
2025-04-03 19:55:18 +07:00
Koakuma
da01a185f1
[SPARC][IAS] Set correct ELF flag values for VIS & VIS2-enabled objects
Reviewers: brad0, s-barannikov, rorth

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/130966
2025-03-23 21:02:02 +07:00
Koakuma
a0d3d690a2
[SPARC][IAS] Add support for setsw pseudoinstruction
Implement `setsw` pseudoinstruction for setting a 32-bit signed imm.

Reviewers: brad0, s-barannikov, rorth

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/125150
2025-02-05 23:05:35 +07:00
Koakuma
498f108cc8
[SPARC][IAS] Add setuw alias for set
Reviewers: s-barannikov, brad0, rorth

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/125149
2025-02-03 23:15:39 +07:00
Koakuma
ad64946549
[SPARC][IAS] Add support for call dest, imm form (#119078)
This follows GCC behavior of allowing a trailing immediate, that is
ignored by the assembler.
2024-12-17 10:42:26 +07:00
Koakuma
7955760bc8
[SPARC][IAS] Add illtrap alias for unimp (#105928)
This follows Solaris behavior of allowing both mnemonics all the time.

Fixes https://github.com/llvm/llvm-project/issues/105639.
2024-08-29 19:28:38 +07:00
Koakuma
aca971d336
[SPARC][IAS] Add v8plus feature bit (#101367)
Implement handling for `v8plus` feature bit to allow the user to switch
between V8 and V8+ mode with 32-bit code.
Currently this only sets the appropriate ELF machine type and flags;
codegen changes will be done in future patches.

This is done as a prerequisite for `-mv8plus` flag on clang (#98713).
2024-08-02 10:30:36 +07:00
Koakuma
a92bcb20b8
[SPARC][IAS] Reject unknown/unavailable mnemonics early in ParseInstruction
Validate and reject any unknown or unavailable instruction mnemonics early
in ParseInstruction, before any operand parsing is performed. Some operands
(mainly memory ones) can be parsed slightly differently in V8 and V9
assembly language, so by rejecting unknown or unavailable instructions early
we can prevent the error message from being shadowed by the one raised during
operand parsing.

As a side effect this also allows us to tell unknown and unavailable
mnemonics apart, and issue a suggestion in appropriate cases.

This is based on the approach taken by the MIPS backend.

Reviewers: brad0, rorth, s-barannikov, jrtc27

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/96021
2024-07-11 11:33:19 +07:00
Koakuma
fb7f65ba46
[SPARC][IAS] Emit the correct ELF machine type (#96583)
Emit the correct machine type when writing out ELF objects.
This patch is modeled on GCC's behavior:
- `-m32` emits an object of type EM_SPARC;
- `-m32 -mcpu=v9` emits EM_SPARC32PLUS (however, see below); and
- `-m64` emits EM_SPARCV9.

Note that GCC does not guarantee emission of EM_SPARC32PLUS objects,
since GNU as doesn't support user control of emitted machine type.
It will always autodetect the type based on the instruction mix:
- If there's a V9 instruction inside, then emit EM_SPARC32PLUS; and
- Emit EM_SPARC otherwise.

For LLVM we choose deterministic behavior instead for simplicity.
2024-07-03 19:14:39 +07:00
Koakuma
e9b8cd0c80
[SPARC][IAS] Rework ASI/Prefetch tag matching in prep for ParseForAllFeatures
Unify parts of ASI and Prefetch tag matching at `parseASITag`
and `parsePrefetchTag` to use a common function to parse any immediate
expressions. This introduces a slight regression to error messages,
but is needed so we can enable `ParseForAllFeatures`
in `MatchOperandParserImpl` in a future patch.

Reviewers: jrtc27, brad0, rorth, s-barannikov

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/96020
2024-06-27 19:45:53 +07:00
Koakuma
e0b9cce4c7
[SPARC][IAS] Add movr(n)e alias for movr(n)z
This adds the alternate mnemonics for movrz and movrnz.

Reviewers: s-barannikov, jrtc27, brad0, rorth

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/94252
2024-06-09 22:16:34 +07:00
Koakuma
41f2ea0b0f
[SPARC][IAS] Add support for prefetcha instruction
This adds support for `prefetcha` instruction for prefetching from
alternate address spaces.

Reviewers: jrtc27, brad0, rorth, s-barannikov

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/94250
2024-06-09 22:13:31 +07:00
Koakuma
2388129d48
[SPARC][IAS] Add named prefetch tag constants
This adds named tag constants (such as `#one_write` and `#one_read`)
for the prefetch instruction.

Reviewers: jrtc27, rorth, brad0, s-barannikov

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/94249
2024-06-09 22:09:14 +07:00
Koakuma
715a5d8d93
[SPARC][IAS] Add aliases for %asr20-21 as defined in JPS1
This adds %set_softint and %clear_softint alias for %asr20 and %asr21
as defined in JPS1.

Reviewers: jrtc27, brad0, s-barannikov, rorth

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/94247
2024-06-09 10:14:50 +07:00
Koakuma
44f93578d2
[SPARC][IAS] Add support for %uhi and %ulo extensions
This adds support for GNU %uhi and %ulo extensions.
Those resolve to the same relocations as %hh and %hm.

Reviewers:
cyndyishida, dcci, brad0, jrtc27, aaupov, Endilll, rorth, maksfb, #reviewers-libcxxabi, s-barannikov, rafaelauler, ayermolo, #reviewers-libunwind, #reviewers-libcxx, daniel-grumberg, tbaederr

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/94246
2024-06-09 10:09:51 +07:00
Koakuma
f20d8b9dcb
[SPARC][IAS] Add GNU extension for addc
Transform `addc imm, %rs, %rd` into `addc %rs, imm, %rd`.
This is used in some GNU and Linux code.

Reviewers: s-barannikov, rorth, jrtc27, brad0

Reviewed By: s-barannikov

Pull Request: https://github.com/llvm/llvm-project/pull/94245
2024-06-09 09:58:46 +07:00
Fangrui Song
252c42354e [test] Change llvm-mc -arch= to -triple=
The issue is uncovered by #47698: for assembly files, -triple= specifies the
full target triple while -arch= merely sets the architecture part of the default
target triple, leaving a target triple which may not make sense, e.g.
riscv64-apple-darwin.

Therefore, -arch= is error-prone and not recommended for tests. The issue has
been benign as we recognize $unknown-apple-darwin as ELF instead of rejecting it
outrightly.

Due to the nature of the issue, we don't see the issue in tests using
architectures that any of Mach-O/COFF/XCOFF supports.
2023-09-11 14:51:50 -07:00
Sergei Barannikov
0917c50118
[Sparc] Replace some CAS instructions with InstAlias (#65588)
According to the manual, cas, casl, casx and casxl are synthetic
instructions. They map to casa and casxa with certain ASI tags.
2023-09-09 15:26:52 +03:00
Koakuma
a21a0a64db [SPARC][IAS] Add more instruction aliases
This adds some commonly-used instruction aliases from various sources:

- GNU
- SPARCv9 manual
- JPS1 ASR names

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157236
2023-09-05 04:59:58 -04:00
Koakuma
bc45acbddc [SPARC][IAS] Add named V9 ASI tag constants for memory instructions
This adds named ASI tag constants (such as #ASI_P and #ASI_P_L) for memory
accesses.

This patch adds 64-bit/V9 tag names, given that currently the majority of SPARC
software targets that arch.

Support for 32-bit/V8 tag names will be added in a future patch.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157235
2023-09-05 04:59:58 -04:00
Koakuma
f1246e90c0 [SPARC][IAS] Add support for the full set of CAS instructions
This completes the support for the CAS instructions.

Besides the base CASA and CASXA forms, on v9 the aliases CAS, CASX, CASL, and
CASXL are also available.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157234
2023-09-05 04:40:04 -04:00
Koakuma
af70618e96 [SPARC][IAS] Add complete set of v9 ASI load, store & swap forms
This extends support for ASI-tagged loads, stores, and swaps with the new
stored-ASI form ([reg+imm] %asi) introduced in v9.

CAS instructions are handled differently by the (dis-)assembler, so it will be
handled in a separate patch.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157233
2023-09-05 04:37:19 -04:00
Koakuma
2f3a362cc0 [SPARC][IAS] Add v9 encoding of %fq
While both SPARCv7/v8 and v9 has a register named %fq, they encode it
differently, so we need to differentiate between them.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157232
2023-09-05 04:37:18 -04:00
Koakuma
3a7431eb2d [SPARC][IAS] Add definitions for v9 State Registers
This adds definitions for SPARC v9 State Registers (privileged/nonprivileged,
see v9 manual ch. 5).

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157231
2023-09-01 23:40:45 -04:00
Koakuma
c8c5f317e4 [SPARC][IAS] Add SETX pseudoinstruction
This adds the v9 SETX pseudoinstruction for convenient loading of 64-bit values.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D157230
2023-08-22 12:49:00 -04:00
Koakuma
bf499ec2b9 [SPARC][IAS] Add support for v9 DONE, RETRY, SAVED, & RESTORED
Add support for DONE, RETRY, SAVED, and RESTORED (v9 Section A.11 & Section A.47).

Those instructions are used for low-level interrupt handling and register window
management by OS kernels.

Reviewed By: barannikov88

Differential Revision: https://reviews.llvm.org/D144936
2023-08-12 18:36:38 -04: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
Tobias Hieta
f84bac329b
[NFC][Py Reformat] Reformat lit.local.cfg python files in llvm
This is a follow-up to b71edfaa4ec3c998aadb35255ce2f60bba2940b0
since I forgot the lit.local.cfg files in that one.

Reformatting is done with `black`.

If you end up having problems merging this commit because you
have made changes to a python file, the best way to handle that
is to run git checkout --ours <yourfile> and then reformat it
with black.

If you run into any problems, post to discourse about it and
we will try to help.

RFC Thread below:

https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Reviewed By: barannikov88, kwk

Differential Revision: https://reviews.llvm.org/D150762
2023-05-17 17:03:15 +02:00
Brad Smith
776d50d371 [SPARC][MC] Fix encoding of backwards BPr branches
Make sure that the upper bits of the offset is placed in bits 20-21 of the
instruction word.

This fixes the encoding of backwards (negative offset) BPr branches.

(Previously, the upper two bits of the offset would overwrite parts of the rs1
field, causing it to branch on the wrong register, with the wrong offset)

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D144012
2023-05-04 23:13:08 -04:00
Vitaly Buka
9a1e60c586 Revert "[SPARC][MC] Fix encoding of backwards BPr branches"
Introduces UB, details in D144012.

This reverts commit 92f1156efc5f762ac3a4cc4eebe62742f6d75789.
2023-04-26 19:41:26 -07:00