gas-preprocessor.pl 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. #!/usr/bin/env perl
  2. # by David Conrad
  3. # This code is licensed under GPLv2 or later; go to gnu.org to read it
  4. # (not that it much matters for an asm preprocessor)
  5. # usage: set your assembler to be something like "perl gas-preprocessor.pl gcc"
  6. use strict;
  7. # Apple's gas is ancient and doesn't support modern preprocessing features like
  8. # .rept and has ugly macro syntax, among other things. Thus, this script
  9. # implements the subset of the gas preprocessor used by x264 and ffmpeg
  10. # that isn't supported by Apple's gas.
  11. my %canonical_arch = ("aarch64" => "aarch64", "arm64" => "aarch64",
  12. "arm" => "arm",
  13. "powerpc" => "powerpc", "ppc" => "powerpc");
  14. my %comments = ("aarch64" => '//',
  15. "arm" => '@',
  16. "powerpc" => '#');
  17. my @gcc_cmd;
  18. my @preprocess_c_cmd;
  19. my $comm;
  20. my $arch;
  21. my $as_type = "apple-gas";
  22. my $fix_unreq = $^O eq "darwin";
  23. my $force_thumb = 0;
  24. my $arm_cond_codes = "eq|ne|cs|cc|mi|pl|vs|vc|hi|ls|ge|lt|gt|le|al|hs|lo";
  25. my $usage_str = "
  26. $0\n
  27. Gas-preprocessor.pl converts assembler files using modern GNU as syntax for
  28. Apple's ancient gas version or clang's incompatible integrated assembler. The
  29. conversion is regularly tested for Libav, x264 and vlc. Other projects might
  30. use different features which are not correctly handled.
  31. Options for this program needs to be separated with ' -- ' from the assembler
  32. command. Following options are currently supported:
  33. -help - this usage text
  34. -arch - target architecture
  35. -as-type - one value out of {{,apple-}{gas,clang},armasm}
  36. -fix-unreq
  37. -no-fix-unreq
  38. -force-thumb - assemble as thumb regardless of the input source
  39. (note, this is incomplete and only works for sources
  40. it explicitly was tested with)
  41. ";
  42. sub usage() {
  43. print $usage_str;
  44. }
  45. while (@ARGV) {
  46. my $opt = shift;
  47. if ($opt =~ /^-(no-)?fix-unreq$/) {
  48. $fix_unreq = $1 ne "no-";
  49. } elsif ($opt eq "-force-thumb") {
  50. $force_thumb = 1;
  51. } elsif ($opt eq "-arch") {
  52. $arch = shift;
  53. die "unknown arch: '$arch'\n" if not exists $canonical_arch{$arch};
  54. } elsif ($opt eq "-as-type") {
  55. $as_type = shift;
  56. die "unknown as type: '$as_type'\n" if $as_type !~ /^((apple-)?(gas|clang)|armasm)$/;
  57. } elsif ($opt eq "-help") {
  58. usage();
  59. exit 0;
  60. } elsif ($opt eq "--" ) {
  61. @gcc_cmd = @ARGV;
  62. } elsif ($opt =~ /^-/) {
  63. die "option '$opt' is not known. See '$0 -help' for usage information\n";
  64. } else {
  65. push @gcc_cmd, $opt, @ARGV;
  66. }
  67. last if (@gcc_cmd);
  68. }
  69. if (grep /\.c$/, @gcc_cmd) {
  70. # C file (inline asm?) - compile
  71. @preprocess_c_cmd = (@gcc_cmd, "-S");
  72. } elsif (grep /\.[sS]$/, @gcc_cmd) {
  73. # asm file, just do C preprocessor
  74. @preprocess_c_cmd = (@gcc_cmd, "-E");
  75. } elsif (grep /-(v|h|-version|dumpversion)/, @gcc_cmd) {
  76. # pass -v/--version along, used during probing. Matching '-v' might have
  77. # uninteded results but it doesn't matter much if gas-preprocessor or
  78. # the compiler fails.
  79. exec(@gcc_cmd);
  80. } else {
  81. die "Unrecognized input filetype";
  82. }
  83. if ($as_type eq "armasm") {
  84. $preprocess_c_cmd[0] = "cpp";
  85. push(@preprocess_c_cmd, "-undef");
  86. # Normally a preprocessor for windows would predefine _WIN32,
  87. # but we're using any generic system-agnostic preprocessor "cpp"
  88. # with -undef (to avoid getting predefined variables from the host
  89. # system in cross compilation cases), so manually define it here.
  90. push(@preprocess_c_cmd, "-D_WIN32");
  91. @preprocess_c_cmd = grep ! /^-nologo$/, @preprocess_c_cmd;
  92. # Remove -ignore XX parameter pairs from preprocess_c_cmd
  93. my $index = 1;
  94. while ($index < $#preprocess_c_cmd) {
  95. if ($preprocess_c_cmd[$index] eq "-ignore" and $index + 1 < $#preprocess_c_cmd) {
  96. splice(@preprocess_c_cmd, $index, 2);
  97. next;
  98. }
  99. $index++;
  100. }
  101. if (grep /^-MM$/, @preprocess_c_cmd) {
  102. system(@preprocess_c_cmd) == 0 or die "Error running preprocessor";
  103. exit 0;
  104. }
  105. }
  106. # if compiling, avoid creating an output file named '-.o'
  107. if ((grep /^-c$/, @gcc_cmd) && !(grep /^-o/, @gcc_cmd)) {
  108. foreach my $i (@gcc_cmd) {
  109. if ($i =~ /\.[csS]$/) {
  110. my $outputfile = $i;
  111. $outputfile =~ s/\.[csS]$/.o/;
  112. push(@gcc_cmd, "-o");
  113. push(@gcc_cmd, $outputfile);
  114. last;
  115. }
  116. }
  117. }
  118. # replace only the '-o' argument with '-', avoids rewriting the make dependency
  119. # target specified with -MT to '-'
  120. my $index = 1;
  121. while ($index < $#preprocess_c_cmd) {
  122. if ($preprocess_c_cmd[$index] eq "-o") {
  123. $index++;
  124. $preprocess_c_cmd[$index] = "-";
  125. }
  126. $index++;
  127. }
  128. my $tempfile;
  129. if ($as_type ne "armasm") {
  130. @gcc_cmd = map { /\.[csS]$/ ? qw(-x assembler -) : $_ } @gcc_cmd;
  131. } else {
  132. @preprocess_c_cmd = grep ! /^-c$/, @preprocess_c_cmd;
  133. @preprocess_c_cmd = grep ! /^-m/, @preprocess_c_cmd;
  134. @preprocess_c_cmd = grep ! /^-G/, @preprocess_c_cmd;
  135. @preprocess_c_cmd = grep ! /^-W/, @preprocess_c_cmd;
  136. @preprocess_c_cmd = grep ! /^-Z/, @preprocess_c_cmd;
  137. @preprocess_c_cmd = grep ! /^-fp/, @preprocess_c_cmd;
  138. @preprocess_c_cmd = grep ! /^-EHsc$/, @preprocess_c_cmd;
  139. @preprocess_c_cmd = grep ! /^-O/, @preprocess_c_cmd;
  140. @gcc_cmd = grep ! /^-G/, @gcc_cmd;
  141. @gcc_cmd = grep ! /^-W/, @gcc_cmd;
  142. @gcc_cmd = grep ! /^-Z/, @gcc_cmd;
  143. @gcc_cmd = grep ! /^-fp/, @gcc_cmd;
  144. @gcc_cmd = grep ! /^-EHsc$/, @gcc_cmd;
  145. @gcc_cmd = grep ! /^-O/, @gcc_cmd;
  146. my @outfiles = grep /\.(o|obj)$/, @gcc_cmd;
  147. $tempfile = $outfiles[0].".asm";
  148. # Remove most parameters from gcc_cmd, which actually is the armasm command,
  149. # which doesn't support any of the common compiler/preprocessor options.
  150. @gcc_cmd = grep ! /^-D/, @gcc_cmd;
  151. @gcc_cmd = grep ! /^-U/, @gcc_cmd;
  152. @gcc_cmd = grep ! /^-m/, @gcc_cmd;
  153. @gcc_cmd = grep ! /^-M/, @gcc_cmd;
  154. @gcc_cmd = grep ! /^-c$/, @gcc_cmd;
  155. @gcc_cmd = grep ! /^-I/, @gcc_cmd;
  156. @gcc_cmd = map { /\.S$/ ? $tempfile : $_ } @gcc_cmd;
  157. }
  158. # detect architecture from gcc binary name
  159. if (!$arch) {
  160. if ($gcc_cmd[0] =~ /(arm64|aarch64|arm|powerpc|ppc)/) {
  161. $arch = $1;
  162. } else {
  163. # look for -arch flag
  164. foreach my $i (1 .. $#gcc_cmd-1) {
  165. if ($gcc_cmd[$i] eq "-arch" and
  166. $gcc_cmd[$i+1] =~ /(arm64|aarch64|arm|powerpc|ppc)/) {
  167. $arch = $1;
  168. }
  169. }
  170. }
  171. }
  172. # assume we're not cross-compiling if no -arch or the binary doesn't have the arch name
  173. $arch = qx/arch/ if (!$arch);
  174. die "Unknown target architecture '$arch'" if not exists $canonical_arch{$arch};
  175. $arch = $canonical_arch{$arch};
  176. $comm = $comments{$arch};
  177. my $inputcomm = $comm;
  178. $comm = ";" if $as_type =~ /armasm/;
  179. my %ppc_spr = (ctr => 9,
  180. vrsave => 256);
  181. open(INPUT, "-|", @preprocess_c_cmd) || die "Error running preprocessor";
  182. if ($ENV{GASPP_DEBUG}) {
  183. open(ASMFILE, ">&STDOUT");
  184. } else {
  185. if ($as_type ne "armasm") {
  186. open(ASMFILE, "|-", @gcc_cmd) or die "Error running assembler";
  187. } else {
  188. open(ASMFILE, ">", $tempfile);
  189. }
  190. }
  191. my $current_macro = '';
  192. my $macro_level = 0;
  193. my $rept_level = 0;
  194. my %macro_lines;
  195. my %macro_args;
  196. my %macro_args_default;
  197. my $macro_count = 0;
  198. my $altmacro = 0;
  199. my $in_irp = 0;
  200. my $num_repts;
  201. my @rept_lines;
  202. my @irp_args;
  203. my $irp_param;
  204. my @ifstack;
  205. my %symbols;
  206. my @sections;
  207. my %literal_labels; # for ldr <reg>, =<expr>
  208. my $literal_num = 0;
  209. my $literal_expr = ".word";
  210. $literal_expr = ".quad" if $arch eq "aarch64";
  211. my $thumb = 0;
  212. my %thumb_labels;
  213. my %call_targets;
  214. my %import_symbols;
  215. my %neon_alias_reg;
  216. my %neon_alias_type;
  217. my $temp_label_next = 0;
  218. my %last_temp_labels;
  219. my %next_temp_labels;
  220. my %labels_seen;
  221. my %aarch64_req_alias;
  222. if ($force_thumb) {
  223. parse_line(".thumb\n");
  224. }
  225. # pass 1: parse .macro
  226. # note that the handling of arguments is probably overly permissive vs. gas
  227. # but it should be the same for valid cases
  228. while (<INPUT>) {
  229. # remove lines starting with '#', preprocessing is done, '#' at start of
  230. # the line indicates a comment for all supported archs (aarch64, arm, ppc
  231. # and x86). Also strips line number comments but since they are off anyway
  232. # it is no loss.
  233. s/^\s*#.*$//;
  234. # remove all comments (to avoid interfering with evaluating directives)
  235. s/(?<!\\)$inputcomm.*//x;
  236. # Strip out windows linefeeds
  237. s/\r$//;
  238. foreach my $subline (split(";", $_)) {
  239. # Add newlines at the end of lines that don't already have one
  240. chomp $subline;
  241. $subline .= "\n";
  242. parse_line($subline);
  243. }
  244. }
  245. sub eval_expr {
  246. my $expr = $_[0];
  247. while ($expr =~ /([A-Za-z._][A-Za-z0-9._]*)/g) {
  248. my $sym = $1;
  249. $expr =~ s/$sym/($symbols{$sym})/ if defined $symbols{$sym};
  250. }
  251. eval $expr;
  252. }
  253. sub handle_if {
  254. my $line = $_[0];
  255. # handle .if directives; apple's assembler doesn't support important non-basic ones
  256. # evaluating them is also needed to handle recursive macros
  257. if ($line =~ /\.if(n?)([a-z]*)\s+(.*)/) {
  258. my $result = $1 eq "n";
  259. my $type = $2;
  260. my $expr = $3;
  261. if ($type eq "b") {
  262. $expr =~ s/\s//g;
  263. $result ^= $expr eq "";
  264. } elsif ($type eq "c") {
  265. if ($expr =~ /(.*)\s*,\s*(.*)/) {
  266. $result ^= $1 eq $2;
  267. } else {
  268. die "argument to .ifc not recognized";
  269. }
  270. } elsif ($type eq "") {
  271. $result ^= eval_expr($expr) != 0;
  272. } elsif ($type eq "eq") {
  273. $result = eval_expr($expr) == 0;
  274. } elsif ($type eq "lt") {
  275. $result = eval_expr($expr) < 0;
  276. } else {
  277. chomp($line);
  278. die "unhandled .if varient. \"$line\"";
  279. }
  280. push (@ifstack, $result);
  281. return 1;
  282. } else {
  283. return 0;
  284. }
  285. }
  286. sub parse_if_line {
  287. my $line = $_[0];
  288. # evaluate .if blocks
  289. if (scalar(@ifstack)) {
  290. # Don't evaluate any new if statements if we're within
  291. # a repetition or macro - they will be evaluated once
  292. # the repetition is unrolled or the macro is expanded.
  293. if (scalar(@rept_lines) == 0 and $macro_level == 0) {
  294. if ($line =~ /\.endif/) {
  295. pop(@ifstack);
  296. return 1;
  297. } elsif ($line =~ /\.elseif\s+(.*)/) {
  298. if ($ifstack[-1] == 0) {
  299. $ifstack[-1] = !!eval_expr($1);
  300. } elsif ($ifstack[-1] > 0) {
  301. $ifstack[-1] = -$ifstack[-1];
  302. }
  303. return 1;
  304. } elsif ($line =~ /\.else/) {
  305. $ifstack[-1] = !$ifstack[-1];
  306. return 1;
  307. } elsif (handle_if($line)) {
  308. return 1;
  309. }
  310. }
  311. # discard lines in false .if blocks
  312. foreach my $i (0 .. $#ifstack) {
  313. if ($ifstack[$i] <= 0) {
  314. return 1;
  315. }
  316. }
  317. }
  318. return 0;
  319. }
  320. sub parse_line {
  321. my $line = $_[0];
  322. return if (parse_if_line($line));
  323. if (scalar(@rept_lines) == 0) {
  324. if (/\.macro/) {
  325. $macro_level++;
  326. if ($macro_level > 1 && !$current_macro) {
  327. die "nested macros but we don't have master macro";
  328. }
  329. } elsif (/\.endm/) {
  330. $macro_level--;
  331. if ($macro_level < 0) {
  332. die "unmatched .endm";
  333. } elsif ($macro_level == 0) {
  334. $current_macro = '';
  335. return;
  336. }
  337. }
  338. }
  339. if ($macro_level == 0) {
  340. if ($line =~ /\.(rept|irp)/) {
  341. $rept_level++;
  342. } elsif ($line =~ /.endr/) {
  343. $rept_level--;
  344. }
  345. }
  346. if ($macro_level > 1) {
  347. push(@{$macro_lines{$current_macro}}, $line);
  348. } elsif (scalar(@rept_lines) and $rept_level >= 1) {
  349. push(@rept_lines, $line);
  350. } elsif ($macro_level == 0) {
  351. expand_macros($line);
  352. } else {
  353. if ($line =~ /\.macro\s+([\d\w\.]+)\s*,?\s*(.*)/) {
  354. $current_macro = $1;
  355. # commas in the argument list are optional, so only use whitespace as the separator
  356. my $arglist = $2;
  357. $arglist =~ s/,/ /g;
  358. my @args = split(/\s+/, $arglist);
  359. foreach my $i (0 .. $#args) {
  360. my @argpair = split(/=/, $args[$i]);
  361. $macro_args{$current_macro}[$i] = $argpair[0];
  362. $argpair[0] =~ s/:vararg$//;
  363. $macro_args_default{$current_macro}{$argpair[0]} = $argpair[1];
  364. }
  365. # ensure %macro_lines has the macro name added as a key
  366. $macro_lines{$current_macro} = [];
  367. } elsif ($current_macro) {
  368. push(@{$macro_lines{$current_macro}}, $line);
  369. } else {
  370. die "macro level without a macro name";
  371. }
  372. }
  373. }
  374. sub handle_set {
  375. my $line = $_[0];
  376. if ($line =~ /\.(?:set|equ)\s+(\S*)\s*,\s*(.*)/) {
  377. $symbols{$1} = eval_expr($2);
  378. return 1;
  379. }
  380. return 0;
  381. }
  382. sub expand_macros {
  383. my $line = $_[0];
  384. # handle .if directives; apple's assembler doesn't support important non-basic ones
  385. # evaluating them is also needed to handle recursive macros
  386. if (handle_if($line)) {
  387. return;
  388. }
  389. if (/\.purgem\s+([\d\w\.]+)/) {
  390. delete $macro_lines{$1};
  391. delete $macro_args{$1};
  392. delete $macro_args_default{$1};
  393. return;
  394. }
  395. if ($line =~ /\.altmacro/) {
  396. $altmacro = 1;
  397. return;
  398. }
  399. if ($line =~ /\.noaltmacro/) {
  400. $altmacro = 0;
  401. return;
  402. }
  403. $line =~ s/\%([^,]*)/eval_expr($1)/eg if $altmacro;
  404. # Strip out the .set lines from the armasm output
  405. return if (handle_set($line) and $as_type eq "armasm");
  406. if ($line =~ /\.rept\s+(.*)/) {
  407. $num_repts = $1;
  408. @rept_lines = ("\n");
  409. # handle the possibility of repeating another directive on the same line
  410. # .endr on the same line is not valid, I don't know if a non-directive is
  411. if ($num_repts =~ s/(\.\w+.*)//) {
  412. push(@rept_lines, "$1\n");
  413. }
  414. $num_repts = eval_expr($num_repts);
  415. } elsif ($line =~ /\.irp\s+([\d\w\.]+)\s*(.*)/) {
  416. $in_irp = 1;
  417. $num_repts = 1;
  418. @rept_lines = ("\n");
  419. $irp_param = $1;
  420. # only use whitespace as the separator
  421. my $irp_arglist = $2;
  422. $irp_arglist =~ s/,/ /g;
  423. $irp_arglist =~ s/^\s+//;
  424. @irp_args = split(/\s+/, $irp_arglist);
  425. } elsif ($line =~ /\.irpc\s+([\d\w\.]+)\s*(.*)/) {
  426. $in_irp = 1;
  427. $num_repts = 1;
  428. @rept_lines = ("\n");
  429. $irp_param = $1;
  430. my $irp_arglist = $2;
  431. $irp_arglist =~ s/,/ /g;
  432. $irp_arglist =~ s/^\s+//;
  433. @irp_args = split(//, $irp_arglist);
  434. } elsif ($line =~ /\.endr/) {
  435. my @prev_rept_lines = @rept_lines;
  436. my $prev_in_irp = $in_irp;
  437. my @prev_irp_args = @irp_args;
  438. my $prev_irp_param = $irp_param;
  439. my $prev_num_repts = $num_repts;
  440. @rept_lines = ();
  441. $in_irp = 0;
  442. @irp_args = '';
  443. if ($prev_in_irp != 0) {
  444. foreach my $i (@prev_irp_args) {
  445. foreach my $origline (@prev_rept_lines) {
  446. my $line = $origline;
  447. $line =~ s/\\$prev_irp_param/$i/g;
  448. $line =~ s/\\\(\)//g; # remove \()
  449. parse_line($line);
  450. }
  451. }
  452. } else {
  453. for (1 .. $prev_num_repts) {
  454. foreach my $origline (@prev_rept_lines) {
  455. my $line = $origline;
  456. parse_line($line);
  457. }
  458. }
  459. }
  460. } elsif ($line =~ /(\S+:|)\s*([\w\d\.]+)\s*(.*)/ && exists $macro_lines{$2}) {
  461. handle_serialized_line($1);
  462. my $macro = $2;
  463. # commas are optional here too, but are syntactically important because
  464. # parameters can be blank
  465. my @arglist = split(/,/, $3);
  466. my @args;
  467. my @args_seperator;
  468. my $comma_sep_required = 0;
  469. foreach (@arglist) {
  470. # allow arithmetic/shift operators in macro arguments
  471. $_ =~ s/\s*(\+|-|\*|\/|<<|>>|<|>)\s*/$1/g;
  472. my @whitespace_split = split(/\s+/, $_);
  473. if (!@whitespace_split) {
  474. push(@args, '');
  475. push(@args_seperator, '');
  476. } else {
  477. foreach (@whitespace_split) {
  478. #print ("arglist = \"$_\"\n");
  479. if (length($_)) {
  480. push(@args, $_);
  481. my $sep = $comma_sep_required ? "," : " ";
  482. push(@args_seperator, $sep);
  483. #print ("sep = \"$sep\", arg = \"$_\"\n");
  484. $comma_sep_required = 0;
  485. }
  486. }
  487. }
  488. $comma_sep_required = 1;
  489. }
  490. my %replacements;
  491. if ($macro_args_default{$macro}){
  492. %replacements = %{$macro_args_default{$macro}};
  493. }
  494. # construct hashtable of text to replace
  495. foreach my $i (0 .. $#args) {
  496. my $argname = $macro_args{$macro}[$i];
  497. my @macro_args = @{ $macro_args{$macro} };
  498. if ($args[$i] =~ m/=/) {
  499. # arg=val references the argument name
  500. # XXX: I'm not sure what the expected behaviour if a lot of
  501. # these are mixed with unnamed args
  502. my @named_arg = split(/=/, $args[$i]);
  503. $replacements{$named_arg[0]} = $named_arg[1];
  504. } elsif ($i > $#{$macro_args{$macro}}) {
  505. # more args given than the macro has named args
  506. # XXX: is vararg allowed on arguments before the last?
  507. $argname = $macro_args{$macro}[-1];
  508. if ($argname =~ s/:vararg$//) {
  509. #print "macro = $macro, args[$i] = $args[$i], args_seperator=@args_seperator, argname = $argname, arglist[$i] = $arglist[$i], arglist = @arglist, args=@args, macro_args=@macro_args\n";
  510. #$replacements{$argname} .= ", $args[$i]";
  511. $replacements{$argname} .= "$args_seperator[$i] $args[$i]";
  512. } else {
  513. die "Too many arguments to macro $macro";
  514. }
  515. } else {
  516. $argname =~ s/:vararg$//;
  517. $replacements{$argname} = $args[$i];
  518. }
  519. }
  520. my $count = $macro_count++;
  521. # apply replacements as regex
  522. foreach (@{$macro_lines{$macro}}) {
  523. my $macro_line = $_;
  524. # do replacements by longest first, this avoids wrong replacement
  525. # when argument names are subsets of each other
  526. foreach (reverse sort {length $a <=> length $b} keys %replacements) {
  527. $macro_line =~ s/\\$_/$replacements{$_}/g;
  528. }
  529. if ($altmacro) {
  530. foreach (reverse sort {length $a <=> length $b} keys %replacements) {
  531. $macro_line =~ s/\b$_\b/$replacements{$_}/g;
  532. }
  533. }
  534. $macro_line =~ s/\\\@/$count/g;
  535. $macro_line =~ s/\\\(\)//g; # remove \()
  536. parse_line($macro_line);
  537. }
  538. } else {
  539. handle_serialized_line($line);
  540. }
  541. }
  542. sub is_arm_register {
  543. my $name = $_[0];
  544. if ($name eq "lr" or
  545. $name eq "ip" or
  546. $name =~ /^[rav]\d+$/) {
  547. return 1;
  548. }
  549. return 0;
  550. }
  551. sub is_aarch64_register {
  552. my $name = $_[0];
  553. if ($name =~ /^[xw]\d+$/) {
  554. return 1;
  555. }
  556. return 0;
  557. }
  558. sub handle_local_label {
  559. my $line = $_[0];
  560. my $num = $_[1];
  561. my $dir = $_[2];
  562. my $target = "$num$dir";
  563. if ($dir eq "b") {
  564. $line =~ s/\b$target\b/$last_temp_labels{$num}/g;
  565. } else {
  566. my $name = "temp_label_$temp_label_next";
  567. $temp_label_next++;
  568. push(@{$next_temp_labels{$num}}, $name);
  569. $line =~ s/\b$target\b/$name/g;
  570. }
  571. return $line;
  572. }
  573. sub handle_serialized_line {
  574. my $line = $_[0];
  575. # handle .previous (only with regard to .section not .subsection)
  576. if ($line =~ /\.(section|text|const_data)/) {
  577. push(@sections, $line);
  578. } elsif ($line =~ /\.previous/) {
  579. if (!$sections[-2]) {
  580. die ".previous without a previous section";
  581. }
  582. $line = $sections[-2];
  583. push(@sections, $line);
  584. }
  585. $thumb = 1 if $line =~ /\.code\s+16|\.thumb/;
  586. $thumb = 0 if $line =~ /\.code\s+32|\.arm/;
  587. # handle ldr <reg>, =<expr>
  588. if ($line =~ /(.*)\s*ldr([\w\s\d]+)\s*,\s*=(.*)/ and $as_type ne "armasm") {
  589. my $label = $literal_labels{$3};
  590. if (!$label) {
  591. $label = "Literal_$literal_num";
  592. $literal_num++;
  593. $literal_labels{$3} = $label;
  594. }
  595. $line = "$1 ldr$2, $label\n";
  596. } elsif ($line =~ /\.ltorg/ and $as_type ne "armasm") {
  597. $line .= ".align 2\n";
  598. foreach my $literal (keys %literal_labels) {
  599. $line .= "$literal_labels{$literal}:\n $literal_expr $literal\n";
  600. }
  601. %literal_labels = ();
  602. }
  603. # handle GNU as pc-relative relocations for adrp/add
  604. if ($line =~ /(.*)\s*adrp([\w\s\d]+)\s*,\s*#?:pg_hi21:([^\s]+)/ and $as_type =~ /^apple-/) {
  605. $line = "$1 adrp$2, ${3}\@PAGE\n";
  606. } elsif ($line =~ /(.*)\s*add([\w\s\d]+)\s*,([\w\s\d]+)\s*,\s*#?:lo12:([^\s]+)/ and $as_type =~ /^apple-/) {
  607. $line = "$1 add$2, $3, ${4}\@PAGEOFF\n";
  608. }
  609. # thumb add with large immediate needs explicit add.w
  610. if ($thumb and $line =~ /add\s+.*#([^@]+)/) {
  611. $line =~ s/add/add.w/ if eval_expr($1) > 255;
  612. }
  613. # mach-o local symbol names start with L (no dot)
  614. $line =~ s/(?<!\w)\.(L\w+)/$1/g;
  615. # recycle the '.func' directive for '.thumb_func'
  616. if ($thumb and $as_type =~ /^apple-/) {
  617. $line =~ s/\.func/.thumb_func/x;
  618. }
  619. if ($thumb and $line =~ /^\s*(\w+)\s*:/) {
  620. $thumb_labels{$1}++;
  621. }
  622. if ($as_type =~ /^apple-/ and
  623. $line =~ /^\s*((\w+\s*:\s*)?bl?x?(..)?(?:\.w)?|\.global)\s+(\w+)/) {
  624. my $cond = $3;
  625. my $label = $4;
  626. # Don't interpret e.g. bic as b<cc> with ic as conditional code
  627. if ($cond =~ /^(|$arm_cond_codes)$/) {
  628. if (exists $thumb_labels{$label}) {
  629. print ASMFILE ".thumb_func $label\n";
  630. } else {
  631. $call_targets{$label}++;
  632. }
  633. }
  634. }
  635. # @l -> lo16() @ha -> ha16()
  636. $line =~ s/,\s+([^,]+)\@l\b/, lo16($1)/g;
  637. $line =~ s/,\s+([^,]+)\@ha\b/, ha16($1)/g;
  638. # move to/from SPR
  639. if ($line =~ /(\s+)(m[ft])([a-z]+)\s+(\w+)/ and exists $ppc_spr{$3}) {
  640. if ($2 eq 'mt') {
  641. $line = "$1${2}spr $ppc_spr{$3}, $4\n";
  642. } else {
  643. $line = "$1${2}spr $4, $ppc_spr{$3}\n";
  644. }
  645. }
  646. if ($line =~ /\.unreq\s+(.*)/) {
  647. if (defined $neon_alias_reg{$1}) {
  648. delete $neon_alias_reg{$1};
  649. delete $neon_alias_type{$1};
  650. return;
  651. } elsif (defined $aarch64_req_alias{$1}) {
  652. delete $aarch64_req_alias{$1};
  653. return;
  654. }
  655. }
  656. # old gas versions store upper and lower case names on .req,
  657. # but they remove only one on .unreq
  658. if ($fix_unreq) {
  659. if ($line =~ /\.unreq\s+(.*)/) {
  660. $line = ".unreq " . lc($1) . "\n";
  661. $line .= ".unreq " . uc($1) . "\n";
  662. }
  663. }
  664. if ($line =~ /(\w+)\s+\.(dn|qn)\s+(\w+)(?:\.(\w+))?(\[\d+\])?/) {
  665. $neon_alias_reg{$1} = "$3$5";
  666. $neon_alias_type{$1} = $4;
  667. return;
  668. }
  669. if (scalar keys %neon_alias_reg > 0 && $line =~ /^\s+v\w+/) {
  670. # This line seems to possibly have a neon instruction
  671. foreach (keys %neon_alias_reg) {
  672. my $alias = $_;
  673. # Require the register alias to match as an invididual word, not as a substring
  674. # of a larger word-token.
  675. if ($line =~ /\b$alias\b/) {
  676. $line =~ s/\b$alias\b/$neon_alias_reg{$alias}/g;
  677. # Add the type suffix. If multiple aliases match on the same line,
  678. # only do this replacement the first time (a vfoo.bar string won't match v\w+).
  679. $line =~ s/^(\s+)(v\w+)(\s+)/$1$2.$neon_alias_type{$alias}$3/;
  680. }
  681. }
  682. }
  683. if ($arch eq "aarch64" or $as_type eq "armasm") {
  684. # clang's integrated aarch64 assembler in Xcode 5 does not support .req/.unreq
  685. if ($line =~ /\b(\w+)\s+\.req\s+(\w+)\b/) {
  686. $aarch64_req_alias{$1} = $2;
  687. return;
  688. }
  689. foreach (keys %aarch64_req_alias) {
  690. my $alias = $_;
  691. # recursively resolve aliases
  692. my $resolved = $aarch64_req_alias{$alias};
  693. while (defined $aarch64_req_alias{$resolved}) {
  694. $resolved = $aarch64_req_alias{$resolved};
  695. }
  696. $line =~ s/\b$alias\b/$resolved/g;
  697. }
  698. }
  699. if ($arch eq "aarch64") {
  700. # fix missing aarch64 instructions in Xcode 5.1 (beta3)
  701. # mov with vector arguments is not supported, use alias orr instead
  702. if ($line =~ /^(\d+:)?\s*mov\s+(v\d[\.{}\[\]\w]+),\s*(v\d[\.{}\[\]\w]+)\b\s*$/) {
  703. $line = "$1 orr $2, $3, $3\n";
  704. }
  705. # movi 16, 32 bit shifted variant, shift is optional
  706. if ($line =~ /^(\d+:)?\s*movi\s+(v[0-3]?\d\.(?:2|4|8)[hsHS])\s*,\s*(#\w+)\b\s*$/) {
  707. $line = "$1 movi $2, $3, lsl #0\n";
  708. }
  709. # Xcode 5 misses the alias uxtl. Replace it with the more general ushll.
  710. # Clang 3.4 misses the alias sxtl too. Replace it with the more general sshll.
  711. # armasm64 also misses these instructions.
  712. if ($line =~ /^(\d+:)?\s*(s|u)xtl(2)?\s+(v[0-3]?\d\.[248][hsdHSD])\s*,\s*(v[0-3]?\d\.(?:2|4|8|16)[bhsBHS])\b\s*$/) {
  713. $line = "$1 $2shll$3 $4, $5, #0\n";
  714. }
  715. # clang 3.4 and armasm64 do not automatically use shifted immediates in add/sub
  716. if (($as_type eq "clang" or $as_type eq "armasm") and
  717. $line =~ /^(\d+:)?(\s*(?:add|sub)s?) ([^#l]+)#([\d\+\-\*\/ <>]+)\s*$/) {
  718. my $imm = eval $4;
  719. if ($imm > 4095 and not ($imm & 4095)) {
  720. $line = "$1 $2 $3#" . ($imm >> 12) . ", lsl #12\n";
  721. }
  722. }
  723. if ($ENV{GASPP_FIX_XCODE5}) {
  724. if ($line =~ /^\s*bsl\b/) {
  725. $line =~ s/\b(bsl)(\s+v[0-3]?\d\.(\w+))\b/$1.$3$2/;
  726. $line =~ s/\b(v[0-3]?\d)\.$3\b/$1/g;
  727. }
  728. if ($line =~ /^\s*saddl2?\b/) {
  729. $line =~ s/\b(saddl2?)(\s+v[0-3]?\d\.(\w+))\b/$1.$3$2/;
  730. $line =~ s/\b(v[0-3]?\d)\.\w+\b/$1/g;
  731. }
  732. if ($line =~ /^\s*dup\b.*\]$/) {
  733. $line =~ s/\bdup(\s+v[0-3]?\d)\.(\w+)\b/dup.$2$1/g;
  734. $line =~ s/\b(v[0-3]?\d)\.[bhsdBHSD](\[\d\])$/$1$2/g;
  735. }
  736. }
  737. }
  738. if ($as_type eq "armasm") {
  739. # Also replace variables set by .set
  740. foreach (keys %symbols) {
  741. my $sym = $_;
  742. $line =~ s/\b$sym\b/$symbols{$sym}/g;
  743. }
  744. # Handle function declarations and keep track of the declared labels
  745. if ($line =~ s/^\s*\.func\s+(\w+)/$1 PROC/) {
  746. $labels_seen{$1} = 1;
  747. }
  748. if ($line =~ s/^\s*(\d+)://) {
  749. # Convert local labels into unique labels. armasm (at least in
  750. # RVCT) has something similar, but still different enough.
  751. # By converting to unique labels we avoid any possible
  752. # incompatibilities.
  753. my $num = $1;
  754. foreach (@{$next_temp_labels{$num}}) {
  755. $line = "$_\n" . $line;
  756. }
  757. @next_temp_labels{$num} = ();
  758. my $name = "temp_label_$temp_label_next";
  759. $temp_label_next++;
  760. # The matching regexp above removes the label from the start of
  761. # the line (which might contain an instruction as well), readd
  762. # it on a separate line above it.
  763. $line = "$name:\n" . $line;
  764. $last_temp_labels{$num} = $name;
  765. }
  766. if ($line =~ s/^\s*(\w+):/$1/) {
  767. # Skip labels that have already been declared with a PROC,
  768. # labels must not be declared multiple times.
  769. return if (defined $labels_seen{$1});
  770. $labels_seen{$1} = 1;
  771. } elsif ($line !~ /(\w+) PROC/) {
  772. # If not a label, make sure the line starts with whitespace,
  773. # otherwise ms armasm interprets it incorrectly.
  774. $line =~ s/^[\.\w]/\t$&/;
  775. }
  776. # Check branch instructions
  777. if ($line =~ /(?:^|\n)\s*(\w+\s*:\s*)?(bl?x?\.?(..)?(\.w)?)\s+(\w+)/) {
  778. my $instr = $2;
  779. my $cond = $3;
  780. my $width = $4;
  781. my $target = $5;
  782. # Don't interpret e.g. bic as b<cc> with ic as conditional code
  783. if ($cond !~ /^(|$arm_cond_codes)$/) {
  784. # Not actually a branch
  785. } elsif ($target =~ /^(\d+)([bf])$/) {
  786. # The target is a local label
  787. $line = handle_local_label($line, $1, $2);
  788. $line =~ s/\b$instr\b/$&.w/ if $width eq "" and $arch eq "arm";
  789. } elsif (($arch eq "arm" and !is_arm_register($target)) or
  790. ($arch eq "aarch64" and !is_aarch64_register($target))) {
  791. $call_targets{$target}++;
  792. }
  793. } elsif ($line =~ /(?:^|\n)\s*(\w+\s*:\s*)?(cbn?z|adr|tbz)\s+(\w+)\s*,(\s*#\d+\s*,)?\s*(\w+)/) {
  794. my $instr = $2;
  795. my $reg = $3;
  796. my $bit = $4;
  797. my $target = $5;
  798. if ($target =~ /^(\d+)([bf])$/) {
  799. # The target is a local label
  800. $line = handle_local_label($line, $1, $2);
  801. } else {
  802. $call_targets{$target}++;
  803. }
  804. # Convert tbz with a wX register into an xX register,
  805. # due to armasm64 bugs/limitations.
  806. if ($instr eq "tbz" and $reg =~ /w\d+/) {
  807. my $xreg = $reg;
  808. $xreg =~ s/w/x/;
  809. $line =~ s/\b$reg\b/$xreg/;
  810. }
  811. } elsif ($line =~ /^\s*.h?word.*\b\d+[bf]\b/) {
  812. while ($line =~ /\b(\d+)([bf])\b/g) {
  813. $line = handle_local_label($line, $1, $2);
  814. }
  815. }
  816. # ALIGN in armasm syntax is the actual number of bytes
  817. if ($line =~ /\.(?:p2)?align\s+(\d+)/) {
  818. my $align = 1 << $1;
  819. $line =~ s/\.(?:p2)?align\s(\d+)/ALIGN $align/;
  820. }
  821. # Convert gas style [r0, :128] into armasm [r0@128] alignment specification
  822. $line =~ s/\[([^\[,]+),?\s*:(\d+)\]/[$1\@$2]/g;
  823. # armasm treats logical values {TRUE} and {FALSE} separately from
  824. # numeric values - logical operators and values can't be intermixed
  825. # with numerical values. Evaluate !<number> and (a <> b) into numbers,
  826. # let the assembler evaluate the rest of the expressions. This current
  827. # only works for cases when ! and <> are used with actual constant numbers,
  828. # we don't evaluate subexpressions here.
  829. # Evaluate !<number>
  830. while ($line =~ /!\s*(\d+)/g) {
  831. my $val = ($1 != 0) ? 0 : 1;
  832. $line =~ s/!(\d+)/$val/;
  833. }
  834. # Evaluate (a > b)
  835. while ($line =~ /\(\s*(\d+)\s*([<>])\s*(\d+)\s*\)/) {
  836. my $val;
  837. if ($2 eq "<") {
  838. $val = ($1 < $3) ? 1 : 0;
  839. } else {
  840. $val = ($1 > $3) ? 1 : 0;
  841. }
  842. $line =~ s/\(\s*(\d+)\s*([<>])\s*(\d+)\s*\)/$val/;
  843. }
  844. if ($arch eq "arm") {
  845. # Change a movw... #:lower16: into a mov32 pseudoinstruction
  846. $line =~ s/^(\s*)movw(\s+\w+\s*,\s*)\#:lower16:(.*)$/$1mov32$2$3/;
  847. # and remove the following, matching movt completely
  848. $line =~ s/^\s*movt\s+\w+\s*,\s*\#:upper16:.*$//;
  849. if ($line =~ /^\s*mov32\s+\w+,\s*([a-zA-Z]\w*)/) {
  850. $import_symbols{$1}++;
  851. }
  852. # Misc bugs/deficiencies:
  853. # armasm seems unable to parse e.g. "vmov s0, s1" without a type
  854. # qualifier, thus add .f32.
  855. $line =~ s/^(\s+(?:vmov|vadd))(\s+s\d+\s*,\s*s\d+)/$1.f32$2/;
  856. } elsif ($arch eq "aarch64") {
  857. # Convert ext into ext8; armasm64 seems to require it named as ext8.
  858. $line =~ s/^(\s+)ext(\s+)/$1ext8$2/;
  859. # Pick up targets from ldr x0, =sym+offset
  860. if ($line =~ /^\s*ldr\s+(\w+)\s*,\s*=([a-zA-Z]\w*)(.*)$/) {
  861. my $reg = $1;
  862. my $sym = $2;
  863. my $offset = eval_expr($3);
  864. if ($offset < 0) {
  865. # armasm64 is buggy with ldr x0, =sym+offset where the
  866. # offset is a negative value; it does write a negative
  867. # offset into the literal pool as it should, but the
  868. # negative offset only covers the lower 32 bit of the 64
  869. # bit literal/relocation.
  870. # Thus remove the offset and apply it manually with a sub
  871. # afterwards.
  872. $offset = -$offset;
  873. $line = "\tldr $reg, =$sym\n\tsub $reg, $reg, #$offset\n";
  874. }
  875. $import_symbols{$sym}++;
  876. }
  877. # armasm64 (currently) doesn't support offsets on adrp targets,
  878. # even though the COFF format relocations (and the linker)
  879. # supports it. Therefore strip out the offsets from adrp and
  880. # add :lo12: (in case future armasm64 would start handling it)
  881. # and add an extra explicit add instruction for the offset.
  882. if ($line =~ s/(adrp\s+\w+\s*,\s*(\w+))([\d\+\-\*\/\(\) <>]+)?/\1/) {
  883. $import_symbols{$2}++;
  884. }
  885. if ($line =~ s/(add\s+(\w+)\s*,\s*\w+\s*,\s*):lo12:(\w+)([\d\+\-\*\/\(\) <>]+)?/\1\3/) {
  886. my $reg = $2;
  887. my $sym = $3;
  888. my $offset = eval_expr($4);
  889. $line .= "\tadd $reg, $reg, #$offset\n" if $offset > 0;
  890. $import_symbols{$sym}++;
  891. }
  892. # Convert e.g. "add x0, x0, w0, uxtw" into "add x0, x0, w0, uxtw #0",
  893. # or "ldr x0, [x0, w0, uxtw]" into "ldr x0, [x0, w0, uxtw #0]".
  894. $line =~ s/(uxtw|sxtw)(\s*\]?\s*)$/\1 #0\2/i;
  895. # Convert "mov x0, v0.d[0]" into "umov x0, v0.d[0]"
  896. $line =~ s/\bmov\s+[xw]\d+\s*,\s*v\d+\.[ds]/u$&/i;
  897. # Convert "ccmp w0, #0, #0, ne" into "ccmpne w0, #0, #0",
  898. # and "csel w0, w0, w0, ne" into "cselne w0, w0, w0".
  899. $line =~ s/(ccmp|csel)\s+([xw]\w+)\s*,\s*([xw#]\w+)\s*,\s*([xw#]\w+)\s*,\s*($arm_cond_codes)/\1\5 \2, \3, \4/;
  900. # Convert "cinc w0, w0, ne" into "cincne w0, w0".
  901. $line =~ s/(cinc)\s+([xw]\w+)\s*,\s*([xw]\w+)\s*,\s*($arm_cond_codes)/\1\4 \2, \3/;
  902. # Convert "cset w0, lo" into "csetlo w0"
  903. $line =~ s/(cset)\s+([xw]\w+)\s*,\s*($arm_cond_codes)/\1\3 \2/;
  904. # Strip out prfum; armasm64 fails to assemble any
  905. # variant/combination of prfum tested so far, but it can be
  906. # left out without any
  907. $line =~ s/prfum.*\]//;
  908. # Convert "ldrb w0, [x0, #-1]" into "ldurb w0, [x0, #-1]".
  909. # Don't do this for forms with writeback though.
  910. if ($line =~ /(ld|st)(r[bh]?)\s+(\w+)\s*,\s*\[\s*(\w+)\s*,\s*#([^\]]+)\s*\][^!]/) {
  911. my $instr = $1;
  912. my $suffix = $2;
  913. my $target = $3;
  914. my $base = $4;
  915. my $offset = eval_expr($5);
  916. if ($offset < 0) {
  917. $line =~ s/$instr$suffix/${instr}u$suffix/;
  918. }
  919. }
  920. if ($ENV{GASPP_ARMASM64_INVERT_SCALE}) {
  921. # Instructions like fcvtzs and scvtf store the scale value
  922. # inverted in the opcode (stored as 64 - scale), but armasm64
  923. # in early versions stores it as-is. Thus convert from
  924. # "fcvtzs w0, s0, #8" into "fcvtzs w0, s0, #56".
  925. if ($line =~ /(?:fcvtzs|scvtf)\s+(\w+)\s*,\s*(\w+)\s*,\s*#(\d+)/) {
  926. my $scale = $3;
  927. my $inverted_scale = 64 - $3;
  928. $line =~ s/#$scale/#$inverted_scale/;
  929. }
  930. }
  931. }
  932. # armasm is unable to parse &0x - add spacing
  933. $line =~ s/&0x/& 0x/g;
  934. }
  935. if ($force_thumb) {
  936. # Convert register post indexing to a separate add instruction.
  937. # This converts e.g. "ldr r0, [r1], r2" into "ldr r0, [r1]",
  938. # "add r1, r1, r2".
  939. $line =~ s/((?:ldr|str)[bh]?)\s+(\w+),\s*\[(\w+)\],\s*(\w+)/$1 $2, [$3]\n\tadd $3, $3, $4/g;
  940. # Convert "mov pc, lr" into "bx lr", since the former only works
  941. # for switching from arm to thumb (and only in armv7), but not
  942. # from thumb to arm.
  943. $line =~ s/mov\s*pc\s*,\s*lr/bx lr/g;
  944. # Convert stmdb/ldmia/stmfd/ldmfd/ldm with only one register into a plain str/ldr with post-increment/decrement.
  945. # Wide thumb2 encoding requires at least two registers in register list while all other encodings support one register too.
  946. $line =~ s/stm(?:db|fd)\s+sp!\s*,\s*\{([^,-]+)\}/str $1, [sp, #-4]!/g;
  947. $line =~ s/ldm(?:ia|fd)?\s+sp!\s*,\s*\{([^,-]+)\}/ldr $1, [sp], #4/g;
  948. # Convert muls into mul+cmp
  949. $line =~ s/muls\s+(\w+),\s*(\w+)\,\s*(\w+)/mul $1, $2, $3\n\tcmp $1, #0/g;
  950. # Convert "and r0, sp, #xx" into "mov r0, sp", "and r0, r0, #xx"
  951. $line =~ s/and\s+(\w+),\s*(sp|r13)\,\s*#(\w+)/mov $1, $2\n\tand $1, $1, #$3/g;
  952. # Convert "ldr r0, [r0, r1, lsl #6]" where the shift is >3 (which
  953. # can't be handled in thumb) into "add r0, r0, r1, lsl #6",
  954. # "ldr r0, [r0]", for the special case where the same address is
  955. # used as base and target for the ldr.
  956. if ($line =~ /(ldr[bh]?)\s+(\w+),\s*\[\2,\s*(\w+),\s*lsl\s*#(\w+)\]/ and $4 > 3) {
  957. $line =~ s/(ldr[bh]?)\s+(\w+),\s*\[\2,\s*(\w+),\s*lsl\s*#(\w+)\]/add $2, $2, $3, lsl #$4\n\t$1 $2, [$2]/;
  958. }
  959. $line =~ s/\.arm/.thumb/x;
  960. }
  961. # comment out unsupported directives
  962. $line =~ s/\.type/$comm$&/x if $as_type =~ /^(apple-|armasm)/;
  963. $line =~ s/\.func/$comm$&/x if $as_type =~ /^(apple-|clang)/;
  964. $line =~ s/\.endfunc/$comm$&/x if $as_type =~ /^(apple-|clang)/;
  965. $line =~ s/\.endfunc/ENDP/x if $as_type =~ /armasm/;
  966. $line =~ s/\.ltorg/$comm$&/x if $as_type =~ /^(apple-|clang)/;
  967. $line =~ s/\.ltorg/LTORG/x if $as_type eq "armasm";
  968. $line =~ s/\.size/$comm$&/x if $as_type =~ /^(apple-|armasm)/;
  969. $line =~ s/\.fpu/$comm$&/x if $as_type =~ /^(apple-|armasm)/;
  970. $line =~ s/\.arch/$comm$&/x if $as_type =~ /^(apple-|clang|armasm)/;
  971. $line =~ s/\.object_arch/$comm$&/x if $as_type =~ /^(apple-|armasm)/;
  972. $line =~ s/.section\s+.note.GNU-stack.*/$comm$&/x if $as_type =~ /^(apple-|armasm)/;
  973. $line =~ s/\.syntax/$comm$&/x if $as_type =~ /armasm/;
  974. $line =~ s/\.hword/.short/x;
  975. if ($as_type =~ /^apple-/) {
  976. # the syntax for these is a little different
  977. $line =~ s/\.global/.globl/x;
  978. # also catch .section .rodata since the equivalent to .const_data is .section __DATA,__const
  979. $line =~ s/(.*)\.rodata/.const_data/x;
  980. $line =~ s/\.int/.long/x;
  981. $line =~ s/\.float/.single/x;
  982. }
  983. if ($as_type eq "apple-gas") {
  984. $line =~ s/vmrs\s+APSR_nzcv/fmrx r15/x;
  985. }
  986. if ($as_type eq "armasm") {
  987. $line =~ s/\.global/EXPORT/x;
  988. $line =~ s/\.int/dcd/x;
  989. $line =~ s/\.long/dcd/x;
  990. $line =~ s/\.float/dcfs/x;
  991. $line =~ s/\.word/dcd/x;
  992. $line =~ s/\.short/dcw/x;
  993. $line =~ s/\.byte/dcb/x;
  994. $line =~ s/\.quad/dcq/x;
  995. $line =~ s/\.ascii/dcb/x;
  996. $line =~ s/\.asciz(.*)$/dcb\1,0/x;
  997. $line =~ s/\.thumb/THUMB/x;
  998. $line =~ s/\.arm/ARM/x;
  999. # The alignment in AREA is the power of two, just as .align in gas
  1000. $line =~ s/\.text/AREA |.text|, CODE, READONLY, ALIGN=4, CODEALIGN/;
  1001. $line =~ s/(\s*)(.*)\.rodata/$1AREA |.rodata|, DATA, READONLY, ALIGN=5/;
  1002. $line =~ s/\.data/AREA |.data|, DATA, ALIGN=5/;
  1003. }
  1004. if ($as_type eq "armasm" and $arch eq "arm") {
  1005. $line =~ s/fmxr/vmsr/;
  1006. $line =~ s/fmrx/vmrs/;
  1007. $line =~ s/fadds/vadd.f32/;
  1008. }
  1009. if ($as_type eq "armasm" and $arch eq "aarch64") {
  1010. # Convert "b.eq" into "beq"
  1011. $line =~ s/\bb\.($arm_cond_codes)\b/b\1/;
  1012. }
  1013. # catch unknown section names that aren't mach-o style (with a comma)
  1014. if ($as_type =~ /apple-/ and $line =~ /.section ([^,]*)$/) {
  1015. die ".section $1 unsupported; figure out the mach-o section name and add it";
  1016. }
  1017. print ASMFILE $line;
  1018. }
  1019. if ($as_type ne "armasm") {
  1020. print ASMFILE ".text\n";
  1021. print ASMFILE ".align 2\n";
  1022. foreach my $literal (keys %literal_labels) {
  1023. print ASMFILE "$literal_labels{$literal}:\n $literal_expr $literal\n";
  1024. }
  1025. map print(ASMFILE ".thumb_func $_\n"),
  1026. grep exists $thumb_labels{$_}, keys %call_targets;
  1027. } else {
  1028. map print(ASMFILE "\tIMPORT $_\n"),
  1029. grep ! exists $labels_seen{$_}, (keys %call_targets, keys %import_symbols);
  1030. print ASMFILE "\tEND\n";
  1031. }
  1032. close(INPUT) or exit 1;
  1033. close(ASMFILE) or exit 1;
  1034. if ($as_type eq "armasm" and ! defined $ENV{GASPP_DEBUG}) {
  1035. system(@gcc_cmd) == 0 or die "Error running assembler";
  1036. }
  1037. END {
  1038. unlink($tempfile) if defined $tempfile;
  1039. }
  1040. #exit 1