Skip to content

Commit

Permalink
use intel dialect for inline assembly
Browse files Browse the repository at this point in the history
closes #242
  • Loading branch information
andrewrk committed Mar 23, 2017
1 parent fd634f3 commit 5c04730
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
5 changes: 3 additions & 2 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1772,9 +1772,10 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
}
LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, input_and_output_count, false);

bool is_x86 = (g->zig_target.arch.arch == ZigLLVM_x86 || g->zig_target.arch.arch == ZigLLVM_x86_64);
bool is_volatile = asm_expr->is_volatile || (asm_expr->output_list.length == 0);
LLVMValueRef asm_fn = LLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),
buf_ptr(&constraint_buf), is_volatile, false);
LLVMValueRef asm_fn = ZigLLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),
buf_ptr(&constraint_buf), is_volatile, false, is_x86);

return LLVMBuildCall(g->builder, asm_fn, param_values, input_and_output_count, "");
}
Expand Down
35 changes: 24 additions & 11 deletions src/zig_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,23 @@
* 3. Prevent C++ from infecting the rest of the project.
*/

#include <llvm/Analysis/TargetLibraryInfo.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/IR/DiagnosticInfo.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/InlineAsm.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/InitializePasses.h>
#include <llvm/PassRegistry.h>
#include <llvm/MC/SubtargetFeature.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/PassRegistry.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/TargetParser.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetMachine.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/IR/DiagnosticInfo.h>
#include <llvm/Analysis/TargetLibraryInfo.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <llvm/Transforms/Scalar.h>
Expand Down Expand Up @@ -139,6 +140,18 @@ LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A
return wrap(unwrap(B)->Insert(call_inst));
}

LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86)
{
if (is_x86) {
return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Constraints, HasSideEffects, IsAlignStack, InlineAsm::AD_Intel));
} else {
return wrap(InlineAsm::get(dyn_cast<FunctionType>(unwrap(Ty)), AsmString,
Constraints, HasSideEffects, IsAlignStack));
}
}

void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
assert( isa<Function>(unwrap(fn)) );
Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));
Expand Down
3 changes: 3 additions & 0 deletions src/zig_llvm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ void ZigLLVMOptimizeModule(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef
LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
unsigned NumArgs, unsigned CC, const char *Name);

LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86);

LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp,
LLVMValueRef new_val, LLVMAtomicOrdering success_ordering,
LLVMAtomicOrdering failure_ordering);
Expand Down
8 changes: 4 additions & 4 deletions std/bootstrap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ export nakedcc fn _start() -> unreachable {

switch (@compileVar("arch")) {
Arch.x86_64 => {
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize));
argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8));
},
Arch.i386 => {
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize));
argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8));
},
else => @compileError("unsupported arch"),
}
Expand Down
14 changes: 7 additions & 7 deletions std/linux_i386.zig
Original file line number Diff line number Diff line change
Expand Up @@ -420,28 +420,28 @@ pub const F_GETOWN_EX = 16;
pub const F_GETOWNER_UIDS = 17;

pub inline fn syscall0(number: usize) -> usize {
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number))
}

pub inline fn syscall1(number: usize, arg1: usize) -> usize {
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1))
}

pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
[arg2] "{ecx}" (arg2))
}

pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) -> usize {
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
Expand All @@ -450,7 +450,7 @@ pub inline fn syscall3(number: usize, arg1: usize, arg2: usize, arg3: usize) ->
}

pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize {
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
Expand All @@ -462,7 +462,7 @@ pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg
pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
arg4: usize, arg5: usize) -> usize
{
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
Expand All @@ -475,7 +475,7 @@ pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,
arg4: usize, arg5: usize, arg6: usize) -> usize
{
asm volatile ("int $0x80"
asm volatile ("int 80h"
: [ret] "={eax}" (-> usize)
: [number] "{eax}" (number),
[arg1] "{ebx}" (arg1),
Expand Down

0 comments on commit 5c04730

Please sign in to comment.