Skip to content

Commit 5c04730

Browse files
committed
use intel dialect for inline assembly
closes #242
1 parent fd634f3 commit 5c04730

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

src/codegen.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,9 +1772,10 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
17721772
}
17731773
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);
17751776
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-
buf_ptr(&constraint_buf), is_volatile, false);
1777+
LLVMValueRef asm_fn = ZigLLVMConstInlineAsm(function_type, buf_ptr(&llvm_template),
1778+
buf_ptr(&constraint_buf), is_volatile, false, is_x86);
17781779

17791780
return LLVMBuildCall(g->builder, asm_fn, param_values, input_and_output_count, "");
17801781
}

src/zig_llvm.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@
2222
* 3. Prevent C++ from infecting the rest of the project.
2323
*/
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>
2535
#include <llvm/InitializePasses.h>
26-
#include <llvm/PassRegistry.h>
2736
#include <llvm/MC/SubtargetFeature.h>
28-
#include <llvm/Support/raw_ostream.h>
37+
#include <llvm/PassRegistry.h>
2938
#include <llvm/Support/FileSystem.h>
3039
#include <llvm/Support/TargetParser.h>
40+
#include <llvm/Support/raw_ostream.h>
3141
#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>
4142
#include <llvm/Transforms/IPO.h>
4243
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
4344
#include <llvm/Transforms/Scalar.h>
@@ -139,6 +140,18 @@ LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *A
139140
return wrap(unwrap(B)->Insert(call_inst));
140141
}
141142

143+
LLVMValueRef 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+
142155
void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
143156
assert( isa<Function>(unwrap(fn)) );
144157
Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));

src/zig_llvm.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void ZigLLVMOptimizeModule(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef
3939
LLVMValueRef ZigLLVMBuildCall(LLVMBuilderRef B, LLVMValueRef Fn, LLVMValueRef *Args,
4040
unsigned NumArgs, unsigned CC, const char *Name);
4141

42+
LLVMValueRef ZigLLVMConstInlineAsm(LLVMTypeRef Ty, const char *AsmString,
43+
const char *Constraints, bool HasSideEffects, bool IsAlignStack, bool is_x86);
44+
4245
LLVMValueRef ZigLLVMBuildCmpXchg(LLVMBuilderRef builder, LLVMValueRef ptr, LLVMValueRef cmp,
4346
LLVMValueRef new_val, LLVMAtomicOrdering success_ordering,
4447
LLVMAtomicOrdering failure_ordering);

std/bootstrap.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ export nakedcc fn _start() -> unreachable {
2424

2525
switch (@compileVar("arch")) {
2626
Arch.x86_64 => {
27-
argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> usize));
28-
argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
27+
argc = asm("mov %[argc], [rsp]": [argc] "=r" (-> usize));
28+
argv = asm("lea %[argv], [rsp + 8h]": [argv] "=r" (-> &&u8));
2929
},
3030
Arch.i386 => {
31-
argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> usize));
32-
argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
31+
argc = asm("mov %[argc], [esp]": [argc] "=r" (-> usize));
32+
argv = asm("lea %[argv], [esp + 4h]": [argv] "=r" (-> &&u8));
3333
},
3434
else => @compileError("unsupported arch"),
3535
}

std/linux_i386.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,28 +420,28 @@ pub const F_GETOWN_EX = 16;
420420
pub const F_GETOWNER_UIDS = 17;
421421

422422
pub inline fn syscall0(number: usize) -> usize {
423-
asm volatile ("int $0x80"
423+
asm volatile ("int 80h"
424424
: [ret] "={eax}" (-> usize)
425425
: [number] "{eax}" (number))
426426
}
427427

428428
pub inline fn syscall1(number: usize, arg1: usize) -> usize {
429-
asm volatile ("int $0x80"
429+
asm volatile ("int 80h"
430430
: [ret] "={eax}" (-> usize)
431431
: [number] "{eax}" (number),
432432
[arg1] "{ebx}" (arg1))
433433
}
434434

435435
pub inline fn syscall2(number: usize, arg1: usize, arg2: usize) -> usize {
436-
asm volatile ("int $0x80"
436+
asm volatile ("int 80h"
437437
: [ret] "={eax}" (-> usize)
438438
: [number] "{eax}" (number),
439439
[arg1] "{ebx}" (arg1),
440440
[arg2] "{ecx}" (arg2))
441441
}
442442

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

452452
pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg4: usize) -> usize {
453-
asm volatile ("int $0x80"
453+
asm volatile ("int 80h"
454454
: [ret] "={eax}" (-> usize)
455455
: [number] "{eax}" (number),
456456
[arg1] "{ebx}" (arg1),
@@ -462,7 +462,7 @@ pub inline fn syscall4(number: usize, arg1: usize, arg2: usize, arg3: usize, arg
462462
pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
463463
arg4: usize, arg5: usize) -> usize
464464
{
465-
asm volatile ("int $0x80"
465+
asm volatile ("int 80h"
466466
: [ret] "={eax}" (-> usize)
467467
: [number] "{eax}" (number),
468468
[arg1] "{ebx}" (arg1),
@@ -475,7 +475,7 @@ pub inline fn syscall5(number: usize, arg1: usize, arg2: usize, arg3: usize,
475475
pub inline fn syscall6(number: usize, arg1: usize, arg2: usize, arg3: usize,
476476
arg4: usize, arg5: usize, arg6: usize) -> usize
477477
{
478-
asm volatile ("int $0x80"
478+
asm volatile ("int 80h"
479479
: [ret] "={eax}" (-> usize)
480480
: [number] "{eax}" (number),
481481
[arg1] "{ebx}" (arg1),

0 commit comments

Comments
 (0)