Commit Graph

26 Commits

Author SHA1 Message Date
Benjamin Oldenburg
e7a16ce876 arm64: finish GNU inline asm constraint support 2026-04-04 20:02:33 +07:00
OpenCode
987a2aa909 arm64-asm: fix operator precedence warning in is_valid_logical_imm 2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
62345bb113 refactor: replace remaining ARM64 opcode literals with symbolic constants 2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
99d2daeb47 refactor(arm64): use symbolic opcode constants consistently
Replace hardcoded magic numbers with symbolic constants for ARM64
instruction opcodes, matching the style used in x86_64 backend.

Changes:
- arm64-tok.h: Add 93 new opcode constants and helper macros
  - Instruction opcodes: ARM64_ADD_IMM, ARM64_LDR_X, ARM64_B, etc.
  - Helper macros: ARM64_RD(), ARM64_RN(), ARM64_IMM12(), etc.
  - Field encodings: ARM64_SF(), ARM64_S(), ARM64_SH(), etc.

- arm64-asm.c: Refactor all instruction generation functions
  - gen_movz/gen_movn/gen_movk: Use ARM64_MOVZ/MOVN/MOVK
  - gen_add_imm/gen_sub_imm: Use ARM64_ADD_IMM/SUB_IMM
  - gen_dp_reg: Use symbolic opcodes
  - gen_ldst_imm/gen_ldst_pair: Use ARM64_LDR_*/STR_*
  - gen_b/gen_bl/gen_br/gen_blr/gen_ret: Use ARM64_B/BL/BR/BLR/RET
  - gen_cbz/gen_cbnz: Use ARM64_CBZ/CBNZ
  - gen_shift: Use ARM64_LSL_REG/LSR_REG/ASR_REG/ROR_REG
  - gen_barrier: Use ARM64_ISB/DSB/DMB
  - gen_mrs/gen_msr: Use symbolic constants
  - Inline asm save/restore: Use ARM64_STP_X/LDP_X

- arm64-gen.c: Begin systematic refactoring (first batch)
  - arm64_sub_sp: Use ARM64_SUB_IMM with helper macros

Benefits:
- Readability: Self-documenting code (ARM64_LDR_X vs 0xF9400000)
- Maintainability: Easier to spot encoding errors
- Consistency: Matches x86_64 backend style
- Safety: Helper macros prevent bit-shift mistakes

All tests pass with no functional changes.
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
44fef0743b style: fix arm64-asm.c to match TCC codebase conventions
- Remove unnecessary braces from single-statement if blocks
- Remove trailing whitespace throughout file
- Remove duplicate comment

Style now matches existing ARM64 backend and TCC conventions:
- Allman style for function definitions
- No braces for single-statement control structures
- Consistent 4-space indentation
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
d9b0c5b920 feat: implement ARM64 extended inline assembly support
Implement full GCC-style extended inline assembly for ARM64 backend:

- Add constraint parsing (constraint_priority, skip_constraint_modifiers)
- Implement register allocation (asm_compute_constraints)
- Add code generation for prolog/epilog and load/store (asm_gen_code)
- Support output/input/read-write operands with r, w, f, x, m, g constraints
- Support immediate constraints (i, I, J, K, L, n)
- Handle clobber lists (registers, memory, cc)
- Support constraint references, early clobber, named operands
- Fix '#' character handling in tccpp.c for ARM64 asm mode

Tests: Add comprehensive test suite with 18 test cases covering all features.

All existing TCC tests continue to pass.
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
7fe9c22cf2 arm64-asm.c: reject invalid registers in address operands
parse_addr_operand() silently accepted invalid register names like
[xyz] without error. Now explicitly validates the register and calls
tcc_error() if arm64_parse_regvar() returns -1 or >= 32.

