#include "zasm_GOOS_GOARCH.h"
#include "../../cmd/ld/textflag.h"
#include "syscall_nacl.h"
#define NACL_SYSCALL(code) \
MOVL $(0x10000 + ((code)<<5)), AX; CALL AX
#define NACL_SYSJMP(code) \
MOVL $(0x10000 + ((code)<<5)), AX; JMP AX
TEXT runtime·exit(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_exit)
TEXT runtime·exit1(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_thread_exit)
TEXT runtime·open(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_open)
TEXT runtime·close(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_close)
TEXT runtime·read(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_read)
TEXT syscall·naclWrite(SB), NOSPLIT, $12-16
MOVL arg1+0(FP), DI
MOVL arg2+4(FP), SI
MOVL arg3+8(FP), DX
MOVL DI, 0(SP)
MOVL SI, 4(SP)
MOVL DX, 8(SP)
CALL runtime·write(SB)
MOVL AX, ret+16(FP)
RET
TEXT runtime·write(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_write)
TEXT runtime·nacl_exception_stack(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_exception_stack)
TEXT runtime·nacl_exception_handler(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_exception_handler)
TEXT runtime·nacl_sem_create(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_sem_create)
TEXT runtime·nacl_sem_wait(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_sem_wait)
TEXT runtime·nacl_sem_post(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_sem_post)
TEXT runtime·nacl_mutex_create(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_mutex_create)
TEXT runtime·nacl_mutex_lock(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_mutex_lock)
TEXT runtime·nacl_mutex_trylock(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_mutex_trylock)
TEXT runtime·nacl_mutex_unlock(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_mutex_unlock)
TEXT runtime·nacl_cond_create(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_cond_create)
TEXT runtime·nacl_cond_wait(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_cond_wait)
TEXT runtime·nacl_cond_signal(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_cond_signal)
TEXT runtime·nacl_cond_broadcast(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_cond_broadcast)
TEXT runtime·nacl_cond_timed_wait_abs(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_cond_timed_wait_abs)
TEXT runtime·nacl_thread_create(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_thread_create)
TEXT runtime·mstart_nacl(SB),NOSPLIT,$0
JMP runtime·mstart(SB)
TEXT runtime·nacl_nanosleep(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_nanosleep)
TEXT runtime·osyield(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_sched_yield)
TEXT runtime·mmap(SB),NOSPLIT,$32
MOVL arg1+0(FP), AX
MOVL AX, 0(SP)
MOVL arg2+4(FP), AX
MOVL AX, 4(SP)
MOVL arg3+8(FP), AX
MOVL AX, 8(SP)
MOVL arg4+12(FP), AX
MOVL AX, 12(SP)
MOVL arg5+16(FP), AX
MOVL AX, 16(SP)
MOVL arg6+20(FP), AX
MOVL AX, 24(SP)
MOVL $0, 28(SP)
LEAL 24(SP), AX
MOVL AX, 20(SP)
NACL_SYSCALL(SYS_mmap)
RET
TEXT time·now(SB),NOSPLIT,$20
MOVL $0, 0(SP)
LEAL 8(SP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_clock_gettime)
MOVL 8(SP), AX
MOVL 12(SP), CX
MOVL 16(SP), BX
MOVL AX, sec+0(FP)
MOVL CX, sec+4(FP)
MOVL BX, nsec+8(FP)
RET
TEXT syscall·now(SB),NOSPLIT,$0
JMP time·now(SB)
TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
NACL_SYSJMP(SYS_clock_gettime)
TEXT runtime·nanotime(SB),NOSPLIT,$20
MOVL $0, 0(SP)
LEAL 8(SP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_clock_gettime)
MOVL 8(SP), AX
MOVL 16(SP), BX
MOVL $1000000000, CX
MULL CX
ADDL BX, AX
ADCL $0, DX
MOVL ret+0(FP), DI
MOVL AX, 0(DI)
MOVL DX, 4(DI)
RET
TEXT runtime·setldt(SB),NOSPLIT,$8
MOVL addr+4(FP), BX
ADDL $0x8, BX
MOVL BX, 0(SP)
NACL_SYSCALL(SYS_tls_init)
RET
TEXT runtime·sigtramp(SB),NOSPLIT,$0
get_tls(CX)
MOVL m(CX), BX
CMPL BX, $0
JNE 6(PC)
MOVL $11, BX
MOVL BX, 0(SP)
MOVL $runtime·badsignal(SB), AX
CALL AX
JMP sigtramp_ret
MOVL g(CX), DI
MOVL DI, 20(SP)
MOVL m_gsignal(BX), BX
MOVL BX, g(CX)
MOVL $11, 0(SP)
MOVL $0, 4(SP)
LEAL ctxt+4(FP), AX
MOVL AX, 8(SP)
MOVL DI, 12(SP)
CALL runtime·sighandler(SB)
get_tls(CX)
MOVL 20(SP), BX
MOVL BX, g(CX)
sigtramp_ret:
NACL_SYSCALL(SYS_exception_clear_flag)
LEAL ctxt+4(FP), BP
ADDL $64, BP
MOVL 0(BP), AX
MOVL 4(BP), CX
MOVL 8(BP), DX
MOVL 12(BP), BX
MOVL 16(BP), SP
MOVL 24(BP), SI
MOVL 28(BP), DI
MOVL 32(BP), BP
JMP BP