1#!/bin/sh
2# Translate the assembler syntax of arm64 assembler programs
3# Usage: asm-arm64.sh < linux-arm64-asm-file > portable-asm-file
4# The portable-asm-file has to be
5#   1. preprocessed,
6#   2. grep -v '^ *#line' | grep -v '^#'
7#   3. sed -e 's,% ,%,g' -e 's,//,@,g' -e 's,\$,#,g'
8
9# Copyright (C) 1999-2021 Bruno Haible <bruno@clisp.org>
10#
11# This program is free software: you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24tmpscript1=sed$$tmp1
25tmpscript2=sed$$tmp2
26tmpscript3=sed$$tmp3
27tmpremove='rm -f $tmpscript1 $tmpscript2 $tmpscript3'
28trap "$tmpremove" 1 2 15
29
30cat > $tmpscript1 << \EOF
31# ----------- Remove gcc self-identification
32/gcc2_compiled/d
33/gnu_compiled_c/d
34/\.ident/d
35EOF
36
37cat > $tmpscript2 << \EOF
38# ----------- Hide comments, to avoid trouble in preprocessing
39s,@,//,g
40# ----------- Turn # into $, to avoid trouble in preprocessing
41s,#,\$,g
42# ----------- Global symbols depends on ASM_UNDERSCORE
43s/^\([A-Za-z0-9_]\+\)/C(\1)/
44s/\.L\([A-Za-z0-9_]\+\)/L(\1)/
45s/\.global[ 	]\([A-Za-z0-9_]*\)/.global C(\1)/
46# ----------- Introduce macro syntax for assembler pseudo-ops
47/\.file\([ 	]\+\)/d
48/\.section\([ 	]\+\).*GNU-stack/d
49s/^C(\([A-Za-z0-9_]*\)):/FUNBEGIN(\1)/
50# ----------- Massage the beginning of functions
51/\.type/{
52s/\.type[ 	]\([A-Za-z0-9_]*\), *%function/DECLARE_FUNCTION(\1)/
53}
54# ----------- Massage the end of functions
55s/\.size[ 	]\([A-Za-z0-9_]*\),\(.*\)/FUNEND(\1)/
56EOF
57
58cat > $tmpscript3 << \EOF
59# ----------- Introduce macro syntax for PIC addressing of functions
60s/^	adrp	\([a-z0-9]*\), *\([A-Za-z0-9_]*\)/	adrp	\1, PAGE(C(\2))/
61s/[$]:lo12:\([A-Za-z0-9_]*\)/PAGEOFF(C(\1))/
62s/:lo12:\([A-Za-z0-9_]*\)/PAGEOFF(C(\1))/
63# ----------- Introduce macro syntax for PIC addressing of variables
64s/PAGE(C(\(vacall_function\)))/GOTPAGE(C(\1))/
65s/	ldr	\([a-z0-9]*\), \[\(.*\)PAGEOFF(C(\(vacall_function\)))\].*/	ldr	\1, [\2GOTPAGEOFF(C(\3))]\n	GOTINDIR(\1)/
66EOF
67
68sed -f $tmpscript1 | \
69sed -f $tmpscript2 | \
70sed -f $tmpscript3
71
72eval "$tmpremove"
73