Add nomacro script from cpuminer sources tree.
[novacoin.git] / contrib / clang / nomacro.pl
1 #!/usr/bin/perl
2 # Copyright 2012 pooler@litecoinpool.org
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the Free
6 # Software Foundation; either version 2 of the License, or (at your option)
7 # any later version.  See COPYING for more details.
8 #
9 # nomacro.pl - convert assembler macros to C preprocessor macros.
10
11 use strict;
12
13 foreach my $f (<*.S>) {
14         rename $f, "$f.orig";
15         open FIN, "$f.orig";
16         open FOUT, ">$f";
17         my $inmacro = 0;
18         my %macros = ();
19         while (<FIN>) {
20                 if (m/^\.macro\s+([_0-9A-Z]+)(?:\s*)(.*)$/i) {
21                         print FOUT "#define $1($2) \\\n";
22                         $macros{$1} = 1;
23                         $inmacro = 1;
24                         next;
25                 }
26                 if (m/^\.endm/) {
27                         print FOUT "\n";
28                         $inmacro = 0;
29                         next;
30                 }
31                 for my $m (keys %macros) {
32                         s/^([ \t]*)($m)(?:[ \t]+([^#\n]*))?([;\n])/\1\2(\3)\4/;
33                 }
34                 if ($inmacro) {
35                         if (m/^\s*#if/) {
36                                 $_ = <FIN> while (!m/^\s*#endif/);
37                                 next;
38                         }
39                         next if (m/^\s*$/);
40                         s/\\//g;
41                         s/$/; \\/;
42                 }
43                 print FOUT;
44         }
45         close FOUT;
46         close FIN;
47 }