forked from yuvi/gas-preprocessor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gas-preprocessor.pl
executable file
·632 lines (553 loc) · 18.7 KB
/
gas-preprocessor.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#!/usr/bin/env perl
# by David Conrad
# This code is licensed under GPLv2 or later; go to gnu.org to read it
# (not that it much matters for an asm preprocessor)
# usage: set your assembler to be something like "perl gas-preprocessor.pl gcc"
use strict;
# Apple's gas is ancient and doesn't support modern preprocessing features like
# .rept and has ugly macro syntax, among other things. Thus, this script
# implements the subset of the gas preprocessor used by x264 and ffmpeg
# that isn't supported by Apple's gas.
my @gcc_cmd = @ARGV;
my @preprocess_c_cmd;
my $fix_unreq = $^O eq "darwin";
if ($gcc_cmd[0] eq "-fix-unreq") {
$fix_unreq = 1;
shift @gcc_cmd;
} elsif ($gcc_cmd[0] eq "-no-fix-unreq") {
$fix_unreq = 0;
shift @gcc_cmd;
}
if (grep /\.c$/, @gcc_cmd) {
# C file (inline asm?) - compile
@preprocess_c_cmd = (@gcc_cmd, "-S");
} elsif (grep /\.[sS]$/, @gcc_cmd) {
# asm file, just do C preprocessor
@preprocess_c_cmd = (@gcc_cmd, "-E");
} else {
die "Unrecognized input filetype";
}
# if compiling, avoid creating an output file named '-.o'
if ((grep /^-c$/, @gcc_cmd) && !(grep /^-o/, @gcc_cmd)) {
foreach my $i (@gcc_cmd) {
if ($i =~ /\.[csS]$/) {
my $outputfile = $i;
$outputfile =~ s/\.[csS]$/.o/;
push(@gcc_cmd, "-o");
push(@gcc_cmd, $outputfile);
last;
}
}
}
@gcc_cmd = map { /\.[csS]$/ ? qw(-x assembler -) : $_ } @gcc_cmd;
@preprocess_c_cmd = map { /\.o$/ ? "-" : $_ } @preprocess_c_cmd;
my $comm;
my $aarch64 = 0;
# detect architecture from gcc binary name
if ($gcc_cmd[0] =~ /arm64|aarch64/) {
$comm = '//';
$aarch64 = 1;
} elsif ($gcc_cmd[0] =~ /arm/) {
$comm = '@';
} elsif ($gcc_cmd[0] =~ /powerpc|ppc/) {
$comm = '#';
}
# look for -arch flag
foreach my $i (1 .. $#gcc_cmd-1) {
if ($gcc_cmd[$i] eq "-arch") {
if ($gcc_cmd[$i+1] =~ /arm64|aarch64/) {
$comm = '//';
$aarch64 = 1;
} elsif ($gcc_cmd[$i+1] =~ /arm/) {
$comm = '@';
} elsif ($gcc_cmd[$i+1] =~ /powerpc|ppc/) {
$comm = '#';
}
}
}
# assume we're not cross-compiling if no -arch or the binary doesn't have the arch name
if (!$comm) {
my $native_arch = qx/arch/;
if ($native_arch =~ /arm64|aarch64/) {
$comm = '//';
$aarch64 = 1;
} elsif ($native_arch =~ /arm/) {
$comm = '@';
} elsif ($native_arch =~ /powerpc|ppc/) {
$comm = '#';
}
}
if (!$comm) {
die "Unable to identify target architecture";
}
my %ppc_spr = (ctr => 9,
vrsave => 256);
open(ASMFILE, "-|", @preprocess_c_cmd) || die "Error running preprocessor";
my $current_macro = '';
my $macro_level = 0;
my %macro_lines;
my %macro_args;
my %macro_args_default;
my $macro_count = 0;
my $altmacro = 0;
my $in_irp = 0;
my @pass1_lines;
my @ifstack;
my %symbols;
# pass 1: parse .macro
# note that the handling of arguments is probably overly permissive vs. gas
# but it should be the same for valid cases
while (<ASMFILE>) {
# remove all comments (to avoid interfering with evaluating directives)
s/(?<!\\)$comm.*//x;
# comment out unsupported directives
s/\.type/$comm$&/x;
s/\.func/$comm$&/x;
s/\.endfunc/$comm$&/x;
s/\.ltorg/$comm$&/x;
s/\.size/$comm$&/x;
s/\.fpu/$comm$&/x;
s/\.arch/$comm$&/x;
s/\.object_arch/$comm$&/x;
# the syntax for these is a little different
s/\.global/.globl/x;
# also catch .section .rodata since the equivalent to .const_data is .section __DATA,__const
s/(.*)\.rodata/.const_data/x;
s/\.int/.long/x;
s/\.float/.single/x;
# catch unknown section names that aren't mach-o style (with a comma)
if (/.section ([^,]*)$/) {
die ".section $1 unsupported; figure out the mach-o section name and add it";
}
parse_line($_);
}
sub eval_expr {
my $expr = $_[0];
while ($expr =~ /([A-Za-z._][A-Za-z0-9._]*)/g) {
my $sym = $1;
$expr =~ s/$sym/$symbols{$sym}/ if defined $symbols{$sym};
}
eval $expr;
}
sub handle_if {
my $line = $_[0];
# handle .if directives; apple's assembler doesn't support important non-basic ones
# evaluating them is also needed to handle recursive macros
if ($line =~ /\.if(n?)([a-z]*)\s+(.*)/) {
my $result = $1 eq "n";
my $type = $2;
my $expr = $3;
if ($type eq "b") {
$expr =~ s/\s//g;
$result ^= $expr eq "";
} elsif ($type eq "c") {
if ($expr =~ /(.*)\s*,\s*(.*)/) {
$result ^= $1 eq $2;
} else {
die "argument to .ifc not recognized";
}
} elsif ($type eq "") {
$result ^= eval_expr($expr) != 0;
} elsif ($type eq "eq") {
$result = eval_expr($expr) == 0;
} elsif ($type eq "lt") {
$result = eval_expr($expr) < 0;
} else {
chomp($line);
die "unhandled .if varient. \"$line\"";
}
push (@ifstack, $result);
return 1;
} else {
return 0;
}
}
sub parse_if_line {
my $line = @_[0];
# evaluate .if blocks
if (scalar(@ifstack)) {
if ($line =~ /\.endif/) {
pop(@ifstack);
return 1;
} elsif ($line =~ /\.elseif\s+(.*)/) {
if ($ifstack[-1] == 0) {
$ifstack[-1] = !!eval_expr($1);
} elsif ($ifstack[-1] > 0) {
$ifstack[-1] = -$ifstack[-1];
}
return 1;
} elsif ($line =~ /\.else/) {
$ifstack[-1] = !$ifstack[-1];
return 1;
} elsif (handle_if($line)) {
return 1;
}
# discard lines in false .if blocks
foreach my $i (0 .. $#ifstack) {
if ($ifstack[$i] <= 0) {
return 1;
}
}
}
return 0;
}
sub parse_line {
my $line = @_[0];
return if (parse_if_line($line));
if (/\.macro/) {
$macro_level++;
if ($macro_level > 1 && !$current_macro) {
die "nested macros but we don't have master macro";
}
} elsif (/\.endm/) {
$macro_level--;
if ($macro_level < 0) {
die "unmatched .endm";
} elsif ($macro_level == 0) {
$current_macro = '';
return;
}
} elsif (/\.irp/ or /\.rept/) {
$in_irp = 1;
} elsif (/.endr/) {
$in_irp = 0;
}
if ($macro_level > 1) {
push(@{$macro_lines{$current_macro}}, $line);
} elsif ($macro_level == 0) {
expand_macros($line);
} else {
if ($line =~ /\.macro\s+([\d\w\.]+)\s*(.*)/) {
$current_macro = $1;
# commas in the argument list are optional, so only use whitespace as the separator
my $arglist = $2;
$arglist =~ s/,/ /g;
my @args = split(/\s+/, $arglist);
foreach my $i (0 .. $#args) {
my @argpair = split(/=/, $args[$i]);
$macro_args{$current_macro}[$i] = $argpair[0];
$argpair[0] =~ s/:vararg$//;
$macro_args_default{$current_macro}{$argpair[0]} = $argpair[1];
}
# ensure %macro_lines has the macro name added as a key
$macro_lines{$current_macro} = [];
} elsif ($current_macro) {
push(@{$macro_lines{$current_macro}}, $line);
} else {
die "macro level without a macro name";
}
}
}
sub handle_set {
my $line = $_[0];
if ($line =~ /\.set\s+(.*),\s*(.*)/) {
$symbols{$1} = eval_expr($2);
}
}
sub expand_macros {
my $line = @_[0];
# handle .if directives; apple's assembler doesn't support important non-basic ones
# evaluating them is also needed to handle recursive macros
if (!$in_irp && handle_if($line)) {
return;
}
if (/\.purgem\s+([\d\w\.]+)/) {
delete $macro_lines{$1};
delete $macro_args{$1};
delete $macro_args_default{$1};
return;
}
if ($line =~ /\.altmacro/) {
$altmacro = 1;
return;
}
if ($line =~ /\.noaltmacro/) {
$altmacro = 0;
return;
}
$line =~ s/\%([^,]*)/eval_expr($1)/eg if $altmacro;
handle_set($line);
if ($line =~ /(\S+:|)\s*([\w\d\.]+)\s*(.*)/ && exists $macro_lines{$2}) {
push(@pass1_lines, $1);
my $macro = $2;
# commas are optional here too, but are syntactically important because
# parameters can be blank
my @arglist = split(/,/, $3);
my @args;
my @args_seperator;
my $comma_sep_required = 0;
foreach (@arglist) {
# allow arithmetic/shift operators in macro arguments
$_ =~ s/\s*(\+|-|\*|\/|<<|>>)\s*/$1/g;
my @whitespace_split = split(/\s+/, $_);
if (!@whitespace_split) {
push(@args, '');
push(@args_seperator, '');
} else {
foreach (@whitespace_split) {
#print ("arglist = \"$_\"\n");
if (length($_)) {
push(@args, $_);
my $sep = $comma_sep_required ? "," : " ";
push(@args_seperator, $sep);
#print ("sep = \"$sep\", arg = \"$_\"\n");
$comma_sep_required = 0;
}
}
}
$comma_sep_required = 1;
}
my %replacements;
if ($macro_args_default{$macro}){
%replacements = %{$macro_args_default{$macro}};
}
# construct hashtable of text to replace
foreach my $i (0 .. $#args) {
my $argname = $macro_args{$macro}[$i];
my @macro_args = @{ $macro_args{$macro} };
if ($args[$i] =~ m/=/) {
# arg=val references the argument name
# XXX: I'm not sure what the expected behaviour if a lot of
# these are mixed with unnamed args
my @named_arg = split(/=/, $args[$i]);
$replacements{$named_arg[0]} = $named_arg[1];
} elsif ($i > $#{$macro_args{$macro}}) {
# more args given than the macro has named args
# XXX: is vararg allowed on arguments before the last?
$argname = $macro_args{$macro}[-1];
if ($argname =~ s/:vararg$//) {
#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";
#$replacements{$argname} .= ", $args[$i]";
$replacements{$argname} .= "$args_seperator[$i] $args[$i]";
} else {
die "Too many arguments to macro $macro";
}
} else {
$argname =~ s/:vararg$//;
$replacements{$argname} = $args[$i];
}
}
my $count = $macro_count++;
# apply replacements as regex
foreach (@{$macro_lines{$macro}}) {
my $macro_line = $_;
# do replacements by longest first, this avoids wrong replacement
# when argument names are subsets of each other
foreach (reverse sort {length $a <=> length $b} keys %replacements) {
$macro_line =~ s/\\$_/$replacements{$_}/g;
}
$macro_line =~ s/\\\@/$count/g;
$macro_line =~ s/\\\(\)//g; # remove \()
parse_line($macro_line);
}
} else {
push(@pass1_lines, $line);
}
}
close(ASMFILE) or exit 1;
if ($ENV{GASPP_DEBUG}) {
open(ASMFILE, ">&STDOUT");
} else {
open(ASMFILE, "|-", @gcc_cmd) or die "Error running assembler";
}
my @sections;
my $num_repts;
my @rept_lines;
my %literal_labels; # for ldr <reg>, =<expr>
my $literal_num = 0;
my $thumb = 0;
my %thumb_labels;
my %call_targets;
my @irp_args;
my $irp_param;
my %neon_alias_reg;
my %neon_alias_type;
my %aarch64_req_alias;
# pass 2: parse .rept and .if variants
foreach my $line (@pass1_lines) {
# handle .previous (only with regard to .section not .subsection)
if ($line =~ /\.(section|text|const_data)/) {
push(@sections, $line);
} elsif ($line =~ /\.previous/) {
if (!$sections[-2]) {
die ".previous without a previous section";
}
$line = $sections[-2];
push(@sections, $line);
}
$thumb = 1 if $line =~ /\.code\s+16|\.thumb/;
$thumb = 0 if $line =~ /\.code\s+32|\.arm/;
# handle ldr <reg>, =<expr>
if ($line =~ /(.*)\s*ldr([\w\s\d]+)\s*,\s*=(.*)/) {
my $label = $literal_labels{$3};
if (!$label) {
$label = "Literal_$literal_num";
$literal_num++;
$literal_labels{$3} = $label;
}
$line = "$1 ldr$2, $label\n";
} elsif ($line =~ /\.ltorg/) {
$line .= ".align 2\n";
foreach my $literal (keys %literal_labels) {
$line .= "$literal_labels{$literal}:\n .word $literal\n";
}
%literal_labels = ();
}
# handle GNU as pc-relative relocations for adrp/add
if ($line =~ /(.*)\s*adrp([\w\s\d]+)\s*,\s*#:pg_hi21:([^\s]+)/) {
$line = "$1 adrp$2, ${3}\@PAGE\n";
} elsif ($line =~ /(.*)\s*add([\w\s\d]+)\s*,([\w\s\d]+)\s*,\s*#:lo12:([^\s]+)/) {
$line = "$1 add$2, $3, ${4}\@PAGEOFF\n";
}
# thumb add with large immediate needs explicit add.w
if ($thumb and $line =~ /add\s+.*#([^@]+)/) {
$line =~ s/add/add.w/ if eval_expr($1) > 255;
}
# mach-o local symbol names start with L (no dot)
$line =~ s/(?<!\w)\.(L\w+)/$1/g;
if ($thumb and $line =~ /^\s*(\w+)\s*:/) {
$thumb_labels{$1}++;
}
if ($line =~ /^\s*((\w+\s*:\s*)?bl?x?(?:..)?(?:\.w)?|\.globl)\s+(\w+)/) {
if (exists $thumb_labels{$3}) {
print ASMFILE ".thumb_func $3\n";
} else {
$call_targets{$3}++;
}
}
# @l -> lo16() @ha -> ha16()
$line =~ s/,\s+([^,]+)\@l\b/, lo16($1)/g;
$line =~ s/,\s+([^,]+)\@ha\b/, ha16($1)/g;
# move to/from SPR
if ($line =~ /(\s+)(m[ft])([a-z]+)\s+(\w+)/ and exists $ppc_spr{$3}) {
if ($2 eq 'mt') {
$line = "$1${2}spr $ppc_spr{$3}, $4\n";
} else {
$line = "$1${2}spr $4, $ppc_spr{$3}\n";
}
}
if ($line =~ /\.unreq\s+(.*)/) {
if (defined $neon_alias_reg{$1}) {
delete $neon_alias_reg{$1};
delete $neon_alias_type{$1};
next;
} elsif (defined $aarch64_req_alias{$1}) {
delete $aarch64_req_alias{$1};
next;
}
}
# old gas versions store upper and lower case names on .req,
# but they remove only one on .unreq
if ($fix_unreq) {
if ($line =~ /\.unreq\s+(.*)/) {
$line = ".unreq " . lc($1) . "\n";
$line .= ".unreq " . uc($1) . "\n";
}
}
if ($line =~ /(\w+)\s+\.(dn|qn)\s+(\w+)(?:\.(\w+))?(\[\d+\])?/) {
$neon_alias_reg{$1} = "$3$5";
$neon_alias_type{$1} = $4;
next;
}
if (scalar keys %neon_alias_reg > 0 && $line =~ /^\s+v\w+/) {
# This line seems to possibly have a neon instruction
foreach (keys %neon_alias_reg) {
my $alias = $_;
# Require the register alias to match as an invididual word, not as a substring
# of a larger word-token.
if ($line =~ /\b$alias\b/) {
$line =~ s/\b$alias\b/$neon_alias_reg{$alias}/g;
# Add the type suffix. If multiple aliases match on the same line,
# only do this replacement the first time (a vfoo.bar string won't match v\w+).
$line =~ s/^(\s+)(v\w+)(\s+)/$1$2.$neon_alias_type{$alias}$3/;
}
}
}
if ($aarch64) {
# clang's integrated aarch64 assembler in Xcode 5 does not support .req/.unreq
if ($line =~ /\b(\w+)\s+\.req\s+(\w+)\b/) {
$aarch64_req_alias{$1} = $2;
next;
}
foreach (keys %aarch64_req_alias) {
my $alias = $_;
# recursively resolve aliases
my $resolved = $aarch64_req_alias{$alias};
while (defined $aarch64_req_alias{$resolved}) {
$resolved = $aarch64_req_alias{$resolved};
}
$line =~ s/\b$alias\b/$resolved/g;
}
}
if ($line =~ /\.rept\s+(.*)/) {
$num_repts = $1;
@rept_lines = ("\n");
# handle the possibility of repeating another directive on the same line
# .endr on the same line is not valid, I don't know if a non-directive is
if ($num_repts =~ s/(\.\w+.*)//) {
push(@rept_lines, "$1\n");
}
$num_repts = eval_expr($num_repts);
} elsif ($line =~ /\.irp\s+([\d\w\.]+)\s*(.*)/) {
$in_irp = 1;
$num_repts = 1;
@rept_lines = ("\n");
$irp_param = $1;
# only use whitespace as the separator
my $irp_arglist = $2;
$irp_arglist =~ s/,/ /g;
$irp_arglist =~ s/^\s+//;
@irp_args = split(/\s+/, $irp_arglist);
} elsif ($line =~ /\.irpc\s+([\d\w\.]+)\s*(.*)/) {
$in_irp = 1;
$num_repts = 1;
@rept_lines = ("\n");
$irp_param = $1;
my $irp_arglist = $2;
$irp_arglist =~ s/,/ /g;
$irp_arglist =~ s/^\s+//;
@irp_args = split(//, $irp_arglist);
} elsif ($line =~ /\.endr/) {
if ($in_irp != 0) {
foreach my $i (@irp_args) {
foreach my $origline (@rept_lines) {
my $line = $origline;
$line =~ s/\\$irp_param/$i/g;
$line =~ s/\\\(\)//g; # remove \()
if (!parse_if_line($line) && !handle_if($line)) {
handle_set($line);
print ASMFILE $line;
}
}
}
} else {
for (1 .. $num_repts) {
foreach my $line (@rept_lines) {
if (!parse_if_line($line) && !handle_if($line)) {
handle_set($line);
print ASMFILE $line;
}
}
}
}
@rept_lines = ();
$in_irp = 0;
@irp_args = '';
} elsif (scalar(@rept_lines)) {
push(@rept_lines, $line);
} else {
handle_set($line);
print ASMFILE $line;
}
}
print ASMFILE ".text\n";
print ASMFILE ".align 2\n";
foreach my $literal (keys %literal_labels) {
my $label = $literal_labels{$literal};
print ASMFILE ".set Lval_$label, $literal\n";
print ASMFILE "$label: .word Lval_$label\n";
}
map print(ASMFILE ".thumb_func $_\n"),
grep exists $thumb_labels{$_}, keys %call_targets;
close(ASMFILE) or exit 1;
#exit 1