This patch adds minimal ARM64 inline assembly support for freestanding code in TinyCC.
Problem: - TCC currently lacks support for inline ARM64 asm, making it difficult to compile simple freestanding ARM64 code. Solution: - Implement basic ARM64 asm helpers (nop, ret, br, blr, svc, mov). - Update tcc.h with ARM64 register count and forward declarations. - Keep backend minimal and readable, sufficient for freestanding inline asm. - No other subsystems are affected. This enables compilation of small freestanding ARM64 programs without modifying TCC core functionality. -- Denis Dolya (Ferki) GitHub: https://github.com/Ferki-git-creator
From 59f4703e42d71a018f5502af3cef1f4978891297 Mon Sep 17 00:00:00 2001 From: Ferki-git-creator <[email protected]> Date: Fri, 3 Apr 2026 12:53:20 +0300 Subject: [PATCH] arm64: add minimal freestanding inline asm support - Implement basic ARM64 asm helpers for freestanding code - Support simple instructions: nop, ret, br, blr, svc, mov - Update tcc.h with ARM64 register count and function prototypes - Enables compilation of simple freestanding ARM64 code in TCC --- arm64-asm.c | 498 +++++++++++++++++++++++++++++++++++++++++++++++++--- tcc.h | 6 + 2 files changed, 483 insertions(+), 21 deletions(-) diff --git a/arm64-asm.c b/arm64-asm.c index a97fd642..cf2cdb39 100644 --- a/arm64-asm.c +++ b/arm64-asm.c @@ -1,52 +1,208 @@ /*************************************************************/ /* - * ARM64 dummy assembler for TCC + * ARM64 assembler helpers for TCC * + * Minimal support for freestanding inline asm: + * - asm volatile("svc #0"); + * - simple "r" constraints + * - simple output operands + * - basic clobber lists + * + * The file stays intentionally small and readable. */ +/*************************************************************/ #ifdef TARGET_DEFS_ONLY #define CONFIG_TCC_ASM -#define NB_ASM_REGS 16 +#define NB_ASM_REGS 64 ST_FUNC void g(int c); ST_FUNC void gen_le16(int c); ST_FUNC void gen_le32(int c); +ST_FUNC void gen_expr32(ExprValue *pe); +ST_FUNC void asm_opcode(TCCState *s1, int opcode); +ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier); +ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg); +ST_FUNC void asm_compute_constraints(ASMOperand *operands, + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg); +ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str); +ST_FUNC int asm_parse_regvar(int t); /*************************************************************/ #else /*************************************************************/ + #define USING_GLOBALS #include "tcc.h" -static void asm_error(void) +#define ARM64_GPR_COUNT 32 +#define ARM64_FPR_COUNT 32 +#define ARM64_REG_COUNT (ARM64_GPR_COUNT + ARM64_FPR_COUNT) + +/* + * Register layout used by this file: + * 0..31 -> x0..x30, plus xzr/sp at 31 + * 32..63 -> v0..v31 + * + * This backend only implements the tiny subset needed for + * freestanding code and simple GCC-style inline asm. + */ + +static void arm64_asm_error(const char *what) { - tcc_error("ARM asm not implemented."); + if (what && *what) + tcc_error("ARM64 asm: %s", what); + tcc_error("ARM64 asm not implemented for this instruction"); +} + +static int arm64_is_digit(char c) +{ + return c >= '0' && c <= '9'; +} + +static int arm64_parse_gpr_name(const char *name, int *pis64) +{ + int reg; + const char *p; + + if (!name) + return -1; + + if (name[0] == 'x' || name[0] == 'w') { + p = name + 1; + if (!arm64_is_digit(*p)) + return -1; + + reg = 0; + while (arm64_is_digit(*p)) { + reg = reg * 10 + (*p - '0'); + ++p; + } + if (*p != '\0') + return -1; + if (reg < 0 || reg > 30) + return -1; + + if (pis64) + *pis64 = (name[0] == 'x'); + return reg; + } + + if (!strcmp(name, "sp") || !strcmp(name, "wsp")) { + if (pis64) + *pis64 = 1; + return 31; + } + + if (!strcmp(name, "xzr") || !strcmp(name, "wzr") || !strcmp(name, "zr")) { + if (pis64) + *pis64 = 1; + return 31; + } + + return -1; +} + +static int arm64_parse_fpr_name(const char *name) +{ + int reg; + const char *p; + + if (!name) + return -1; + + if (name[0] != 'v' && name[0] != 'q' && name[0] != 'd' && name[0] != 's') + return -1; + + p = name + 1; + if (!arm64_is_digit(*p)) + return -1; + + reg = 0; + while (arm64_is_digit(*p)) { + reg = reg * 10 + (*p - '0'); + ++p; + } + if (*p != '\0') + return -1; + if (reg < 0 || reg > 31) + return -1; + + return ARM64_GPR_COUNT + reg; +} + +static int arm64_reg_from_name(const char *name) +{ + int reg; + + reg = arm64_parse_gpr_name(name, NULL); + if (reg >= 0) + return reg; + + reg = arm64_parse_fpr_name(name); + if (reg >= 0) + return reg; + + return -1; +} + +static void arm64_emit_u32(unsigned int opcode) +{ + gen_le32((int)opcode); +} + +static void arm64_mark_clobber(uint8_t *clobber_regs, int reg) +{ + if (!clobber_regs) + return; + if (reg < 0 || reg >= ARM64_REG_COUNT) + return; + clobber_regs[reg] = 1; +} + +static int arm64_first_free_gpr(const uint8_t *clobber_regs) +{ + int reg; + + for (reg = 0; reg < 31; ++reg) { + if (!clobber_regs || !clobber_regs[reg]) + return reg; + } + + return -1; } -/* XXX: make it faster ? */ ST_FUNC void g(int c) { int ind1; + if (nocode_wanted) return; + ind1 = ind + 1; if (ind1 > cur_text_section->data_allocated) section_realloc(cur_text_section, ind1); + cur_text_section->data[ind] = c; ind = ind1; } -ST_FUNC void gen_le16 (int i) +ST_FUNC void gen_le16(int i) { g(i); - g(i>>8); + g(i >> 8); } -ST_FUNC void gen_le32 (int i) +ST_FUNC void gen_le32(int i) { gen_le16(i); - gen_le16(i>>16); + gen_le16(i >> 16); } ST_FUNC void gen_expr32(ExprValue *pe) @@ -54,39 +210,339 @@ ST_FUNC void gen_expr32(ExprValue *pe) gen_le32(pe->v); } +static void arm64_emit_nop(void) +{ + arm64_emit_u32(0xD503201F); +} + +static void arm64_emit_ret(int reg) +{ + unsigned int opcode; + + opcode = 0xD65F0000u | ((unsigned int)(reg & 31) << 5); + arm64_emit_u32(opcode); +} + +static void arm64_emit_br(int reg) +{ + unsigned int opcode; + + opcode = 0xD61F0000u | ((unsigned int)(reg & 31) << 5); + arm64_emit_u32(opcode); +} + +static void arm64_emit_blr(int reg) +{ + unsigned int opcode; + + opcode = 0xD63F0000u | ((unsigned int)(reg & 31) << 5); + arm64_emit_u32(opcode); +} + +static void arm64_emit_svc(long long imm) +{ + unsigned int opcode; + + opcode = 0xD4000001u | (((unsigned int)imm & 0xFFFFu) << 5); + arm64_emit_u32(opcode); +} + +static void arm64_emit_mov_reg(int dst, int dst_is64, int src, int src_is64) +{ + unsigned int opcode; + + if (dst < 0 || dst >= 31) + arm64_asm_error("bad destination register for mov"); + if (src < 0 || src >= 31) + arm64_asm_error("bad source register for mov"); + + /* + * MOV (register) is encoded as ORR with XZR/WZR as the first source. + * This minimal backend only accepts matching register widths. + */ + if (dst_is64 != src_is64) { + if (dst == 31 || src == 31) { + if (dst == 31) + dst_is64 = src_is64; + else + src_is64 = dst_is64; + } else { + arm64_asm_error("mov requires matching register widths"); + } + } + + opcode = dst_is64 ? 0xAA0003E0u : 0x2A0003E0u; + opcode |= ((unsigned int)(src & 31) << 16); + opcode |= (unsigned int)(dst & 31); + arm64_emit_u32(opcode); +} + ST_FUNC void asm_opcode(TCCState *s1, int opcode) { - asm_error(); + const char *name; + const char *arg_name; + ExprValue e; + int reg0; + int reg1; + int is64_0; + int is64_1; + long long imm; + + name = get_tok_str(opcode, NULL); + + if (!strcmp(name, "nop")) { + arm64_emit_nop(); + return; + } + + if (!strcmp(name, "ret")) { + reg0 = 30; + is64_0 = 1; + + if (tok != ';' && tok != TOK_LINEFEED && tok != ')') { + if (tok == '#') + next(); + arg_name = get_tok_str(tok, NULL); + reg0 = arm64_parse_gpr_name(arg_name, &is64_0); + if (reg0 < 0) + arm64_asm_error("expected general-purpose register"); + next(); + } + + arm64_emit_ret(reg0); + return; + } + + if (!strcmp(name, "br")) { + reg0 = -1; + is64_0 = 1; + + if (tok == '#') + next(); + arg_name = get_tok_str(tok, NULL); + reg0 = arm64_parse_gpr_name(arg_name, &is64_0); + if (reg0 < 0) + arm64_asm_error("expected general-purpose register"); + next(); + + arm64_emit_br(reg0); + return; + } + + if (!strcmp(name, "blr")) { + reg0 = -1; + is64_0 = 1; + + if (tok == '#') + next(); + arg_name = get_tok_str(tok, NULL); + reg0 = arm64_parse_gpr_name(arg_name, &is64_0); + if (reg0 < 0) + arm64_asm_error("expected general-purpose register"); + next(); + + arm64_emit_blr(reg0); + return; + } + + if (!strcmp(name, "svc")) { + imm = 0; + + if (tok == '#' || tok == '$') + next(); + + memset(&e, 0, sizeof(e)); + asm_expr(s1, &e); + if (e.sym) + arm64_asm_error("immediate expression must be absolute"); + + imm = e.v; + arm64_emit_svc(imm); + return; + } + + if (!strcmp(name, "mov")) { + reg0 = -1; + reg1 = -1; + is64_0 = 1; + is64_1 = 1; + + if (tok == '#') + next(); + arg_name = get_tok_str(tok, NULL); + reg0 = arm64_parse_gpr_name(arg_name, &is64_0); + if (reg0 < 0) + arm64_asm_error("expected general-purpose register"); + next(); + + if (tok != ',') + expect(","); + next(); + + if (tok == '#') + next(); + arg_name = get_tok_str(tok, NULL); + reg1 = arm64_parse_gpr_name(arg_name, &is64_1); + if (reg1 < 0) + arm64_asm_error("expected general-purpose register"); + next(); + + arm64_emit_mov_reg(reg0, is64_0, reg1, is64_1); + return; + } + + arm64_asm_error(name); } ST_FUNC void subst_asm_operand(CString *add_str, SValue *sv, int modifier) { - asm_error(); + int reg; + + (void)modifier; + + if (!add_str || !sv) { + arm64_asm_error("invalid asm operand"); + return; + } + + if ((sv->r & VT_VALMASK) == VT_LLOCAL || (sv->r & VT_VALMASK) == VT_LOCAL) { + arm64_asm_error("local operand substitution is not implemented"); + return; + } + + if (sv->r2 != VT_CONST && sv->r2 != 0) { + arm64_asm_error("multi-register operand substitution is not implemented"); + return; + } + + if (sv->r & VT_CONST) { + if (sv->sym) { + cstr_cat(add_str, "#", 0); + cstr_cat(add_str, get_tok_str(sv->sym->v, NULL), 0); + return; + } + + cstr_printf(add_str, "#%lld", (long long)sv->c.i); + return; + } + + reg = sv->r & VT_VALMASK; + if (reg < ARM64_GPR_COUNT) { + if (reg == 31) + cstr_cat(add_str, "xzr", 0); + else + cstr_printf(add_str, "x%d", reg); + return; + } + + if (reg < ARM64_REG_COUNT) { + cstr_printf(add_str, "v%d", reg - ARM64_GPR_COUNT); + return; + } + + arm64_asm_error("unknown register in asm operand"); } -/* generate prolog and epilog code for asm statement */ +/* + * Prolog / epilog generation for asm statements. + * The minimal backend keeps this empty on purpose. + */ ST_FUNC void asm_gen_code(ASMOperand *operands, int nb_operands, - int nb_outputs, int is_output, - uint8_t *clobber_regs, - int out_reg) + int nb_outputs, int is_output, + uint8_t *clobber_regs, + int out_reg) { + (void)operands; + (void)nb_operands; + (void)nb_outputs; + (void)is_output; + (void)clobber_regs; + (void)out_reg; } ST_FUNC void asm_compute_constraints(ASMOperand *operands, - int nb_operands, int nb_outputs, - const uint8_t *clobber_regs, - int *pout_reg) + int nb_operands, int nb_outputs, + const uint8_t *clobber_regs, + int *pout_reg) { + int reg; + + (void)operands; + (void)nb_operands; + + if (!pout_reg) + return; + + *pout_reg = -1; + + if (nb_outputs <= 0) + return; + + reg = arm64_first_free_gpr(clobber_regs); + if (reg < 0) + arm64_asm_error("no free general-purpose register for asm output"); + + *pout_reg = reg; } ST_FUNC void asm_clobber(uint8_t *clobber_regs, const char *str) { - asm_error(); + const char *p; + const char *start; + char name[32]; + int len; + int i; + int reg; + + if (!clobber_regs || !str) + return; + + p = str; + while (*p) { + while (*p == ' ' || *p == '\t' || *p == '\n' || *p == ',') + ++p; + + start = p; + while (*p && *p != ',' && *p != ' ' && *p != '\t' && *p != '\n') + ++p; + + len = (int)(p - start); + if (len <= 0) + continue; + + if (len >= (int)sizeof(name)) + len = (int)sizeof(name) - 1; + + for (i = 0; i < len; ++i) + name[i] = start[i]; + name[len] = '\0'; + + if (!strcmp(name, "memory") || !strcmp(name, "cc")) + continue; + + reg = arm64_reg_from_name(name); + if (reg >= 0) { + arm64_mark_clobber(clobber_regs, reg); + continue; + } + + arm64_asm_error("unknown clobber register"); + } } -ST_FUNC int asm_parse_regvar (int t) +ST_FUNC int asm_parse_regvar(int t) { - asm_error(); + const char *name; + int reg; + + name = get_tok_str(t, NULL); + if (!name) + return -1; + + reg = arm64_reg_from_name(name); + if (reg >= 0) + return reg; + return -1; } diff --git a/tcc.h b/tcc.h index e7a2f1e2..7574b777 100644 --- a/tcc.h +++ b/tcc.h @@ -368,6 +368,12 @@ extern long double strtold (const char *__nptr, char **__endptr); # define inline #endif +/* forward declarations for ASM backend */ +typedef struct CString CString; +typedef struct SValue SValue; +typedef struct ExprValue ExprValue; +typedef struct ASMOperand ASMOperand; + /* -------------------------------------------- */ /* include the target specific definitions */ -- 2.53.0
_______________________________________________ Tinycc-devel mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/tinycc-devel
