authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-30 11:28:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-30 11:28:11-04:00
log29defd705dcaf25d4a080f7db8f76e8787fca146
tree2524f7c0433dfb8bfcac7f2379976d67c5cac1ad
parentcbfe4b4bae61682e367a477283e4d64a518203a8

back to AT&T syntax for assembly

this reverts 5c04730534ea7933855429c5fc5dc7b22eba7bc2. sadly the quality of the intel dialect in llvm's assembly parser has many frustrating bugs, and generally has unfortunate syntax. the plan is to use AT&T for now since it at least works, and eventually zig will have its own assembly parser for x86 and it will be as close to NASM as possible.

6 files changed, 17 insertions(+), 37 deletions(-)

src/codegen.cpp+2-6
...@@ -2088,10 +2088,9 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -2088,10 +2088,9 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
2088 }2088 }
2089 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);2089 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, (unsigned)input_and_output_count, false);
20902090
2091 bool is_x86 = (g->zig_target.arch.arch == ZigLLVM_x86 || g->zig_target.arch.arch == ZigLLVM_x86_64);
2092 bool is_volatile = asm_expr->is_volatile || (asm_expr->output_list.length == 0);2091 bool is_volatile = asm_expr->is_volatile || (asm_expr->output_list.length == 0);
2093 LLVMValueRef asm_fn = ZigLLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),2092 LLVMValueRef asm_fn = LLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),
2094 buf_ptr(&constraint_buf), is_volatile, false, is_x86);2093 buf_ptr(&constraint_buf), is_volatile, false);
20952094
2096 return LLVMBuildCall(g->builder, asm_fn, param_values, (unsigned)input_and_output_count, "");2095 return LLVMBuildCall(g->builder, asm_fn, param_values, (unsigned)input_and_output_count, "");
2097}2096}
...@@ -4805,9 +4804,6 @@ static void gen_global_asm(CodeGen *g) {...@@ -4805,9 +4804,6 @@ static void gen_global_asm(CodeGen *g) {
4805 if ((err = os_fetch_file_path(asm_file, &contents))) {4804 if ((err = os_fetch_file_path(asm_file, &contents))) {
4806 zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));4805 zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
4807 }4806 }
4808 if (g->zig_target.arch.arch == ZigLLVM_x86 || g->zig_target.arch.arch == ZigLLVM_x86_64) {
4809 buf_append_str(&g->global_asm, ".intel_syntax noprefix\n");
4810 }
4811 buf_append_buf(&g->global_asm, &contents);4807 buf_append_buf(&g->global_asm, &contents);
4812 }4808 }
4813}4809}
src/zig_llvm.cpp-12
...@@ -186,18 +186,6 @@ LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A...@@ -186,18 +186,6 @@ LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A
186 return wrap(unwrap(B)->Insert(call_inst));186 return wrap(unwrap(B)->Insert(call_inst));
187}187}
188188
189LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
190 const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86)
191{
192 if (is_x86) {
193 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
194 Constraints, HasSideEffects, IsAlignStack, InlineAsm::AD_Intel));
195 } else {
196 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
197 Constraints, HasSideEffects, IsAlignStack));
198 }
199}
200
201void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {189void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
202 assert( isa<Function>(unwrap(fn)) );190 assert( isa<Function>(unwrap(fn)) );
203 Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));191 Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));
src/zig_llvm.hpp-3
...@@ -40,9 +40,6 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM...@@ -40,9 +40,6 @@ bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMM
40LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,40LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
41 unsigned NumArgs, unsigned CC, bool always_inline, const char *Name);41 unsigned NumArgs, unsigned CC, bool always_inline, const char *Name);
4242
43LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
44 const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86);
45
46LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp,43LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp,
47 LLVMValueRef new_val, LLVMAtomicOrdering success_ordering,44 LLVMValueRef new_val, LLVMAtomicOrdering success_ordering,
48 LLVMAtomicOrdering failure_ordering);45 LLVMAtomicOrdering failure_ordering);
std/os/linux_i386.zig+7-7
...@@ -420,20 +420,20 @@ pub const F_GETOWN_EX = 16;...@@ -420,20 +420,20 @@ pub const F_GETOWN_EX = 16;
420pub const F_GETOWNER_UIDS = 17;420pub const F_GETOWNER_UIDS = 17;
421421
422pub inline fn syscall0(number: usize) -> usize {422pub inline fn syscall0(number: usize) -> usize {
423 asm volatile ("int 80h"423 asm volatile ("int $0x80"
424 : [ret] "={eax}" (-> usize)424 : [ret] "={eax}" (-> usize)
425 : [number] "{eax}" (number))425 : [number] "{eax}" (number))
426}426}
427427
428pub inline fn syscall1(number: usize, arg1: usize) -> usize {428pub inline fn syscall1(number: usize, arg1: usize) -> usize {
429 asm volatile ("int 80h"429 asm volatile ("int $0x80"
430 : [ret] "={eax}" (-> usize)430 : [ret] "={eax}" (-> usize)
431 : [number] "{eax}" (number),431 : [number] "{eax}" (number),
432 [arg1] "{ebx}" (arg1))432 [arg1] "{ebx}" (arg1))
433}433}
434434
435pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {435pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {
436 asm volatile ("int 80h"436 asm volatile ("int $0x80"
437 : [ret] "={eax}" (-> usize)437 : [ret] "={eax}" (-> usize)
438 : [number] "{eax}" (number),438 : [number] "{eax}" (number),
439 [arg1] "{ebx}" (arg1),439 [arg1] "{ebx}" (arg1),
...@@ -441,7 +441,7 @@ pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {...@@ -441,7 +441,7 @@ pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {
441}441}
442442
443pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {443pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
444 asm volatile ("int 80h"444 asm volatile ("int $0x80"
445 : [ret] "={eax}" (-> usize)445 : [ret] "={eax}" (-> usize)
446 : [number] "{eax}" (number),446 : [number] "{eax}" (number),
447 [arg1] "{ebx}" (arg1),447 [arg1] "{ebx}" (arg1),
...@@ -450,7 +450,7 @@ pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) ->...@@ -450,7 +450,7 @@ pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) ->
450}450}
451451
452pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize {452pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize {
453 asm volatile ("int 80h"453 asm volatile ("int $0x80"
454 : [ret] "={eax}" (-> usize)454 : [ret] "={eax}" (-> usize)
455 : [number] "{eax}" (number),455 : [number] "{eax}" (number),
456 [arg1] "{ebx}" (arg1),456 [arg1] "{ebx}" (arg1),
...@@ -462,7 +462,7 @@ pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg...@@ -462,7 +462,7 @@ pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg
462pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,462pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
463 arg4: usize, arg5: usize) -> usize463 arg4: usize, arg5: usize) -> usize
464{464{
465 asm volatile ("int 80h"465 asm volatile ("int $0x80"
466 : [ret] "={eax}" (-> usize)466 : [ret] "={eax}" (-> usize)
467 : [number] "{eax}" (number),467 : [number] "{eax}" (number),
468 [arg1] "{ebx}" (arg1),468 [arg1] "{ebx}" (arg1),
...@@ -475,7 +475,7 @@ pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,...@@ -475,7 +475,7 @@ pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
475pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,475pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,
476 arg4: usize, arg5: usize, arg6: usize) -> usize476 arg4: usize, arg5: usize, arg6: usize) -> usize
477{477{
478 asm volatile ("int 80h"478 asm volatile ("int $0x80"
479 : [ret] "={eax}" (-> usize)479 : [ret] "={eax}" (-> usize)
480 : [number] "{eax}" (number),480 : [number] "{eax}" (number),
481 [arg1] "{ebx}" (arg1),481 [arg1] "{ebx}" (arg1),
std/special/bootstrap.zig+2-2
...@@ -19,10 +19,10 @@ export nakedcc fn _start() -> noreturn {...@@ -19,10 +19,10 @@ export nakedcc fn _start() -> noreturn {
1919
20 switch (@compileVar("arch")) {20 switch (@compileVar("arch")) {
21 Arch.x86_64 => {21 Arch.x86_64 => {
22 argc_ptr = asm("lea %[argc], [rsp]": [argc] "=r" (-> &usize));22 argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));
23 },23 },
24 Arch.i386 => {24 Arch.i386 => {
25 argc_ptr = asm("lea %[argc], [esp]": [argc] "=r" (-> &usize));25 argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));
26 },26 },
27 else => @compileError("unsupported arch"),27 else => @compileError("unsupported arch"),
28 }28 }
test/assemble_and_link.zig+6-7
...@@ -7,18 +7,17 @@ pub fn addCases(cases: &tests.CompareOutputContext) {...@@ -7,18 +7,17 @@ pub fn addCases(cases: &tests.CompareOutputContext) {
7 \\.globl _start7 \\.globl _start
8 \\8 \\
9 \\_start:9 \\_start:
10 \\ mov rax, 110 \\ mov $1, %rax
11 \\ mov rdi, 111 \\ mov $1, %rdi
12 \\ lea rsi, msg12 \\ mov $msg, %rsi
13 \\ mov rdx, 1413 \\ mov $14, %rdx
14 \\ syscall14 \\ syscall
15 \\15 \\
16 \\ mov rax, 6016 \\ mov $60, %rax
17 \\ mov rdi, 017 \\ mov $0, %rdi
18 \\ syscall18 \\ syscall
19 \\19 \\
20 \\.data20 \\.data
21 \\
22 \\msg:21 \\msg:
23 \\ .ascii "Hello, world!\n"22 \\ .ascii "Hello, world!\n"
24 , "Hello, world!\n");23 , "Hello, world!\n");