Skip to content

Commit

Permalink
Support OS X
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kpamnany committed Jun 28, 2016
1 parent c0d401d commit 755e767
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/strsplit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 27 additions & 1 deletion examples/time_slice1.c
Original file line number Diff line number Diff line change
@@ -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 <sys/types.h>
#include <sys/_types/_timespec.h>
#include <mach/mach.h>
#include <mach/clock.h>
#define CLOCK_MONOTONIC SYSTEM_CLOCK
#elif !defined(__FreeBSD__)
#include <features.h>
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Expand All @@ -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;
};
Expand Down
9 changes: 8 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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
Expand Down
21 changes: 11 additions & 10 deletions src/arch/x86_64/concurrent_arch.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ else
endif

EXT=out
ifeq ($(shell uname -o),Cygwin)
ifeq ($(findstring Cygwin, $(shell uname -a)), Cygwin)
EXT=exe
endif

Expand Down

0 comments on commit 755e767

Please sign in to comment.