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 "\t.set WIDE_REFERENCE, 0\n"; 24print "\t.set ARCHITECTURE, 5\n"; 25print "\t.set DO1STROUNDING, 0\n"; 26print "\t.syntax unified\n"; 27 28my %register_aliases; 29my %macro_aliases; 30 31my @mapping_list = ("\$0", "\$1", "\$2", "\$3", "\$4", "\$5", "\$6", "\$7", "\$8", "\$9"); 32 33my @incoming_array; 34 35my @imported_functions; 36 37# Perl trim function to remove whitespace from the start and end of the string 38sub trim($) 39{ 40 my $string = shift; 41 $string =~ s/^\s+//; 42 $string =~ s/\s+$//; 43 return $string; 44} 45 46while (<STDIN>) 47{ 48 # Load and store alignment 49 s/@/,:/g; 50 51 # Comment character 52 s/;/ @/g; 53 54 # Hexadecimal constants prefaced by 0x 55 s/#&/#0x/g; 56 57 # Convert :OR: to | 58 s/:OR:/ | /g; 59 60 # Convert :AND: to & 61 s/:AND:/ & /g; 62 63 # Convert :NOT: to ~ 64 s/:NOT:/ ~ /g; 65 66 # Convert :SHL: to << 67 s/:SHL:/ << /g; 68 69 # Convert :SHR: to >> 70 s/:SHR:/ >> /g; 71 72 # Convert ELSE to .else 73 s/\bELSE\b/.else/g; 74 75 # Convert ENDIF to .endif 76 s/\bENDIF\b/.endif/g; 77 78 # Convert ELSEIF to .elseif 79 s/\bELSEIF\b/.elseif/g; 80 81 # Convert LTORG to .ltorg 82 s/\bLTORG\b/.ltorg/g; 83 84 # Convert IF :DEF:to .if 85 # gcc doesn't have the ability to do a conditional 86 # if defined variable that is set by IF :DEF: on 87 # armasm, so convert it to a normal .if and then 88 # make sure to define a value elesewhere 89 if (s/\bIF :DEF:\b/.if /g) 90 { 91 s/=/==/g; 92 } 93 94 # Convert IF to .if 95 if (s/\bIF\b/.if/g) 96 { 97 s/=/==/g; 98 } 99 100 # Convert INCLUDE to .INCLUDE "file" 101 s/INCLUDE(\s*)(.*)$/.include $1\"$2\"/; 102 103 # Code directive (ARM vs Thumb) 104 s/CODE([0-9][0-9])/.code $1/; 105 106 # No AREA required 107 # But ALIGNs in AREA must be obeyed 108 s/^\s*AREA.*ALIGN=([0-9])$/.text\n.p2align $1/; 109 # If no ALIGN, strip the AREA and align to 4 bytes 110 s/^\s*AREA.*$/.text\n.p2align 2/; 111 112 # DCD to .word 113 # This one is for incoming symbols 114 s/DCD\s+\|(\w*)\|/.long $1/; 115 116 # DCW to .short 117 s/DCW\s+\|(\w*)\|/.short $1/; 118 s/DCW(.*)/.short $1/; 119 120 # Constants defined in scope 121 s/DCD(.*)/.long $1/; 122 s/DCB(.*)/.byte $1/; 123 124 # Make function visible to linker, and make additional symbol with 125 # prepended underscore 126 s/EXPORT\s+\|([\$\w]*)\|/.globl _$1\n\t.globl $1/; 127 128 # Prepend imported functions with _ 129 if (s/IMPORT\s+\|([\$\w]*)\|/.globl $1/) 130 { 131 $function = trim($1); 132 push(@imported_functions, $function); 133 } 134 135 foreach $function (@imported_functions) 136 { 137 s/$function/_$function/; 138 } 139 140 # No vertical bars required; make additional symbol with prepended 141 # underscore 142 s/^\|(\$?\w+)\|/_$1\n\t$1:/g; 143 144 # Labels need trailing colon 145# s/^(\w+)/$1:/ if !/EQU/; 146 # put the colon at the end of the line in the macro 147 s/^([a-zA-Z_0-9\$]+)/$1:/ if !/EQU/; 148 149 # ALIGN directive 150 s/\bALIGN\b/.balign/g; 151 152 # Strip ARM 153 s/\sARM/@ ARM/g; 154 155 # Strip REQUIRE8 156 #s/\sREQUIRE8/@ REQUIRE8/g; 157 s/\sREQUIRE8/@ /g; 158 159 # Strip PRESERVE8 160 s/\sPRESERVE8/@ PRESERVE8/g; 161 162 # Strip PROC and ENDPROC 163 s/\bPROC\b/@/g; 164 s/\bENDP\b/@/g; 165 166 # EQU directive 167 s/(.*)EQU(.*)/.set $1, $2/; 168 169 # Begin macro definition 170 if (/\bMACRO\b/) 171 { 172 # Process next line down, which will be the macro definition 173 $_ = <STDIN>; 174 175 $trimmed = trim($_); 176 177 # remove commas that are separating list 178 $trimmed =~ s/,//g; 179 180 # string to array 181 @incoming_array = split(/\s+/, $trimmed); 182 183 print ".macro @incoming_array[0]\n"; 184 185 # remove the first element, as that is the name of the macro 186 shift (@incoming_array); 187 188 @macro_aliases{@incoming_array} = @mapping_list; 189 190 next; 191 } 192 193 while (($key, $value) = each(%macro_aliases)) 194 { 195 $key =~ s/\$/\\\$/; 196 s/$key\b/$value/g; 197 } 198 199 # For macros, use \ to reference formal params 200# s/\$/\\/g; # End macro definition 201 s/\bMEND\b/.endm/; # No need to tell it where to stop assembling 202 next if /^\s*END\s*$/; 203 204 print; 205} 206