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 Windows prologue and epilogue automatically. If you want
34 # to take care of ABI differences yourself, tag functions as
35 # ".type name,@abi-omnipotent."
36 # 4. As minor optimization you can specify number of input arguments
37 # as ".type name,@function,N." Keep in mind that if N is larger
38 # than 6, then you *have to* write "abi-omnipotent" code, because
39 # it can't be resolved with unified prologue.
40 # 5. Name local labels as .L*.
41 # 6. Don't use repret, it's generated automatically.
44 open STDOUT,">$output" || die "can't open $output: $!";
46 my $masm=1 if ($output =~ /\.asm/);
51 { package opcode; # pick up opcodes
53 my $self = shift; # single instance in enough...
57 if ($line =~ /^([a-z]+)/i) {
60 $line = substr($line,@+[0]); $line =~ s/^\s+//;
63 if ($self->{op} =~ /(movz)b.*/) { # movz is pain...
66 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])/) {
76 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
82 if ($self->{op} eq "movz") { # movz in pain...
83 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
84 } elsif ($self->{op} eq "ret") {
87 "$self->{op}$self->{sz}";
90 $self->{op} =~ s/movz/movzx/;
91 if ($self->{op} eq "ret") {
93 if ($current_function->{abi} eq "svr4") {
94 $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t".
95 "mov rsi,QWORD PTR 16[rsp]\n\t";
97 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
103 { package const; # pick up constants, which start with $
105 my $self = shift; # single instance in enough...
109 if ($line =~ /^\$([^,]+)/) {
112 $line = substr($line,@+[0]); $line =~ s/^\s+//;
118 sprintf $masm?"%s":"\$%s",$self->{value};
121 { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
123 my $self = shift; # single instance in enough...
127 if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) {
129 ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
130 $self->{scale} = 1 if (!defined($self->{scale}));
132 $line = substr($line,@+[0]); $line =~ s/^\s+//;
134 $self->{label} =~ s/\.L/\$L/g;
135 $self->{base} =~ s/^%//;
136 $self->{index} =~ s/^%// if (defined($self->{index}));
146 if (defined($self->{index})) {
147 sprintf "%s(%%%s,%%%s,%d)", $self->{label},$self->{base},
148 $self->{index},$self->{scale};
151 sprintf "%s(%%%s)", $self->{label},$self->{base};
154 %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" );
156 if (defined($self->{index})) {
157 sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz},
159 $self->{index},$self->{scale},
163 sprintf "%s PTR %s[%s]",$szmap{$sz},
164 $self->{label},$self->{base};
169 { package register; # pick up registers, which start with %.
171 my $class = shift; # muliple instances...
176 if ($line =~ /^%(\w+)/) {
180 $line = substr($line,@+[0]); $line =~ s/^\s+//;
188 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
189 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
190 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
191 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
192 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
193 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
194 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
195 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
201 sprintf $masm?"%s":"%%%s",$self->{value};
204 { package label; # pick up labels, which end with :
206 my $self = shift; # single instance is enough...
210 if ($line =~ /(^[\.\w]+\:)/) {
213 $line = substr($line,@+[0]); $line =~ s/^\s+//;
215 $self->{value} =~ s/\.L/\$L/ if ($masm);
224 } elsif ($self->{value} ne "$current_function->{name}:") {
226 } elsif ($current_function->{abi} eq "svr4") {
227 my $func = "$current_function->{name} PROC\n".
228 " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n".
229 " mov QWORD PTR 16[rsp],rsi\n";
230 my $narg = $current_function->{narg};
231 $narg=6 if (!defined($narg));
232 $func .= " mov rdi,rcx\n" if ($narg>0);
233 $func .= " mov rsi,rdx\n" if ($narg>1);
234 $func .= " mov rdx,r8\n" if ($narg>2);
235 $func .= " mov rcx,r9\n" if ($narg>3);
236 $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4);
237 $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5);
240 "$current_function->{name} PROC";
244 { package expr; # pick up expressioins
246 my $self = shift; # single instance is enough...
250 if ($line =~ /(^[^,]+)/) {
253 $line = substr($line,@+[0]); $line =~ s/^\s+//;
255 $self->{value} =~ s/\.L/\$L/g if ($masm);
264 { package directive; # pick up directives, which start with .
266 my $self = shift; # single instance is enough...
271 if ($line =~ /^\s*(\.\w+)/) {
274 $line =~ s/\@abi\-omnipotent/\@function/;
275 $line =~ s/\@function.*/\@function/;
276 $self->{value} = $line;
283 undef $self->{value};
284 $line = substr($line,@+[0]); $line =~ s/^\s+//;
288 $v="$current_segment\tENDS\n" if ($current_segment);
289 $current_segment = "_$1";
290 $current_segment =~ tr/[a-z]/[A-Z]/;
291 $v.="$current_segment\tSEGMENT PARA";
295 /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; };
296 /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
297 if ($type eq "\@function")
298 { undef $current_function;
299 $current_function->{name} = $sym;
300 $current_function->{abi} = "svr4";
301 $current_function->{narg} = $narg;
303 elsif ($type eq "\@abi-omnipotent")
304 { undef $current_function;
305 $current_function->{name} = $sym;
309 /\.size/ && do { if (defined($current_function))
310 { $self->{value}="$current_function->{name}\tENDP";
311 undef $current_function;
315 /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
316 /\.(byte|value|long|quad)/
317 && do { my @arr = split(',',$line);
318 my $sz = substr($1,0,1);
319 my $last = pop(@arr);
321 $sz =~ tr/bvlq/BWDQ/;
322 $self->{value} = "\tD$sz\t";
323 for (@arr) { $self->{value} .= sprintf"0%Xh,",oct; }
324 $self->{value} .= sprintf"0%Xh",oct($last);
343 $line =~ s/\[#!].*$//; # get rid of comments...
344 $line =~ s/^\s+//; # ... and skip white spaces
352 if ($label=label->re(\$line)) { print $label->out(); }
354 if (directive->re(\$line)) {
355 printf "%s",directive->out();
356 } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: {
358 if ($src=register->re(\$line)) { opcode->size($src->size()); }
359 elsif ($src=const->re(\$line)) { }
360 elsif ($src=ea->re(\$line)) { }
361 elsif ($src=expr->re(\$line)) { }
363 last ARGUMENT if ($line !~ /^,/);
365 $line = substr($line,1); $line =~ s/^\s+//;
367 if ($dst=register->re(\$line)) { opcode->size($dst->size()); }
368 elsif ($dst=const->re(\$line)) { }
369 elsif ($dst=ea->re(\$line)) { }
377 printf "\t%s\t%s,%s", $opcode->out($dst->size()),
378 $src->out($sz),$dst->out($sz);
381 printf "\t%s\t%s,%s", $opcode->out(),
382 $dst->out($sz),$src->out($sz);
385 elsif (defined($src)) {
386 printf "\t%s\t%s",$opcode->out(),$src->out($sz);
388 printf "\t%s",$opcode->out();
395 print "\n$current_segment\tENDS\nEND\n" if ($masm);
399 #################################################
400 # Cross-reference x86_64 ABI "card"
420 # (*) volatile register
421 # (-) preserved by callee
422 # (#) Nth argument, volatile
424 # In Unix terms top of stack is argument transfer area for arguments
425 # which could not be accomodated in registers. Or in other words 7th
426 # [integer] argument resides at 8(%rsp) upon function entry point.
427 # 128 bytes above %rsp constitute a "red zone" which is not touched
428 # by signal handlers and can be used as temporal storage without
429 # allocating a frame.
431 # In Win64 terms N*8 bytes on top of stack is argument transfer area,
432 # which belongs to/can be overwritten by callee. N is the number of
433 # arguments passed to callee, *but* not less than 4! This means that
434 # upon function entry point 5th argument resides at 40(%rsp), as well
435 # as that 32 bytes from 8(%rsp) can always be used as temporal
436 # storage [without allocating a frame].
438 # All the above means that if assembler programmer adheres to Unix
439 # register and stack layout, but disregards the "red zone" existense,
440 # it's possible to use following prologue and epilogue to "gear" from
441 # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
443 # omnipotent_function:
447 # movq %rcx,%rdi ; if 1st argument is actually present
448 # movq %rdx,%rsi ; if 2nd argument is actually ...
449 # movq %r8,%rdx ; if 3rd argument is ...
450 # movq %r9,%rcx ; if 4th argument ...
451 # movq 40(%rsp),%r8 ; if 5th ...
452 # movq 48(%rsp),%r9 ; if 6th ...