llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.42k stars 11.74k forks source link

[PowerPC] PPCAsmParser request: support darwin asm syntax in PPCAsmParser #17075

Closed llvmbot closed 10 years ago

llvmbot commented 11 years ago
Bugzilla Link 16701
Resolution FIXED
Resolved on Dec 23, 2013 21:39
Version trunk
OS MacOS X
Reporter LLVM Bugzilla Contributor
CC @hfinkel,@iains

Extended Description

This is a feature request for supporting darwin asm syntax for PPC assembly.
The most noticeable difference is in the naming of register operands.

% cat hello-puts.c extern int puts(const char); int main(int argc, char argv[]) { puts("Hello, world!\n"); return 0; }

% make hello-puts.s ~/local/src/LLVM-svn/gcc40-cmake-build/bin/clang -v -fno-common -DPIC -femit-all-decls -c hello-puts.c -S -o hello-puts.s clang version 3.4 Target: powerpc-apple-darwin8.11.0 Thread model: posix "/Volumes/Isolde/builds/LLVM/gcc40-cmake-build/bin/clang-3.4" -cc1 -triple powerpc-apple-macosx10.4.0 -S -disable-free -main-file-name hello-puts.c -mrelocation-model pic -pic-level 2 -mdisable-fp-elim -masm-verbose -v -coverage-file /Users/fang/temp/clang/hello-puts.s -resource-dir /Volumes/Isolde/builds/LLVM/gcc40-cmake-build/bin/../lib/clang/3.4 -D PIC -fdebug-compilation-dir /Users/fang/temp/clang -ferror-limit 19 -fmessage-length 80 -femit-all-decls -mstackrealign -fblocks -fblocks-runtime-optional -fobjc-runtime=macosx-10.4.0 -fobjc-default-synthesize-properties -fencode-extended-block-signature -fno-common -fdiagnostics-show-option -fcolor-diagnostics -vectorize-loops -o hello-puts.s -x c hello-puts.c clang -cc1 version 3.4 based upon LLVM 3.4git-969f343 default target powerpc-apple-darwin8.11.0 ignoring nonexistent directory "/usr/local/include"

include "..." search starts here:

include <...> search starts here:

/Volumes/Isolde/builds/LLVM/gcc40-cmake-build/bin/../lib/clang/3.4/include /usr/include /System/Library/Frameworks (framework directory) /Library/Frameworks (framework directory) End of search list.

% cat hello-puts.s .machine ppc7400 .section TEXT,textcoal_nt,coalesced,pure_instructions .section TEXT,picsymbolstub1,symbol_stubs,pure_instructions,32 .section TEXT,text,regular,pure_instructions .globl _main .align 4 main: ; @​main ; BB#0: ; %entry mflr r0 stw r31, -4(r1) stw r0, 8(r1) stwu r1, -96(r1) mr r31, r1 bl L0$pb L0$pb: mflr r2 li r5, 0 stw r5, 88(r31) stw r3, 84(r31) addis r2, r2, ha16(L.str-L0$pb) stw r4, 80(r31) la r2, lo16(L_.str-L0$pb)(r2) stw r3, 76(r31) ; 4-byte Folded Spill mr r3, r2 stw r4, 72(r31) ; 4-byte Folded Spill stw r5, 68(r31) ; 4-byte Folded Spill bl L_puts$stub lwz r2, 72(r31) ; 4-byte Folded Reload lwz r4, 76(r31) ; 4-byte Folded Reload lwz r5, 68(r31) ; 4-byte Folded Reload stw r3, 64(r31) ; 4-byte Folded Spill mr r3, r5 stw r2, 60(r31) ; 4-byte Folded Spill stw r4, 56(r31) ; 4-byte Folded Spill addi r1, r1, 96 lwz r0, 8(r1) lwz r31, -4(r1) mtlr r0 blr

    .section        __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
    .align  4

L_puts$stub: .indirect_symbol _puts mflr r0 bcl 20, 31, L_puts$stub$tmp L_puts$stub$tmp: mflr r11 addis r11, r11, ha16(L_puts$lazy_ptr-L_puts$stub$tmp) mtlr r0 lwzu r12, lo16(L_puts$lazy_ptr-L_puts$stub$tmp)(r11) mtctr r12 bctr .section DATA,la_symbol_ptr,lazy_symbol_pointers L_puts$lazy_ptr: .indirect_symbol _puts .long dyld_stub_binding_helper

