2010-05-18 17:58:33 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
##
|
2010-09-09 14:16:39 +02:00
|
|
|
## Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-06-18 18:39:21 +02:00
|
|
|
##
|
|
|
|
## Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
## that can be found in the LICENSE file in the root of the source
|
|
|
|
## tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
## in the file PATENTS. All contributing project authors may
|
|
|
|
## be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
##
|
|
|
|
|
|
|
|
|
2013-01-25 21:11:19 +01:00
|
|
|
# ads2gas_apple.pl
|
2010-05-18 17:58:33 +02:00
|
|
|
# Author: Eric Fung (efung (at) acm.org)
|
|
|
|
#
|
|
|
|
# Convert ARM Developer Suite 1.0.1 syntax assembly source to GNU as format
|
|
|
|
#
|
2013-01-25 21:11:19 +01:00
|
|
|
# Usage: cat inputfile | perl ads2gas_apple.pl > outputfile
|
2010-05-18 17:58:33 +02:00
|
|
|
#
|
2013-11-14 00:41:40 +01:00
|
|
|
|
2010-05-18 17:58:33 +02:00
|
|
|
print "@ This file was created from a .asm file\n";
|
|
|
|
print "@ using the ads2gas_apple.pl script.\n\n";
|
|
|
|
print "\t.set WIDE_REFERENCE, 0\n";
|
|
|
|
print "\t.set ARCHITECTURE, 5\n";
|
|
|
|
print "\t.set DO1STROUNDING, 0\n";
|
|
|
|
|
|
|
|
my %register_aliases;
|
|
|
|
my %macro_aliases;
|
|
|
|
|
|
|
|
my @mapping_list = ("\$0", "\$1", "\$2", "\$3", "\$4", "\$5", "\$6", "\$7", "\$8", "\$9");
|
|
|
|
|
|
|
|
my @incoming_array;
|
|
|
|
|
2011-12-13 02:14:03 +01:00
|
|
|
my @imported_functions;
|
|
|
|
|
2010-05-18 17:58:33 +02:00
|
|
|
# Perl trim function to remove whitespace from the start and end of the string
|
|
|
|
sub trim($)
|
|
|
|
{
|
|
|
|
my $string = shift;
|
|
|
|
$string =~ s/^\s+//;
|
|
|
|
$string =~ s/\s+$//;
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (<STDIN>)
|
|
|
|
{
|
2011-04-11 11:04:17 +02:00
|
|
|
# Load and store alignment
|
|
|
|
s/@/,:/g;
|
|
|
|
|
2010-05-18 17:58:33 +02:00
|
|
|
# Comment character
|
2013-11-14 00:41:40 +01:00
|
|
|
s/;/ @/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# Hexadecimal constants prefaced by 0x
|
|
|
|
s/#&/#0x/g;
|
|
|
|
|
|
|
|
# Convert :OR: to |
|
|
|
|
s/:OR:/ | /g;
|
|
|
|
|
|
|
|
# Convert :AND: to &
|
|
|
|
s/:AND:/ & /g;
|
|
|
|
|
|
|
|
# Convert :NOT: to ~
|
|
|
|
s/:NOT:/ ~ /g;
|
|
|
|
|
|
|
|
# Convert :SHL: to <<
|
|
|
|
s/:SHL:/ << /g;
|
|
|
|
|
|
|
|
# Convert :SHR: to >>
|
|
|
|
s/:SHR:/ >> /g;
|
|
|
|
|
|
|
|
# Convert ELSE to .else
|
2014-02-05 09:57:24 +01:00
|
|
|
s/\bELSE\b/.else/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# Convert ENDIF to .endif
|
2014-02-05 09:57:24 +01:00
|
|
|
s/\bENDIF\b/.endif/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# Convert ELSEIF to .elseif
|
2014-02-05 09:57:24 +01:00
|
|
|
s/\bELSEIF\b/.elseif/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# Convert LTORG to .ltorg
|
2014-02-05 09:57:24 +01:00
|
|
|
s/\bLTORG\b/.ltorg/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# Convert IF :DEF:to .if
|
|
|
|
# gcc doesn't have the ability to do a conditional
|
|
|
|
# if defined variable that is set by IF :DEF: on
|
|
|
|
# armasm, so convert it to a normal .if and then
|
|
|
|
# make sure to define a value elesewhere
|
|
|
|
if (s/\bIF :DEF:\b/.if /g)
|
|
|
|
{
|
|
|
|
s/=/==/g;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Convert IF to .if
|
|
|
|
if (s/\bIF\b/.if/g)
|
|
|
|
{
|
|
|
|
s/=/==/g;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Convert INCLUDE to .INCLUDE "file"
|
|
|
|
s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/;
|
|
|
|
|
|
|
|
# Code directive (ARM vs Thumb)
|
|
|
|
s/CODE([0-9][0-9])/.code $1/;
|
|
|
|
|
|
|
|
# No AREA required
|
2011-06-27 17:38:26 +02:00
|
|
|
# But ALIGNs in AREA must be obeyed
|
|
|
|
s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/;
|
|
|
|
# If no ALIGN, strip the AREA and align to 4 bytes
|
|
|
|
s/^\s*AREA.*$/.text\n.p2align 2/;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# DCD to .word
|
|
|
|
# This one is for incoming symbols
|
|
|
|
s/DCD\s+\|(\w*)\|/.long $1/;
|
|
|
|
|
|
|
|
# DCW to .short
|
|
|
|
s/DCW\s+\|(\w*)\|/.short $1/;
|
|
|
|
s/DCW(.*)/.short $1/;
|
|
|
|
|
|
|
|
# Constants defined in scope
|
|
|
|
s/DCD(.*)/.long $1/;
|
|
|
|
s/DCB(.*)/.byte $1/;
|
|
|
|
|
|
|
|
# Make function visible to linker, and make additional symbol with
|
|
|
|
# prepended underscore
|
|
|
|
s/EXPORT\s+\|([\$\w]*)\|/.globl _$1\n\t.globl $1/;
|
2011-12-13 02:14:03 +01:00
|
|
|
|
|
|
|
# Prepend imported functions with _
|
|
|
|
if (s/IMPORT\s+\|([\$\w]*)\|/.globl $1/)
|
|
|
|
{
|
|
|
|
$function = trim($1);
|
|
|
|
push(@imported_functions, $function);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach $function (@imported_functions)
|
|
|
|
{
|
|
|
|
s/$function/_$function/;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# No vertical bars required; make additional symbol with prepended
|
|
|
|
# underscore
|
|
|
|
s/^\|(\$?\w+)\|/_$1\n\t$1:/g;
|
|
|
|
|
|
|
|
# Labels need trailing colon
|
|
|
|
# s/^(\w+)/$1:/ if !/EQU/;
|
|
|
|
# put the colon at the end of the line in the macro
|
|
|
|
s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/;
|
|
|
|
|
2011-04-11 11:04:17 +02:00
|
|
|
# ALIGN directive
|
2014-02-05 09:57:24 +01:00
|
|
|
s/\bALIGN\b/.balign/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# Strip ARM
|
|
|
|
s/\sARM/@ ARM/g;
|
|
|
|
|
|
|
|
# Strip REQUIRE8
|
|
|
|
#s/\sREQUIRE8/@ REQUIRE8/g;
|
|
|
|
s/\sREQUIRE8/@ /g;
|
|
|
|
|
|
|
|
# Strip PRESERVE8
|
|
|
|
s/\sPRESERVE8/@ PRESERVE8/g;
|
|
|
|
|
|
|
|
# Strip PROC and ENDPROC
|
2011-12-13 02:17:07 +01:00
|
|
|
s/\bPROC\b/@/g;
|
|
|
|
s/\bENDP\b/@/g;
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
# EQU directive
|
|
|
|
s/(.*)EQU(.*)/.set $1, $2/;
|
|
|
|
|
|
|
|
# Begin macro definition
|
2014-02-05 09:57:24 +01:00
|
|
|
if (/\bMACRO\b/)
|
2010-05-18 17:58:33 +02:00
|
|
|
{
|
|
|
|
# Process next line down, which will be the macro definition
|
|
|
|
$_ = <STDIN>;
|
|
|
|
|
|
|
|
$trimmed = trim($_);
|
|
|
|
|
|
|
|
# remove commas that are separating list
|
|
|
|
$trimmed =~ s/,//g;
|
|
|
|
|
|
|
|
# string to array
|
2013-11-12 21:26:45 +01:00
|
|
|
@incoming_array = split(/\s+/, $trimmed);
|
2010-05-18 17:58:33 +02:00
|
|
|
|
|
|
|
print ".macro @incoming_array[0]\n";
|
|
|
|
|
|
|
|
# remove the first element, as that is the name of the macro
|
|
|
|
shift (@incoming_array);
|
|
|
|
|
|
|
|
@macro_aliases{@incoming_array} = @mapping_list;
|
|
|
|
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (($key, $value) = each(%macro_aliases))
|
|
|
|
{
|
|
|
|
$key =~ s/\$/\\\$/;
|
|
|
|
s/$key\b/$value/g;
|
|
|
|
}
|
|
|
|
|
|
|
|
# For macros, use \ to reference formal params
|
|
|
|
# s/\$/\\/g; # End macro definition
|
2014-02-05 09:57:24 +01:00
|
|
|
s/\bMEND\b/.endm/; # No need to tell it where to stop assembling
|
2010-05-18 17:58:33 +02:00
|
|
|
next if /^\s*END\s*$/;
|
2013-11-14 00:41:40 +01:00
|
|
|
|
2010-05-18 17:58:33 +02:00
|
|
|
print;
|
|
|
|
}
|