3 # Ascetic x86_64 AT&T to MASM assembler translator by <appro>.
5 # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
6 # format is way easier to parse. Because it's simpler to "gear" from
7 # Unix ABI to Windows one [see cross-reference "card" at the end of
8 # file]. Because Linux targets were available first...
10 # In addition the script also "distills" code suitable for GNU
11 # assembler, so that it can be compiled with more rigid assemblers,
12 # such as Solaris /usr/ccs/bin/as.
14 # This translator is not designed to convert *arbitrary* assembler
15 # code from AT&T format to MASM one. It's designed to convert just
16 # enough to provide for dual-ABI OpenSSL modules development...
17 # There *are* limitations and you might have to modify your assembler
18 # code or this script to achieve the desired result...
20 # Currently recognized limitations:
22 # - can't use multiple ops per line;
23 # - indirect calls and jumps are not supported;
25 # Dual-ABI styling rules.
27 # 1. Adhere to Unix register and stack layout [see the end for
29 # 2. Forget about "red zone," stick to more traditional blended
30 # stack frame allocation. If volatile storage is actually required
31 # that is. If not, just leave the stack as is.
32 # 3. Functions tagged with ".type name,@function" get crafted with
33 # unified Win64 prologue and epilogue automatically. If you want
34 # to take care of ABI differences yourself, tag functions as
35 # ".type name,@abi-omnipotent" instead.
36 # 4. To optimize the Win64 prologue you can specify number of input
37 # arguments as ".type name,@function,N." Keep in mind that if N is
38 # larger than 6, then you *have to* write "abi-omnipotent" code,
39 # because >6 cases can't be addressed with unified prologue.
40 # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
41 # (sorry about latter).
42 # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
43 # required to identify the spots, where to inject Win64 epilogue!
44 # But on the pros, it's then prefixed with rep automatically:-)
47 open STDOUT,">$output" || die "can't open $output: $!";
49 my $masm=1 if ($output =~ /\.asm/);
54 { package opcode; # pick up opcodes
56 my $self = shift; # single instance in enough...
60 if ($line =~ /^([a-z]+)/i) {
63 $line = substr($line,@+[0]); $line =~ s/^\s+//;
66 if ($self->{op} =~ /(movz)b.*/) { # movz is pain...
69 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])/) {
79 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
85 if ($self->{op} eq "movz") { # movz in pain...
86 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
87 } elsif ($self->{op} eq "ret") {
90 "$self->{op}$self->{sz}";
93 $self->{op} =~ s/movz/movzx/;
94 if ($self->{op} eq "ret") {
96 if ($current_function->{abi} eq "svr4") {
97 $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t".
98 "mov rsi,QWORD PTR 16[rsp]\n\t";
100 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
106 { package const; # pick up constants, which start with $
108 my $self = shift; # single instance in enough...
112 if ($line =~ /^\$([^,]+)/) {
115 $line = substr($line,@+[0]); $line =~ s/^\s+//;
121 sprintf $masm?"%s":"\$%s",$self->{value};
124 { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
126 my $self = shift; # single instance in enough...
130 if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) {
132 ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
133 $self->{scale} = 1 if (!defined($self->{scale}));
135 $line = substr($line,@+[0]); $line =~ s/^\s+//;
137 $self->{label} =~ s/\.L/\$L/g;
138 $self->{base} =~ s/^%//;
139 $self->{index} =~ s/^%// if (defined($self->{index}));
149 # elder GNU assembler insists on 64-bit EAs:-(
150 # on pros side, this results in more compact code:-)
151 $self->{index} =~ s/^[er](.?[0-9xp])[d]?$/r\1/;
152 $self->{base} =~ s/^[er](.?[0-9xp])[d]?$/r\1/;
154 if (defined($self->{index})) {
155 sprintf "%s(%%%s,%%%s,%d)", $self->{label},$self->{base},
156 $self->{index},$self->{scale};
159 sprintf "%s(%%%s)", $self->{label},$self->{base};
162 %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" );
164 if (defined($self->{index})) {
165 sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz},
167 $self->{index},$self->{scale},
171 sprintf "%s PTR %s[%s]",$szmap{$sz},
172 $self->{label},$self->{base};
177 { package register; # pick up registers, which start with %.
179 my $class = shift; # muliple instances...
184 if ($line =~ /^%(\w+)/) {
188 $line = substr($line,@+[0]); $line =~ s/^\s+//;
196 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
197 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
198 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
199 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
200 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
201 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
202 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
203 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
209 sprintf $masm?"%s":"%%%s",$self->{value};
212 { package label; # pick up labels, which end with :
214 my $self = shift; # single instance is enough...
218 if ($line =~ /(^[\.\w]+\:)/) {
221 $line = substr($line,@+[0]); $line =~ s/^\s+//;
223 $self->{value} =~ s/\.L/\$L/ if ($masm);
232 } elsif ($self->{value} ne "$current_function->{name}:") {
234 } elsif ($current_function->{abi} eq "svr4") {
235 my $func = "$current_function->{name} PROC\n".
236 " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n".
237 " mov QWORD PTR 16[rsp],rsi\n";
238 my $narg = $current_function->{narg};
239 $narg=6 if (!defined($narg));
240 $func .= " mov rdi,rcx\n" if ($narg>0);
241 $func .= " mov rsi,rdx\n" if ($narg>1);
242 $func .= " mov rdx,r8\n" if ($narg>2);
243 $func .= " mov rcx,r9\n" if ($narg>3);
244 $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4);
245 $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5);
248 "$current_function->{name} PROC";
252 { package expr; # pick up expressioins
254 my $self = shift; # single instance is enough...
258 if ($line =~ /(^[^,]+)/) {
261 $line = substr($line,@+[0]); $line =~ s/^\s+//;
263 $self->{value} =~ s/\.L/\$L/g if ($masm);
272 { package directive; # pick up directives, which start with .
274 my $self = shift; # single instance is enough...
279 if ($line =~ /^\s*(\.\w+)/) {
282 $line =~ s/\@abi\-omnipotent/\@function/;
283 $line =~ s/\@function.*/\@function/;
284 $self->{value} = $line;
291 undef $self->{value};
292 $line = substr($line,@+[0]); $line =~ s/^\s+//;
296 $v="$current_segment\tENDS\n" if ($current_segment);
297 $current_segment = "_$1";
298 $current_segment =~ tr/[a-z]/[A-Z]/;
299 $v.="$current_segment\tSEGMENT PARA";
303 /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; };
304 /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
305 if ($type eq "\@function")
306 { undef $current_function;
307 $current_function->{name} = $sym;
308 $current_function->{abi} = "svr4";
309 $current_function->{narg} = $narg;
311 elsif ($type eq "\@abi-omnipotent")
312 { undef $current_function;
313 $current_function->{name} = $sym;
317 /\.size/ && do { if (defined($current_function))
318 { $self->{value}="$current_function->{name}\tENDP";
319 undef $current_function;
323 /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
324 /\.(byte|value|long|quad)/
325 && do { my @arr = split(',',$line);
326 my $sz = substr($1,0,1);
327 my $last = pop(@arr);
329 $sz =~ tr/bvlq/BWDQ/;
330 $self->{value} = "\tD$sz\t";
331 for (@arr) { $self->{value} .= sprintf"0%Xh,",oct; }
332 $self->{value} .= sprintf"0%Xh",oct($last);
351 $line =~ s|[#!].*$||; # get rid of asm-style comments...
352 $line =~ s|/\*.*\*/||; # ... and C-style comments...
353 $line =~ s|^\s+||; # ... and skip white spaces in beginning
361 if ($label=label->re(\$line)) { print $label->out(); }
363 if (directive->re(\$line)) {
364 printf "%s",directive->out();
365 } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: {
367 if ($src=register->re(\$line)) { opcode->size($src->size()); }
368 elsif ($src=const->re(\$line)) { }
369 elsif ($src=ea->re(\$line)) { }
370 elsif ($src=expr->re(\$line)) { }
372 last ARGUMENT if ($line !~ /^,/);
374 $line = substr($line,1); $line =~ s/^\s+//;
376 if ($dst=register->re(\$line)) { opcode->size($dst->size()); }
377 elsif ($dst=const->re(\$line)) { }
378 elsif ($dst=ea->re(\$line)) { }
386 printf "\t%s\t%s,%s", $opcode->out($dst->size()),
387 $src->out($sz),$dst->out($sz);
390 printf "\t%s\t%s,%s", $opcode->out(),
391 $dst->out($sz),$src->out($sz);
394 elsif (defined($src)) {
395 printf "\t%s\t%s",$opcode->out(),$src->out($sz);
397 printf "\t%s",$opcode->out();
404 print "\n$current_segment\tENDS\nEND\n" if ($masm);
408 #################################################
409 # Cross-reference x86_64 ABI "card"
429 # (*) volatile register
430 # (-) preserved by callee
431 # (#) Nth argument, volatile
433 # In Unix terms top of stack is argument transfer area for arguments
434 # which could not be accomodated in registers. Or in other words 7th
435 # [integer] argument resides at 8(%rsp) upon function entry point.
436 # 128 bytes above %rsp constitute a "red zone" which is not touched
437 # by signal handlers and can be used as temporal storage without
438 # allocating a frame.
440 # In Win64 terms N*8 bytes on top of stack is argument transfer area,
441 # which belongs to/can be overwritten by callee. N is the number of
442 # arguments passed to callee, *but* not less than 4! This means that
443 # upon function entry point 5th argument resides at 40(%rsp), as well
444 # as that 32 bytes from 8(%rsp) can always be used as temporal
445 # storage [without allocating a frame].
447 # All the above means that if assembler programmer adheres to Unix
448 # register and stack layout, but disregards the "red zone" existense,
449 # it's possible to use following prologue and epilogue to "gear" from
450 # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
452 # omnipotent_function:
456 # movq %rcx,%rdi ; if 1st argument is actually present
457 # movq %rdx,%rsi ; if 2nd argument is actually ...
458 # movq %r8,%rdx ; if 3rd argument is ...
459 # movq %r9,%rcx ; if 4th argument ...
460 # movq 40(%rsp),%r8 ; if 5th ...
461 # movq 48(%rsp),%r9 ; if 6th ...