.subsections_via_symbols .section TEXT,cstring,cstringliterals L.str: ; @.str .asciz "Hello, world!\n"

and attempting to use llvm-mc: % /Volumes/Isolde/builds/LLVM/gcc40-cmake-build/bin/llvm-mc -triple=powerpc-apple-darwin8 hello-puts.s .section TEXT,text,regular,pure_instructions hello-puts.s:1:2: error: unrecognized machine type .machine ppc7400 ^ hello-puts.s:1:2: error: unknown directive .machine ppc7400 ^ .section TEXT,textcoal_nt,coalesced,pure_instructions .section TEXT,picsymbolstub1,symbol_stubs,pure_instructions,32 .section TEXT,text,regular,pure_instructions .globl _main .align 4 _main:

hello-puts.s:9:7: error: invalid operand for instruction mflr r0 ^

hello-puts.s:10:6: error: invalid operand for instruction stw r31, -4(r1) ^

I've already taken a stab at accepting rX on memory operands with the following patch: <> diff --git a/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp index 3c677cc..e49cf08 100644 --- a/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp +++ b/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp @@ -26,6 +26,8 @@

include "llvm/Support/TargetRegistry.h"

include "llvm/Support/raw_ostream.h"

+#define ALLOW_DARWIN_ASM_SYNTAX 1 + using namespace llvm;

namespace { @@ -1158,7 +1160,14 @@ ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { IntVal < 0 || IntVal > 31) return Error(S, "invalid register number"); break;

+#if ALLOW_DARWIN_ASM_SYNTAX

but I wouldn't know how to proceed from here. How difficult would this project be?

llvmbot commented 10 years ago

Contributed by Iain Sandoe, r197324. Many thanks again!

llvmbot commented 11 years ago

using this sed-script (GNU sed):

% cat darwin-to-linux-asm.sed

!/sw/bin/sed -f

s|\<r([0-9]+)>|\1|g s|\<ha16>(([^(]))|\1@ha|g s|\<lo16>(([^(]))|\1@l|g

s|\<L[A-Za-z0-9.$_]+>|.&|g

I took the asm with darwin syntax to produce the currently supported syntax:

% cat hello-puts-linux.ll-s ; .machine ppc7400 .section TEXT,textcoal_nt,coalesced,pure_instructions .section TEXT,picsymbolstub1,symbol_stubs,pure_instructions,32 .section TEXT,text,regular,pure_instructions .globl _main .align 4 main: ; @​main ; BB#0: ; %entry mflr 0 stw 31, -4(1) stw 0, 8(1) stwu 1, -80(1) bl L0$pb L0$pb: mr 31, 1 li 5, 0 mflr 2 stw 3, 68(31) stw 5, 72(31) stw 4, 64(31) addis 2, 2, (L.str-L0$pb)@ha la 3, (L_.str-L0$pb)@l(2) bl L_puts$stub li 3, 0 addi 1, 1, 80 lwz 0, 8(1) lwz 31, -4(1) mtlr 0 blr

    .section        __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
    .align  4

L_puts$stub: .indirect_symbol _puts mflr 0 bcl 20, 31, L_puts$stub$tmp L_puts$stub$tmp: mflr 11 addis 11, 11, (L_puts$lazy_ptr-L_puts$stub$tmp)@ha mtlr 0 lwzu 12, (L_puts$lazy_ptr-L_puts$stub$tmp)@l(11) mtctr 12 bctr .section DATA,la_symbol_ptr,lazy_symbol_pointers L_puts$lazy_ptr: .indirect_symbol _puts .long dyld_stub_binding_helper

.subsections_via_symbols .section TEXT,cstring,cstringliterals L.str: ; @.str .asciz "Hello, world!"

% diff hello-puts.ll-o hello-puts-linux.ll-mc-o [MATCH!]

llvmbot commented 11 years ago

I happened to notice that the llc/llvm-mc tools have the following option:

-x86-asm-syntax - Choose style of code to emit from X86 backend: =att - Emit AT&T-style assembly =intel - Emit Intel-style assembly

Maybe we could add something similar for -ppc-asm-syntax?