From 673d638070452d86543c6bb47879e83adcfa73a1 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 15 Dec 2015 01:55:52 -0700 Subject: [PATCH] std: more efficient inline assembly --- src/codegen.cpp | 12 +++++------- std/std.zig | 26 ++++++++------------------ 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 110286a3c9..f60f116a2c 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -805,14 +805,12 @@ static LLVMValueRef gen_asm_expr(CodeGen *g, AstNode *node) { LLVMValueRef *param_values = allocate(input_and_output_count); for (int i = 0; i < asm_expr->output_list.length; i += 1, total_index += 1) { AsmOutput *asm_output = asm_expr->output_list.at(i); - bool is_return = false; - if (buf_eql_str(&asm_output->constraint, "=m")) { - buf_append_str(&constraint_buf, "=*m"); - } else if (buf_eql_str(&asm_output->constraint, "=r")) { - buf_append_str(&constraint_buf, "=r"); - is_return = true; + bool is_return = (asm_output->return_type != nullptr); + assert(*buf_ptr(&asm_output->constraint) == '='); + if (is_return) { + buf_appendf(&constraint_buf, "=%s", buf_ptr(&asm_output->constraint) + 1); } else { - zig_panic("TODO unable to handle anything other than '=m' and '=r' for outputs"); + buf_appendf(&constraint_buf, "=*%s", buf_ptr(&asm_output->constraint) + 1); } if (total_index + 1 < total_constraint_count) { buf_append_char(&constraint_buf, ','); diff --git a/std/std.zig b/std/std.zig index d3ce4f976c..b0aa266be6 100644 --- a/std/std.zig +++ b/std/std.zig @@ -3,27 +3,17 @@ const SYS_exit : isize = 60; const stdout_fileno : isize = 1; fn syscall1(number: isize, arg1: isize) -> isize { - asm volatile (" - mov %[number], %%rax - mov %[arg1], %%rdi - syscall - mov %%rax, %[ret]" - : [ret] "=r" (return isize) - : [number] "r" (number), [arg1] "r" (arg1) - : "rcx", "r11", "rax", "rdi") + asm volatile ("syscall" + : [ret] "={rax}" (return isize) + : [number] "{rax}" (number), [arg1] "{rdi}" (arg1) + : "rcx", "r11") } fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize { - asm volatile (" - mov %[number], %%rax - mov %[arg1], %%rdi - mov %[arg2], %%rsi - mov %[arg3], %%rdx - syscall - mov %%rax, %[ret]" - : [ret] "=r" (return isize) - : [number] "r" (number), [arg1] "r" (arg1), [arg2] "r" (arg2), [arg3] "r" (arg3) - : "rcx", "r11", "rax", "rdi", "rsi", "rdx") + asm volatile ("syscall" + : [ret] "={rax}" (return isize) + : [number] "{rax}" (number), [arg1] "{rdi}" (arg1), [arg2] "{rsi}" (arg2), [arg3] "{rdx}" (arg3) + : "rcx", "r11") } pub fn write(fd: isize, buf: &const u8, count: usize) -> isize {