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:-)
45 # 7. Due to MASM limitations [and certain general counter-intuitivity
46 # of ip-relative addressing] generation of position-independent
47 # code is assisted by synthetic directive, .picmeup, which puts
48 # address of the *next* instruction into target register.
52 # lea .Label-.(%rax),%rax
57 # lea .Label-.Lpic_point(%rcx),%rbp
60 open STDOUT,">$output" || die "can't open $output: $!";
62 my $masm=1 if ($output =~ /\.asm/);
67 { package opcode; # pick up opcodes
69 my $self = shift; # single instance in enough...
73 if ($line =~ /^([a-z]+)/i) {
76 $line = substr($line,@+[0]); $line =~ s/^\s+//;
79 if ($self->{op} =~ /(movz)b.*/) { # movz is pain...
82 } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])/) {
92 $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
98 if ($self->{op} eq "movz") { # movz in pain...
99 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
100 } elsif ($self->{op} eq "ret") {
103 "$self->{op}$self->{sz}";
106 $self->{op} =~ s/movz/movzx/;
107 if ($self->{op} eq "ret") {
109 if ($current_function->{abi} eq "svr4") {
110 $self->{op} = "mov rdi,QWORD PTR 8[rsp]\t;WIN64 epilogue\n\t".
111 "mov rsi,QWORD PTR 16[rsp]\n\t";
113 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
119 { package const; # pick up constants, which start with $
121 my $self = shift; # single instance in enough...
125 if ($line =~ /^\$([^,]+)/) {
128 $line = substr($line,@+[0]); $line =~ s/^\s+//;
136 # Solaris /usr/ccs/bin/as can't handle multiplications
138 $self->{value} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi;
139 $self->{value} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
140 sprintf "\$%s",$self->{value};
142 $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig;
143 sprintf "%s",$self->{value};
147 { package ea; # pick up effective addresses: expr(%reg,%reg,scale)
149 my $self = shift; # single instance in enough...
153 if ($line =~ /^([^\(,]*)\(([%\w,]+)\)/) {
155 ($self->{base},$self->{index},$self->{scale})=split(/,/,$2);
156 $self->{scale} = 1 if (!defined($self->{scale}));
158 $line = substr($line,@+[0]); $line =~ s/^\s+//;
160 $self->{base} =~ s/^%//;
161 $self->{index} =~ s/^%// if (defined($self->{index}));
170 # silently convert all EAs to 64-bit, required for elder GNU
171 # assembler and results in more compact code
172 $self->{index} =~ s/^[er](.?[0-9xp])[d]?$/r\1/;
173 $self->{base} =~ s/^[er](.?[0-9xp])[d]?$/r\1/;
175 # Solaris /usr/ccs/bin/as can't handle multiplications
177 $self->{label} =~ s/(?<![0-9a-f])(0[x0-9a-f]+)/oct($1)/egi;
178 $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
180 if (defined($self->{index})) {
181 sprintf "%s(%%%s,%%%s,%d)",
182 $self->{label},$self->{base},
183 $self->{index},$self->{scale};
185 sprintf "%s(%%%s)", $self->{label},$self->{base};
188 %szmap = ( b=>"BYTE", w=>"WORD", l=>"DWORD", q=>"QWORD" );
190 $self->{label} =~ s/\./\$/g;
191 $self->{label} =~ s/0x([0-9a-f]+)/0$1h/ig;
192 $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
194 if (defined($self->{index})) {
195 sprintf "%s PTR %s[%s*%d+%s]",$szmap{$sz},
197 $self->{index},$self->{scale},
200 sprintf "%s PTR %s[%s]",$szmap{$sz},
201 $self->{label},$self->{base};
206 { package register; # pick up registers, which start with %.
208 my $class = shift; # muliple instances...
213 if ($line =~ /^%(\w+)/) {
217 $line = substr($line,@+[0]); $line =~ s/^\s+//;
225 if ($self->{value} =~ /^r[\d]+b$/i) { $ret="b"; }
226 elsif ($self->{value} =~ /^r[\d]+w$/i) { $ret="w"; }
227 elsif ($self->{value} =~ /^r[\d]+d$/i) { $ret="l"; }
228 elsif ($self->{value} =~ /^r[\w]+$/i) { $ret="q"; }
229 elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
230 elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
231 elsif ($self->{value} =~ /^[\w]{2}$/i) { $ret="w"; }
232 elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
238 sprintf $masm?"%s":"%%%s",$self->{value};
241 { package label; # pick up labels, which end with :
243 my $self = shift; # single instance is enough...
247 if ($line =~ /(^[\.\w]+\:)/) {
250 $line = substr($line,@+[0]); $line =~ s/^\s+//;
252 $self->{value} =~ s/\.L/\$L/ if ($masm);
261 } elsif ($self->{value} ne "$current_function->{name}:") {
263 } elsif ($current_function->{abi} eq "svr4") {
264 my $func = "$current_function->{name} PROC\n".
265 " mov QWORD PTR 8[rsp],rdi\t;WIN64 prologue\n".
266 " mov QWORD PTR 16[rsp],rsi\n";
267 my $narg = $current_function->{narg};
268 $narg=6 if (!defined($narg));
269 $func .= " mov rdi,rcx\n" if ($narg>0);
270 $func .= " mov rsi,rdx\n" if ($narg>1);
271 $func .= " mov rdx,r8\n" if ($narg>2);
272 $func .= " mov rcx,r9\n" if ($narg>3);
273 $func .= " mov r8,QWORD PTR 40[rsp]\n" if ($narg>4);
274 $func .= " mov r9,QWORD PTR 48[rsp]\n" if ($narg>5);
277 "$current_function->{name} PROC";
281 { package expr; # pick up expressioins
283 my $self = shift; # single instance is enough...
287 if ($line =~ /(^[^,]+)/) {
290 $line = substr($line,@+[0]); $line =~ s/^\s+//;
292 $self->{value} =~ s/\.L/\$L/g if ($masm);
301 { package directive; # pick up directives, which start with .
303 my $self = shift; # single instance is enough...
307 my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
308 ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
309 "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
310 "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
311 "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
312 "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
313 "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
314 "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
315 "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c );
317 if ($line =~ /^\s*(\.\w+)/) {
320 $line =~ s/\@abi\-omnipotent/\@function/;
321 $line =~ s/\@function.*/\@function/;
322 if ($line =~ /\.picmeup\s+(%r[\w]+)/i) {
323 $self->{value} = sprintf "\t.long\t0x%x,0x90000000",$opcode{$1};
325 $self->{value} = $line;
333 undef $self->{value};
334 $line = substr($line,@+[0]); $line =~ s/^\s+//;
338 $v="$current_segment\tENDS\n" if ($current_segment);
339 $current_segment = "_$1\$";
340 $current_segment =~ tr/[a-z]/[A-Z]/;
341 $v.="$current_segment\tSEGMENT ALIGN(64) 'CODE'";
345 /\.globl/ && do { $self->{value} = "PUBLIC\t".$line; last; };
346 /\.type/ && do { ($sym,$type,$narg) = split(',',$line);
347 if ($type eq "\@function") {
348 undef $current_function;
349 $current_function->{name} = $sym;
350 $current_function->{abi} = "svr4";
351 $current_function->{narg} = $narg;
352 } elsif ($type eq "\@abi-omnipotent") {
353 undef $current_function;
354 $current_function->{name} = $sym;
358 /\.size/ && do { if (defined($current_function)) {
359 $self->{value}="$current_function->{name}\tENDP";
360 undef $current_function;
364 /\.align/ && do { $self->{value} = "ALIGN\t".$line; last; };
365 /\.(byte|value|long|quad)/
366 && do { my @arr = split(',',$line);
367 my $sz = substr($1,0,1);
368 my $last = pop(@arr);
370 $sz =~ tr/bvlq/BWDQ/;
371 $self->{value} = "\tD$sz\t";
372 for (@arr) { $self->{value} .= sprintf"0%Xh,",oct; }
373 $self->{value} .= sprintf"0%Xh",oct($last);
376 /\.picmeup/ && do { $self->{value} = sprintf"\tDD\t 0%Xh,090000000h",$opcode{$line};
395 $line =~ s|[#!].*$||; # get rid of asm-style comments...
396 $line =~ s|/\*.*\*/||; # ... and C-style comments...
397 $line =~ s|^\s+||; # ... and skip white spaces in beginning
405 if ($label=label->re(\$line)) { print $label->out(); }
407 if (directive->re(\$line)) {
408 printf "%s",directive->out();
409 } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: {
411 if ($src=register->re(\$line)) { opcode->size($src->size()); }
412 elsif ($src=const->re(\$line)) { }
413 elsif ($src=ea->re(\$line)) { }
414 elsif ($src=expr->re(\$line)) { }
416 last ARGUMENT if ($line !~ /^,/);
418 $line = substr($line,1); $line =~ s/^\s+//;
420 if ($dst=register->re(\$line)) { opcode->size($dst->size()); }
421 elsif ($dst=const->re(\$line)) { }
422 elsif ($dst=ea->re(\$line)) { }
430 printf "\t%s\t%s,%s", $opcode->out($dst->size()),
431 $src->out($sz),$dst->out($sz);
433 printf "\t%s\t%s,%s", $opcode->out(),
434 $dst->out($sz),$src->out($sz);
436 } elsif (defined($src)) {
437 printf "\t%s\t%s",$opcode->out(),$src->out($sz);
439 printf "\t%s",$opcode->out();
446 print "\n$current_segment\tENDS\nEND\n" if ($masm);
450 #################################################
451 # Cross-reference x86_64 ABI "card"
471 # (*) volatile register
472 # (-) preserved by callee
473 # (#) Nth argument, volatile
475 # In Unix terms top of stack is argument transfer area for arguments
476 # which could not be accomodated in registers. Or in other words 7th
477 # [integer] argument resides at 8(%rsp) upon function entry point.
478 # 128 bytes above %rsp constitute a "red zone" which is not touched
479 # by signal handlers and can be used as temporal storage without
480 # allocating a frame.
482 # In Win64 terms N*8 bytes on top of stack is argument transfer area,
483 # which belongs to/can be overwritten by callee. N is the number of
484 # arguments passed to callee, *but* not less than 4! This means that
485 # upon function entry point 5th argument resides at 40(%rsp), as well
486 # as that 32 bytes from 8(%rsp) can always be used as temporal
487 # storage [without allocating a frame].
489 # All the above means that if assembler programmer adheres to Unix
490 # register and stack layout, but disregards the "red zone" existense,
491 # it's possible to use following prologue and epilogue to "gear" from
492 # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
494 # omnipotent_function:
498 # movq %rcx,%rdi ; if 1st argument is actually present
499 # movq %rdx,%rsi ; if 2nd argument is actually ...
500 # movq %r8,%rdx ; if 3rd argument is ...
501 # movq %r9,%rcx ; if 4th argument ...
502 # movq 40(%rsp),%r8 ; if 5th ...
503 # movq 48(%rsp),%r9 ; if 6th ...