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"
8 # Apple's gas is ancient and doesn't support modern preprocessing features like
9 # .rept and has ugly macro syntax, among other things. Thus, this script
10 # implements the subset of the gas preprocessor used by x264 and ffmpeg
11 # that isn't supported by Apple's gas.
16 my $fix_unreq = $^O eq "darwin";
18 if ($gcc_cmd[0] eq "-fix-unreq") {
21 } elsif ($gcc_cmd[0] eq "-no-fix-unreq") {
26 if (grep /\.c$/, @gcc_cmd) {
27 # C file (inline asm?) - compile
28 @preprocess_c_cmd = (@gcc_cmd, "-S");
29 } elsif (grep /\.[sS]$/, @gcc_cmd) {
30 # asm file, just do C preprocessor
31 @preprocess_c_cmd = (@gcc_cmd, "-E");
33 die "Unrecognized input filetype";
36 # if compiling, avoid creating an output file named '-.o'
37 if ((grep /^-c$/, @gcc_cmd) && !(grep /^-o/, @gcc_cmd)) {
38 foreach my $i (@gcc_cmd) {
39 if ($i =~ /\.[csS]$/) {
41 $outputfile =~ s/\.[csS]$/.o/;
43 push(@gcc_cmd, $outputfile);
48 @gcc_cmd = map { /\.[csS]$/ ? qw(-x assembler -) : $_ } @gcc_cmd;
49 @preprocess_c_cmd = map { /\.o$/ ? "-" : $_ } @preprocess_c_cmd;
53 # detect architecture from gcc binary name
54 if ($gcc_cmd[0] =~ /arm/) {
56 } elsif ($gcc_cmd[0] =~ /powerpc|ppc/) {
61 foreach my $i (1 .. $#gcc_cmd-1) {
62 if ($gcc_cmd[$i] eq "-arch") {
63 if ($gcc_cmd[$i+1] =~ /arm/) {
65 } elsif ($gcc_cmd[$i+1] =~ /powerpc|ppc/) {
71 # assume we're not cross-compiling if no -arch or the binary doesn't have the arch name
73 my $native_arch = qx/arch/;
74 if ($native_arch =~ /arm/) {
76 } elsif ($native_arch =~ /powerpc|ppc/) {
82 die "Unable to identify target architecture";
85 my %ppc_spr = (ctr => 9,
88 open(ASMFILE, "-|", @preprocess_c_cmd) || die "Error running preprocessor";
90 my $current_macro = '';
94 my %macro_args_default;
103 # pass 1: parse .macro
104 # note that the handling of arguments is probably overly permissive vs. gas
105 # but it should be the same for valid cases
107 # remove all comments (to avoid interfering with evaluating directives)
110 # comment out unsupported directives
111 s/\.type/$comm.type/x;
112 s/\.func/$comm.func/x;
113 s/\.endfunc/$comm.endfunc/x;
114 s/\.ltorg/$comm.ltorg/x;
115 s/\.size/$comm.size/x;
117 s/\.arch/$comm.arch/x;
118 s/\.object_arch/$comm.object_arch/x;
120 # the syntax for these is a little different
122 # also catch .section .rodata since the equivalent to .const_data is .section __DATA,__const
123 s/(.*)\.rodata/.const_data/x;
127 # catch unknown section names that aren't mach-o style (with a comma)
128 if (/.section ([^,]*)$/) {
129 die ".section $1 unsupported; figure out the mach-o section name and add it";
137 $expr =~ s/([A-Za-z._][A-Za-z0-9._]*)/$symbols{$1}/g;
143 # handle .if directives; apple's assembler doesn't support important non-basic ones
144 # evaluating them is also needed to handle recursive macros
145 if ($line =~ /\.if(n?)([a-z]*)\s+(.*)/) {
146 my $result = $1 eq "n";
152 $result ^= $expr eq "";
153 } elsif ($type eq "c") {
154 if ($expr =~ /(.*)\s*,\s*(.*)/) {
157 die "argument to .ifc not recognized";
159 } elsif ($type eq "") {
160 $result ^= eval_expr($expr) != 0;
161 } elsif ($type eq "eq") {
162 $result = eval_expr($expr) == 0;
163 } elsif ($type eq "lt") {
164 $result = eval_expr($expr) < 0;
167 die "unhandled .if varient. \"$line\"";
169 push (@ifstack, $result);
179 # evaluate .if blocks
180 if (scalar(@ifstack)) {
184 } elsif ($line =~ /\.elseif\s+(.*)/) {
185 if ($ifstack[-1] == 0) {
186 $ifstack[-1] = !!eval_expr($1);
187 } elsif ($ifstack[-1] > 0) {
188 $ifstack[-1] = -$ifstack[-1];
192 $ifstack[-1] = !$ifstack[-1];
194 } elsif (handle_if($line)) {
198 # discard lines in false .if blocks
199 foreach my $i (0 .. $#ifstack) {
200 if ($ifstack[$i] <= 0) {
208 if ($macro_level > 1 && !$current_macro) {
209 die "nested macros but we don't have master macro";
213 if ($macro_level < 0) {
214 die "unmatched .endm";
215 } elsif ($macro_level == 0) {
221 if ($macro_level > 1) {
222 push(@{$macro_lines{$current_macro}}, $line);
223 } elsif ($macro_level == 0) {
224 expand_macros($line);
226 if ($line =~ /\.macro\s+([\d\w\.]+)\s*(.*)/) {
229 # commas in the argument list are optional, so only use whitespace as the separator
233 my @args = split(/\s+/, $arglist);
234 foreach my $i (0 .. $#args) {
235 my @argpair = split(/=/, $args[$i]);
236 $macro_args{$current_macro}[$i] = $argpair[0];
237 $argpair[0] =~ s/:vararg$//;
238 $macro_args_default{$current_macro}{$argpair[0]} = $argpair[1];
240 # ensure %macro_lines has the macro name added as a key
241 $macro_lines{$current_macro} = [];
243 } elsif ($current_macro) {
244 push(@{$macro_lines{$current_macro}}, $line);
246 die "macro level without a macro name";
254 # handle .if directives; apple's assembler doesn't support important non-basic ones
255 # evaluating them is also needed to handle recursive macros
256 if (handle_if($line)) {
260 if (/\.purgem\s+([\d\w\.]+)/) {
261 delete $macro_lines{$1};
262 delete $macro_args{$1};
263 delete $macro_args_default{$1};
267 if ($line =~ /\.altmacro/) {
272 if ($line =~ /\.noaltmacro/) {
277 $line =~ s/\%([^,]*)/eval_expr($1)/eg if $altmacro;
279 if ($line =~ /\.set\s+(.*),\s*(.*)/) {
280 $symbols{$1} = eval_expr($2);
283 if ($line =~ /(\S+:|)\s*([\w\d\.]+)\s*(.*)/ && exists $macro_lines{$2}) {
284 push(@pass1_lines, $1);
287 # commas are optional here too, but are syntactically important because
288 # parameters can be blank
289 my @arglist = split(/,/, $3);
293 my $comma_sep_required = 0;
295 # allow arithmetic/shift operators in macro arguments
296 $_ =~ s/\s*(\+|-|\*|\/|<<|>>)\s*/$1/g;
298 my @whitespace_split = split(/\s+/, $_);
299 if (!@whitespace_split) {
301 push(@args_seperator, '');
303 foreach (@whitespace_split) {
304 #print ("arglist = \"$_\"\n");
307 my $sep = $comma_sep_required ? "," : " ";
308 push(@args_seperator, $sep);
309 #print ("sep = \"$sep\", arg = \"$_\"\n");
310 $comma_sep_required = 0;
315 $comma_sep_required = 1;
319 if ($macro_args_default{$macro}){
320 %replacements = %{$macro_args_default{$macro}};
323 # construct hashtable of text to replace
324 foreach my $i (0 .. $#args) {
325 my $argname = $macro_args{$macro}[$i];
326 my @macro_args = @{ $macro_args{$macro} };
327 if ($args[$i] =~ m/=/) {
328 # arg=val references the argument name
329 # XXX: I'm not sure what the expected behaviour if a lot of
330 # these are mixed with unnamed args
331 my @named_arg = split(/=/, $args[$i]);
332 $replacements{$named_arg[0]} = $named_arg[1];
333 } elsif ($i > $#{$macro_args{$macro}}) {
334 # more args given than the macro has named args
335 # XXX: is vararg allowed on arguments before the last?
336 $argname = $macro_args{$macro}[-1];
337 if ($argname =~ s/:vararg$//) {
338 #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";
339 #$replacements{$argname} .= ", $args[$i]";
340 $replacements{$argname} .= "$args_seperator[$i] $args[$i]";
342 die "Too many arguments to macro $macro";
345 $argname =~ s/:vararg$//;
346 $replacements{$argname} = $args[$i];
350 my $count = $macro_count++;
352 # apply replacements as regex
353 foreach (@{$macro_lines{$macro}}) {
355 # do replacements by longest first, this avoids wrong replacement
356 # when argument names are subsets of each other
357 foreach (reverse sort {length $a <=> length $b} keys %replacements) {
358 $macro_line =~ s/\\$_/$replacements{$_}/g;
360 $macro_line =~ s/\\\@/$count/g;
361 $macro_line =~ s/\\\(\)//g; # remove \()
362 parse_line($macro_line);
365 push(@pass1_lines, $line);
369 close(ASMFILE) or exit 1;
370 open(ASMFILE, "|-", @gcc_cmd) or die "Error running assembler";
371 #open(ASMFILE, ">/tmp/a.S") or die "Error running assembler";
377 my %literal_labels; # for ldr <reg>, =<expr>
389 # pass 2: parse .rept and .if variants
390 # NOTE: since we don't implement a proper parser, using .rept with a
391 # variable assigned from .set is not supported
392 foreach my $line (@pass1_lines) {
393 # handle .previous (only with regard to .section not .subsection)
394 if ($line =~ /\.(section|text|const_data)/) {
395 push(@sections, $line);
396 } elsif ($line =~ /\.previous/) {
397 if (!$sections[-2]) {
398 die ".previous without a previous section";
400 $line = $sections[-2];
401 push(@sections, $line);
404 $thumb = 1 if $line =~ /\.code\s+16|\.thumb/;
405 $thumb = 0 if $line =~ /\.code\s+32|\.arm/;
407 # handle ldr <reg>, =<expr>
408 if ($line =~ /(.*)\s*ldr([\w\s\d]+)\s*,\s*=(.*)/) {
409 my $label = $literal_labels{$3};
411 $label = "Literal_$literal_num";
413 $literal_labels{$3} = $label;
415 $line = "$1 ldr$2, $label\n";
416 } elsif ($line =~ /\.ltorg/) {
417 $line .= ".align 2\n";
418 foreach my $literal (keys %literal_labels) {
419 $line .= "$literal_labels{$literal}:\n .word $literal\n";
421 %literal_labels = ();
424 # thumb add with large immediate needs explicit add.w
425 if ($thumb and $line =~ /add\s+.*#([^@]+)/) {
426 $line =~ s/add/add.w/ if eval_expr($1) > 255;
429 # mach-o local symbol names start with L (no dot)
430 $line =~ s/(?<!\w)\.(L\w+)/$1/g;
432 if ($thumb and $line =~ /^\s*(\w+)\s*:/) {
436 if ($line =~ /^\s*((\w+:)?blx?|\.globl)\s+(\w+)/) {
440 # @l -> lo16() @ha -> ha16()
441 $line =~ s/,\s+([^,]+)\@l\b/, lo16($1)/g;
442 $line =~ s/,\s+([^,]+)\@ha\b/, ha16($1)/g;
445 if ($line =~ /(\s+)(m[ft])([a-z]+)\s+(\w+)/ and exists $ppc_spr{$3}) {
447 $line = "$1${2}spr $ppc_spr{$3}, $4\n";
449 $line = "$1${2}spr $4, $ppc_spr{$3}\n";
453 # old gas versions store upper and lower case names on .req,
454 # but they remove only one on .unreq
456 if ($line =~ /\.unreq\s+(.*)/) {
457 $line = ".unreq " . lc($1) . "\n";
458 print ASMFILE ".unreq " . uc($1) . "\n";
462 if ($line =~ /\.rept\s+(.*)/) {
466 # handle the possibility of repeating another directive on the same line
467 # .endr on the same line is not valid, I don't know if a non-directive is
468 if ($num_repts =~ s/(\.\w+.*)//) {
469 $rept_lines .= "$1\n";
471 $num_repts = eval($num_repts);
472 } elsif ($line =~ /\.irp\s+([\d\w\.]+)\s*(.*)/) {
478 # only use whitespace as the separator
479 my $irp_arglist = $2;
480 $irp_arglist =~ s/,/ /g;
481 $irp_arglist =~ s/^\s+//;
482 @irp_args = split(/\s+/, $irp_arglist);
483 } elsif ($line =~ /\.irpc\s+([\d\w\.]+)\s*(.*)/) {
489 my $irp_arglist = $2;
490 $irp_arglist =~ s/,/ /g;
491 $irp_arglist =~ s/^\s+//;
492 @irp_args = split(//, $irp_arglist);
493 } elsif ($line =~ /\.endr/) {
495 foreach my $i (@irp_args) {
496 my $line = $rept_lines;
497 $line =~ s/\\$irp_param/$i/g;
498 $line =~ s/\\\(\)//g; # remove \()
502 for (1 .. $num_repts) {
503 print ASMFILE $rept_lines;
509 } elsif ($rept_lines) {
510 $rept_lines .= $line;
516 print ASMFILE ".text\n";
517 print ASMFILE ".align 2\n";
518 foreach my $literal (keys %literal_labels) {
519 print ASMFILE "$literal_labels{$literal}:\n .word $literal\n";
522 map print(ASMFILE ".thumb_func $_\n"),
523 grep exists $thumb_labels{$_}, keys %call_targets;
525 close(ASMFILE) or exit 1;