Before: invalid registers caused silent wrong code or confusing errors
After: clear error message 'invalid register in address operand'
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
a702dcce9e arm64-asm.c: fix shift instruction encoding to match ARM ISA
LSL/LSR/ASR immediate shifts are UBFM/SBFM aliases with specific
immr/imms field encodings:
- LSL #shift: immr = (width - shift) & 0x3F, imms = width - 1
- LSR #shift: immr = shift & 0x3F, imms = width - 1
- ASR #shift: immr = shift & 0x3F, imms = width - 1

Fixes:
- immr field now always masked with 0x3F (6 bits), not width-1
- imms field is constant (width-1), not calculated from shift
- ROR uses EXTR format (Rm=shift, Rn=src, Rd=dest), not UBFM format

Based on ARM ARM documentation for UBFM/SBFM/EXTR instructions.
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
356e22677a arm64-asm: accept symbolic branch targets 2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
680c2d40e8 arm64-asm.c: remove dead operand type definitions
OPT_VREG, OPT_IM12, OPT_SHIFT, and OPT_REGSET were defined in the enum
and as OP_* bit masks but never used by any parsing function or
instruction handler in arm64-asm.c.

These appear to be artifacts copied from other assembler implementations
(arm-asm.c uses OP_VREG32/OP_VREG64/OP_REGSET32, riscv64-asm.c uses
OP_IM12S) but were never integrated into the ARM64 operand parsing logic.

Removing these unused definitions:
- Eliminates confusion for developers
- Reduces code clutter
- Makes the actual operand types (OPT_REG, OPT_IM, OPT_ADDR, OPT_COND)
  clearer
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
3f26af7b4c arm64-asm.c: consolidate near-identical code generation functions
Three pairs of functions differed only in base opcode constants:
- gen_movz/gen_movn/gen_movk (34 lines → 17 lines core + 9 lines wrappers)
- gen_b/gen_bl (14 lines → 8 lines core + 6 lines wrappers)
- gen_cbz/gen_cbnz (18 lines → 10 lines core + 8 lines wrappers)

Consolidated each into a single core function with base_opcode parameter:
- gen_mov_with_base() - handles MOVZ, MOVN, MOVK
- gen_b_or_bl() - handles B, BL
- gen_cbz_or_cbnz() - handles CBZ, CBNZ

Original functions retained as thin wrappers for backward compatibility.

Net reduction: 20 lines (66 → 46), eliminates code duplication hazard.
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
1f60eb4574 arm64-asm.c: deduplicate branch condition mapping
asm_branch() had two identical 15-case switch blocks (30 lines total)
that duplicated condition code mapping. This also duplicated the logic
in the existing parse_condition() helper.

Added get_branch_condition() helper that:
1. Maps branch tokens (TOK_ASM_beq) to condition tokens (TOK_ASM_eq)
2. Calls the existing parse_condition() helper
3. Returns the condition code (0-13) or -1 for non-conditional branches

This reduces code duplication from 30 lines to a single 29-line helper
function, and ensures all condition mapping logic is in one place.
2026-04-04 20:02:33 +07:00
Benjamin Oldenburg
5497f87f59 arm64-asm.c: validate operand types before encoding
Multiple instruction handlers were extracting op->reg without checking
that the operand was actually a register. When parse_operand() failed
to recognize a token, it set op->reg = -1, which when masked with 0x1F
became 31 (xzr/sp), silently encoding wrong instructions.

Now each handler validates operand types before extraction:
- asm_shift: validates op1 and op2 are registers
- asm_data_proc: validates op1, op2, and op3 are registers
- asm_ldst: validates op1 is register, op2 is address
- asm_ldst_pair: validates op1 and op2 are registers, op3 is address

This implements fail-fast behavior to catch typos and invalid operands
immediately rather than producing silently incorrect code.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
a1bf1d187d arm64-asm.c: reject invalid operands in parse_operand()
Previously, parse_operand() would silently accept any unrecognized token
and pass it to asm_expr() as an immediate, causing typos like:
  add x0, x1, xyz    ; 'xyz' is not a valid register
