From 755e767642cfd2b9b382693103a4a34e9f25ce93 Mon Sep 17 00:00:00 2001 From: kpamnany Date: Mon, 27 Jun 2016 21:29:20 -0700 Subject: [PATCH] Support OS X Use relative addressing in the assembly code. Remove warning in strsplit example. Add clock_gettime() substitute for time_slice1 example. Don't use uname -o for Darwin in the makefiles. Tested on OS X 10.11.5/Core i7-4770HQ (x86_64) and for regressions on CentOS 7.1/Xeon E5-2699 v4 (x86_64). --- examples/strsplit.c | 2 +- examples/time_slice1.c | 28 +++++++++++++++++++++++++++- makefile | 9 ++++++++- src/arch/x86_64/concurrent_arch.asm | 21 +++++++++++---------- test/makefile | 2 +- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/examples/strsplit.c b/examples/strsplit.c index fd80bcc..3177661 100644 --- a/examples/strsplit.c +++ b/examples/strsplit.c @@ -70,7 +70,7 @@ int main(void) struct strsplit *s = strsplit_new(text, '|'); struct split_position *p; while ((p = strsplit_next(s)) != NULL) { - printf("%.*s\n", p->len, p->s); + printf("%.*s\n", (int)p->len, p->s); } strsplit_del(s); diff --git a/examples/time_slice1.c b/examples/time_slice1.c index dcecd96..872356a 100644 --- a/examples/time_slice1.c +++ b/examples/time_slice1.c @@ -1,9 +1,16 @@ /* -*- Mode: c; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- */ #define _GNU_SOURCE -#if !defined(__FreeBSD__) +#if defined(__MACH__) +#include +#include +#include +#include +#define CLOCK_MONOTONIC SYSTEM_CLOCK +#elif !defined(__FreeBSD__) #include #endif +#include #include #include #include @@ -15,6 +22,25 @@ #define TIME_SLICE_SEC (0.1) +#if defined(__MACH__) +int clock_gettime(int clk_id, struct timespec *tp) +{ + assert(clk_id == CLOCK_MONOTONIC); + kern_return_t retval = KERN_SUCCESS; + clock_serv_t cclock; + mach_timespec_t mts; + + host_get_clock_service(mach_host_self(), clk_id, &cclock); + retval = clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + + tp->tv_sec = mts.tv_sec; + tp->tv_nsec = mts.tv_nsec; + + return retval; +} +#endif + struct task_info { const char *out; }; diff --git a/makefile b/makefile index e90cf28..818e0a2 100644 --- a/makefile +++ b/makefile @@ -21,7 +21,12 @@ ifeq ($(ARCH),) endif ifeq ($(SYSTEM),) - SYSTEM=$(shell $(UNAME) -o) + SYSNAME=$(shell $(UNAME) -a) + ifeq ($(findstring Darwin, $(SYSNAME)), Darwin) + SYSTEM=Darwin + else + SYSTEM=$(shell $(UNAME) -o) + endif endif NASM_FLAGS= @@ -32,6 +37,8 @@ ifeq ($(findstring true, $(eq $(SYSTEM),Cygwin), $(eq $(SYSTEM),MSys)), true) endif else ifeq ($(SYSTEM),GNU/Linux) NASM_FLAGS+=-f elf$(ARCH_BITS) +else ifeq ($(SYSTEM),Darwin) + NASM_FLAGS+=-f macho$(ARCH_BITS) --prefix _ else NASM_FLAGS+=-f elf$(ARCH_BITS) endif diff --git a/src/arch/x86_64/concurrent_arch.asm b/src/arch/x86_64/concurrent_arch.asm index b9e0bab..d16a4ea 100644 --- a/src/arch/x86_64/concurrent_arch.asm +++ b/src/arch/x86_64/concurrent_arch.asm @@ -47,14 +47,15 @@ concurrent_arch_setup_execution_context: mov rcx, rdi ; exchange stack - mov rax, qword [concurrent_offsetof_stack_ptr] + mov rax, [rel concurrent_offsetof_stack_ptr] xchg rsp, qword [rcx + rax] push rcx ; ctx for "return" or out of scope push rcx ; ctx for yield - push concurrent_arch_return_at_procedure + lea rax, [rel concurrent_arch_return_at_procedure] + push rax - mov rax, qword [concurrent_offsetof_procedure] + mov rax, [rel concurrent_offsetof_procedure] push qword [rcx + rax] ; entry point ; initial register value @@ -69,7 +70,7 @@ concurrent_arch_setup_execution_context: push rax ; r15 ; restore stack - mov rax, qword [concurrent_offsetof_stack_ptr] + mov rax, [rel concurrent_offsetof_stack_ptr] xchg rsp, qword [rcx + rax] ret @@ -85,11 +86,11 @@ concurrent_arch_trampoline_to_procedure: ; save return address mov rdx, qword [rsp + 8 * 8] ; return address of this function - mov rax, qword [concurrent_offsetof_caller_return_addr] + mov rax, [rel concurrent_offsetof_caller_return_addr] mov qword [rcx + rax], rdx ; exchange stack - mov rax, qword [concurrent_offsetof_stack_ptr] + mov rax, [rel concurrent_offsetof_stack_ptr] xchg rsp, qword [rcx + rax] nop @@ -107,12 +108,12 @@ concurrent_arch_trampoline_to_caller: save_context ; exchange stack - mov rax, qword [concurrent_offsetof_stack_ptr] + mov rax, [rel concurrent_offsetof_stack_ptr] xchg qword [rcx + rax], rsp nop ; get return address - mov rax, qword [concurrent_offsetof_caller_return_addr] + mov rax, [rel concurrent_offsetof_caller_return_addr] mov rax, qword [rcx + rax] restore_context @@ -130,12 +131,12 @@ concurrent_arch_return_at_procedure: save_context ; exchange stack - mov rax, qword [concurrent_offsetof_stack_ptr] + mov rax, [rel concurrent_offsetof_stack_ptr] xchg qword [rcx + rax], rsp nop ; get return address - mov rax, qword [concurrent_offsetof_caller_return_addr] + mov rax, [rel concurrent_offsetof_caller_return_addr] mov rax, qword [rcx + rax] restore_context diff --git a/test/makefile b/test/makefile index 14c3d40..4839a88 100644 --- a/test/makefile +++ b/test/makefile @@ -11,7 +11,7 @@ else endif EXT=out -ifeq ($(shell uname -o),Cygwin) +ifeq ($(findstring Cygwin, $(shell uname -a)), Cygwin) EXT=exe endif