• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env perl
2##
3##  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4##
5##  Use of this source code is governed by a BSD-style license
6##  that can be found in the LICENSE file in the root of the source
7##  tree. An additional intellectual property rights grant can be found
8##  in the file PATENTS.  All contributing project authors may
9##  be found in the AUTHORS file in the root of the source tree.
10##
11
12
13# ads2gas_apple.pl
14# Author: Eric Fung (efung (at) acm.org)
15#
16# Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format
17#
18# Usage: cat inputfile | perl ads2gas_apple.pl > outputfile
19#
20
21print "@ This file was created from a .asm file\n";
22print "@  using the ads2gas_apple.pl script.\n\n";
23print ".syntax unified\n";
24
25my %macro_aliases;
26
27my @mapping_list = ("\$0", "\$1", "\$2", "\$3", "\$4", "\$5", "\$6", "\$7", "\$8", "\$9");
28
29my @incoming_array;
30
31# Perl trim function to remove whitespace from the start and end of the string
32sub trim($)
33{
34    my $string = shift;
35    $string =~ s/^\s+//;
36    $string =~ s/\s+$//;
37    return $string;
38}
39
40while (<STDIN>)
41{
42    # Load and store alignment
43    s/@/,:/g;
44
45    # Comment character
46    s/;/@/;
47
48    # Convert ELSE to .else
49    s/\bELSE\b/.else/g;
50
51    # Convert ENDIF to .endif
52    s/\bENDIF\b/.endif/g;
53
54    # Convert IF to .if
55    if (s/\bIF\b/.if/g) {
56        s/=+/==/g;
57    }
58
59    # Convert INCLUDE to .INCLUDE "file"
60    s/INCLUDE\s?(.*)$/.include \"$1\"/;
61
62    # No AREA required
63    # But ALIGNs in AREA must be obeyed
64    s/^(\s*)\bAREA\b.*ALIGN=([0-9])$/$1.text\n$1.p2align $2/;
65    # If no ALIGN, strip the AREA and align to 4 bytes
66    s/^(\s*)\bAREA\b.*$/$1.text\n$1.p2align 2/;
67
68    # Make function visible to linker.
69    s/EXPORT\s+\|([\$\w]*)\|/.globl _$1/;
70
71    # No vertical bars on function names
72    s/^\|(\$?\w+)\|/$1/g;
73
74    # Labels and functions need a leading underscore and trailing colon
75    s/^([a-zA-Z_0-9\$]+)/_$1:/ if !/EQU/;
76
77    # Branches need to call the correct, underscored, function
78    s/^(\s+b[egln]?[teq]?\s+)([a-zA-Z_0-9\$]+)/$1 _$2/ if !/EQU/;
79
80    # ALIGN directive
81    s/\bALIGN\b/.balign/g;
82
83    # Strip ARM
84    s/\s+ARM//;
85
86    # Strip REQUIRE8
87    s/\s+REQUIRE8//;
88
89    # Strip PRESERVE8
90    s/\s+PRESERVE8//;
91
92    # Strip PROC and ENDPROC
93    s/\bPROC\b//g;
94    s/\bENDP\b//g;
95
96    # EQU directive
97    s/(\S+\s+)EQU(\s+\S+)/.equ $1, $2/;
98
99    # Begin macro definition
100    if (/\bMACRO\b/) {
101        # Process next line down, which will be the macro definition
102        $_ = <STDIN>;
103        s/^/.macro/;
104        s/\$//g;             # Remove $ from the variables in the declaration
105    }
106
107    s/\$/\\/g;               # Use \ to reference formal parameters
108    # End macro definition
109
110    s/\bMEND\b/.endm/;       # No need to tell it where to stop assembling
111    next if /^\s*END\s*$/;
112    s/[ \t]+$//;
113    print;
114}
115