to be silently assembled as a symbol reference instead of erroring.

Now, if a token is not a register, condition code, or valid immediate
prefix (#, :, @, $), an error is emitted for identifier tokens.

This implements fail-fast behavior for invalid operands, making it easier
to catch typos and mistakes in assembly code.
2026-04-04 20:02:32 +07:00
OpenCode
d2c06612a5 arm64-asm: validate register width consistency in data processing instructions
The asm_data_proc function was OR-ing register widths together, which
allowed invalid ARM64 instructions like 'add x0, w1, w2' (mixed widths).
ARM64 requires all registers in data processing instructions to have
the same width (all X or all W).

Fix by validating that all three operand registers have matching widths
and emitting an error if they don't match.
2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
545378ed5b arm64: clarify inline asm support boundary 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
0decb1b86f arm64: support mnemonic win32 stack helpers 2026-04-04 20:02:32 +07:00
Benjamin Oldenburg
cf4441c415 Fix Windows ARM64 runtime regressions 2026-04-04 20:02:31 +07:00
Benjamin Oldenburg
4f4b3dda6b Add and stabilize Windows ARM64 support 2026-04-04 20:02:31 +07:00
grischka
5ec0e6f84b some reverts & fixes
Some checks failed
build and test / test-x86_64-linux (push) Has been cancelled
build and test / test-x86_64-osx (push) Has been cancelled
build and test / test-aarch64-osx (push) Has been cancelled
build and test / test-x86_64-win32 (push) Has been cancelled
build and test / test-i386-win32 (push) Has been cancelled
build and test / test-armv7-linux (push) Has been cancelled
build and test / test-aarch64-linux (push) Has been cancelled
build and test / test-riscv64-linux (push) Has been cancelled
workflow:
- revert 'pinact for security' for readability
  from 831c3fa184
tccpp.c:
- remove code that allows tcc to parse numbers incorrectly (*)
  from 829c848520
tccgen.c:
- Revert "Relaxed the 'incompatible pointer type' warning a bit" (*)
  from d9ec17d334.
tccrun.c:
- remove support for -nostdlib -run
  for simplicity, we require "main" with tcc -run always
tccpp.c:
- Revert "Free all preprocessor memmory in case of error."
  from c96f0cad61
  Remove TinyAlloc->limit instead.  Thus it can do also bigger
  allocs.  Big TokenStrings (like 200kb+ when compiling tcc)
  may come from inline functions or from large initializers.
Makefile/configure:
- use --config-pie for configuring tcc output only
- use -fPIC with clang-x86_64 to avoid 32-bit relocs
libtcc.c:
- fix "tcc file.c -run" i.e. -run as last argument
i386-gen.c:
- PIC refactor

(*) sorry, but code in tcc should have a minimum of generic relevance
2026-01-10 13:46:23 +01:00
herman ten brugge
8a8388c6ff Solve some bug reports
The savannah web site had some new bug report last december.
A lot of them are assemmbly bugs.
See testcase 60 for an overview.
2026-01-06 07:49:02 +01:00
mingodad
1645616843 Revert "Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64"
This reverts commit af686a796b.
2021-10-22 07:39:54 +02:00
mingodad
2ce2dbcb09 Revert "Fix some errors on arm64-asm.c, rename some variables, fix several code style declarations"
This reverts commit 61537d899a.
2021-10-22 07:39:26 +02:00
mingodad
61537d899a Fix some errors on arm64-asm.c, rename some variables, fix several code style declarations 2021-10-22 07:20:00 +02:00
mingodad
af686a796b Move almost all global variables to TCCState, actually all tests pass on Ubuntu 18.04 x86_64 2021-10-21 20:09:42 +02:00
Danny Milosavljevic
9539f51369
Makefile: Split arm64-asm from arm-asm 2021-01-03 15:49:17 +01:00