feat(rtl): migrate blaise_atomic .s to inline asm

This commit is contained in:
Graeme Geldenhuys 2026-06-25 11:07:21 +01:00
parent 85a46aa1ec
commit fe7b016119
3 changed files with 59 additions and 46 deletions

View file

@ -41,13 +41,14 @@ BLAISE = $(COMPILER_BIN)/blaise
BLAISE_FLAGS = --no-incremental --assembler internal
# Assembly sources — platform-specific, no C compiler needed.
# (blaise_atomic migrated to an inline-asm Pascal unit — see blaise_atomic.pas.)
ASM_OBJS = $(OBJ_DIR)/blaise_start_x86_64.o \
$(OBJ_DIR)/blaise_setjmp_x86_64.o \
$(OBJ_DIR)/blaise_atomic_x86_64.o \
$(OBJ_DIR)/blaise_utf8_x86_64.o
# Pascal RTL units compiled via the Blaise compiler.
PAS_OBJS = $(OBJ_DIR)/blaise_mem.o \
PAS_OBJS = $(OBJ_DIR)/blaise_atomic.o \
$(OBJ_DIR)/blaise_mem.o \
$(OBJ_DIR)/blaise_str.o \
$(OBJ_DIR)/blaise_set.o \
$(OBJ_DIR)/blaise_arc.o \
@ -73,6 +74,10 @@ $(OBJ_DIR)/%.o: $(ASM_DIR)/%.s
$(CC) -c -o $@ $<
# --- Pascal unit rules (unit-as-top-level) ---
$(OBJ_DIR)/blaise_atomic.o: $(PAS_SRC_DIR)/blaise_atomic.pas
@mkdir -p $(OBJ_DIR)
$(BLAISE) $(BLAISE_FLAGS) --source $< --unit-path $(PAS_SRC_DIR) --output $@
$(OBJ_DIR)/blaise_mem.o: $(PAS_SRC_DIR)/blaise_mem.pas
@mkdir -p $(OBJ_DIR)
$(BLAISE) $(BLAISE_FLAGS) --source $< --unit-path $(PAS_SRC_DIR) --output $@

View file

@ -1,44 +0,0 @@
#
# Blaise — An Object Pascal Compiler
# Copyright (c) 2026 Graeme Geldenhuys
# SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
# Licensed under the Apache License v2.0 with Runtime Library Exception.
# See LICENSE file in the project root for full license terms.
#
# Atomic 32-bit integer operations for ARC thread safety (x86_64).
#
# These use the x86 LOCK prefix for full sequential-consistency
# atomics. LOCK XADD atomically adds a value to a memory location
# and returns the previous value in the source register.
#
.text
# function _AtomicAddInt32(Ptr: PInteger; Delta: Integer): Integer;
# %rdi = pointer to 32-bit integer
# %esi = value to add
# Returns the value BEFORE the addition.
.globl _AtomicAddInt32
.type _AtomicAddInt32, @function
_AtomicAddInt32:
movl %esi, %eax
lock xaddl %eax, (%rdi)
ret
.size _AtomicAddInt32, .-_AtomicAddInt32
# function _AtomicSubInt32(Ptr: PInteger; Delta: Integer): Integer;
# %rdi = pointer to 32-bit integer
# %esi = value to subtract
# Returns the value BEFORE the subtraction.
.globl _AtomicSubInt32
.type _AtomicSubInt32, @function
_AtomicSubInt32:
negl %esi
movl %esi, %eax
lock xaddl %eax, (%rdi)
ret
.size _AtomicSubInt32, .-_AtomicSubInt32
.section .note.GNU-stack,"",@progbits

View file

@ -0,0 +1,52 @@
{
Blaise - An Object Pascal Compiler
Copyright (c) 2026 Graeme Geldenhuys
SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
Licensed under the Apache License v2.0 with Runtime Library Exception.
See LICENSE file in the project root for full license terms.
}
unit blaise_atomic;
// Atomic 32-bit add/sub primitives used by ARC reference counting.
//
// Inline-assembler port of runtime/src/main/asm/blaise_atomic_x86_64.s — the
// bodies are now `asm … end` routines in this Pascal unit so the RTL builds
// with Blaise's own internal assembler and needs no hand-written .s file
// (docs/inline-asm-design.adoc, §"Migration of the .s files").
//
// Each returns the PREVIOUS value at Ptr (xadd semantics). System V AMD64:
// arg1 Ptr -> %rdi, arg2 Delta -> %esi; the 32-bit result is returned in %eax.
// `nostackframe` lets the asm body own the frame so the registers hold the
// raw incoming arguments.
interface
type
PInteger = ^Integer;
function _AtomicAddInt32(Ptr: PInteger; Delta: Integer): Integer;
function _AtomicSubInt32(Ptr: PInteger; Delta: Integer): Integer;
implementation
{ return *Ptr (old); *Ptr += Delta — atomically. }
function _AtomicAddInt32(Ptr: PInteger; Delta: Integer): Integer;
assembler; nostackframe;
asm
movl %esi, %eax
lock xaddl %eax, (%rdi)
ret
end;
{ return *Ptr (old); *Ptr -= Delta — atomically (xadd of the negated delta). }
function _AtomicSubInt32(Ptr: PInteger; Delta: Integer): Integer;
assembler; nostackframe;
asm
negl %esi
movl %esi, %eax
lock xaddl %eax, (%rdi)
ret
end;
end.