authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-23 18:59:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-03-23 18:59:43-04:00
log5c04730534ea7933855429c5fc5dc7b22eba7bc2
tree6270702f9aefc48a6a1eabfa61c3e8209d020569
parentfd634f3db35791ea904e82a525e4f49f4c5b67a8

use intel dialect for inline assembly

closes #242

5 files changed, 41 insertions(+), 24 deletions(-)

src/codegen.cpp+3-2
...@@ -1772,9 +1772,10 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru...@@ -1772,9 +1772,10 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
1772 }1772 }
1773 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, input_and_output_count, false);1773 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, input_and_output_count, false);
17741774
1775 bool is_x86 = (g->zig_target.arch.arch == ZigLLVM_x86 || g->zig_target.arch.arch == ZigLLVM_x86_64);
1775 bool is_volatile = asm_expr->is_volatile || (asm_expr->output_list.length == 0);1776 bool is_volatile = asm_expr->is_volatile || (asm_expr->output_list.length == 0);
1776 LLVMValueRef asm_fn = LLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),1777 LLVMValueRef asm_fn = ZigLLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),
1777 buf_ptr(&constraint_buf), is_volatile, false);1778 buf_ptr(&constraint_buf), is_volatile, false, is_x86);
17781779
1779 return LLVMBuildCall(g->builder, asm_fn, param_values, input_and_output_count, "");1780 return LLVMBuildCall(g->builder, asm_fn, param_values, input_and_output_count, "");
1780}1781}
src/zig_llvm.cpp+24-11
...@@ -22,22 +22,23 @@...@@ -22,22 +22,23 @@
22 * 3. Prevent C++ from infecting the rest of the project.22 * 3. Prevent C++ from infecting the rest of the project.
23 */23 */
2424
25#include <llvm/Analysis/TargetLibraryInfo.h>
26#include <llvm/Analysis/TargetTransformInfo.h>
27#include <llvm/IR/DIBuilder.h>
28#include <llvm/IR/DiagnosticInfo.h>
29#include <llvm/IR/IRBuilder.h>
30#include <llvm/IR/InlineAsm.h>
31#include <llvm/IR/Instructions.h>
32#include <llvm/IR/LegacyPassManager.h>
33#include <llvm/IR/Module.h>
34#include <llvm/IR/Verifier.h>
25#include <llvm/InitializePasses.h>35#include <llvm/InitializePasses.h>
26#include <llvm/PassRegistry.h>
27#include <llvm/MC/SubtargetFeature.h>36#include <llvm/MC/SubtargetFeature.h>
28#include <llvm/Support/raw_ostream.h>37#include <llvm/PassRegistry.h>
29#include <llvm/Support/FileSystem.h>38#include <llvm/Support/FileSystem.h>
30#include <llvm/Support/TargetParser.h>39#include <llvm/Support/TargetParser.h>
40#include <llvm/Support/raw_ostream.h>
31#include <llvm/Target/TargetMachine.h>41#include <llvm/Target/TargetMachine.h>
32#include <llvm/IR/LegacyPassManager.h>
33#include <llvm/IR/Module.h>
34#include <llvm/IR/Verifier.h>
35#include <llvm/IR/Instructions.h>
36#include <llvm/IR/IRBuilder.h>
37#include <llvm/IR/DIBuilder.h>
38#include <llvm/IR/DiagnosticInfo.h>
39#include <llvm/Analysis/TargetLibraryInfo.h>
40#include <llvm/Analysis/TargetTransformInfo.h>
41#include <llvm/Transforms/IPO.h>42#include <llvm/Transforms/IPO.h>
42#include <llvm/Transforms/IPO/PassManagerBuilder.h>43#include <llvm/Transforms/IPO/PassManagerBuilder.h>
43#include <llvm/Transforms/Scalar.h>44#include <llvm/Transforms/Scalar.h>
...@@ -139,6 +140,18 @@ LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A...@@ -139,6 +140,18 @@ LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A
139 return wrap(unwrap(B)->Insert(call_inst));140 return wrap(unwrap(B)->Insert(call_inst));
140}141}
141142
143LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
144 const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86)
145{
146 if (is_x86) {
147 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
148 Constraints, HasSideEffects, IsAlignStack, InlineAsm::AD_Intel));
149 } else {
150 return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
151 Constraints, HasSideEffects, IsAlignStack));
152 }
153}
154
142void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {155void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
143 assert( isa<Function>(unwrap(fn)) );156 assert( isa<Function>(unwrap(fn)) );
144 Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));157 Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));
src/zig_llvm.hpp+3
...@@ -39,6 +39,9 @@ void ZigLLVMOptimizeModule(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef...@@ -39,6 +39,9 @@ void ZigLLVMOptimizeModule(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef
39LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,39LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
40 unsigned NumArgs, unsigned CC, const char *Name);40 unsigned NumArgs, unsigned CC, const char *Name);
4141
42LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
43 const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86);
44
42LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp,45LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp,
43 LLVMValueRef new_val, LLVMAtomicOrdering success_ordering,46 LLVMValueRef new_val, LLVMAtomicOrdering success_ordering,
44 LLVMAtomicOrdering failure_ordering);47 LLVMAtomicOrdering failure_ordering);
std/bootstrap.zig+4-4
...@@ -24,12 +24,12 @@ export nakedcc fn _start() -> unreachable {...@@ -24,12 +24,12 @@ export nakedcc fn _start() -> unreachable {
2424
25 switch (@compileVar("arch")) {25 switch (@compileVar("arch")) {
26 Arch.x86_64 => {26 Arch.x86_64 => {
27 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));27 argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize));
28 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));28 argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8));
29 },29 },
30 Arch.i386 => {30 Arch.i386 => {
31 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));31 argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize));
32 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));32 argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8));
33 },33 },
34 else => @compileError("unsupported arch"),34 else => @compileError("unsupported arch"),
35 }35 }
std/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 $0x80"423 asm volatile ("int 80h"
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 $0x80"429 asm volatile ("int 80h"
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 $0x80"436 asm volatile ("int 80h"
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 $0x80"444 asm volatile ("int 80h"
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 $0x80"453 asm volatile ("int 80h"
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 $0x80"465 asm volatile ("int 80h"
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 $0x80"478 asm volatile ("int 80h"
479 : [ret] "={eax}" (-> usize)479 : [ret] "={eax}" (-> usize)
480 : [number] "{eax}" (number),480 : [number] "{eax}" (number),
481 [arg1] "{ebx}" (arg1),481 [arg1] "{ebx}" (arg1),