Ruby 2.7.0p0(2019-12-25revision647ee6f091eafcce70ffb75ddf7e121e192ab217)

Context.h
Go to the documentation of this file.
1 /*
2 * This file is part of the "Coroutine" project and released under the MIT License.
3 *
4 * Created by Samuel Williams on 10/5/2018.
5 * Copyright, 2018, by Samuel Williams. All rights reserved.
6 */
7
8 #pragma once
9
10 #include <assert.h>
11 #include <string.h>
12
13 #define COROUTINE __declspec(noreturn) void
14
15 enum {
16 COROUTINE_REGISTERS = 8,
17 COROUTINE_XMM_REGISTERS = 1+10*2,
18 };
19
20 struct coroutine_context
21 {
22 void **stack_pointer;
23 };
24
25 typedef void(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self);
26
27 void coroutine_trampoline();
28
29 static inline void coroutine_initialize_main(struct coroutine_context * context) {
30 context->stack_pointer = NULL;
31 }
32
33 static inline void coroutine_initialize(
34 struct coroutine_context *context,
35 coroutine_start start,
36 void *stack,
37 size_t size
38 ) {
39 assert(start && stack && size >= 1024);
40
41 // Stack grows down. Force 16-byte alignment.
42 char * top = (char*)stack + size;
43 context->stack_pointer = (void**)((uintptr_t)top & ~0xF);
44
45 /* Win64 ABI requires space for arguments */
46 context->stack_pointer -= 4;
47
48 /* Return address */
49 *--context->stack_pointer = 0;
50 *--context->stack_pointer = (void*)start;
51 *--context->stack_pointer = (void*)coroutine_trampoline;
52
53 /* Windows Thread Information Block */
54 /* *--context->stack_pointer = 0; */ /* gs:[0x00] is not used */
55 *--context->stack_pointer = (void*)top; /* gs:[0x08] */
56 *--context->stack_pointer = (void*)stack; /* gs:[0x10] */
57
58 context->stack_pointer -= COROUTINE_REGISTERS;
59 memset(context->stack_pointer, 0, sizeof(void*) * COROUTINE_REGISTERS);
60 memset(context->stack_pointer - COROUTINE_XMM_REGISTERS, 0, sizeof(void*) * COROUTINE_XMM_REGISTERS);
61 }
62
63 struct coroutine_context * coroutine_transfer(struct coroutine_context * current, struct coroutine_context * target);
64
65 static inline void coroutine_destroy(struct coroutine_context * context)
66 {
67 }
void
void
Definition: rb_mjit_min_header-2.7.0.h:13273
coroutine_context::stack
void * stack
Definition: Context.h:29
COROUTINE_XMM_REGISTERS
@ COROUTINE_XMM_REGISTERS
Definition: Context.h:17
assert
#define assert(x)
Definition: dlmalloc.c:1176
memset
void * memset(void *, int, size_t)
COROUTINE_REGISTERS
@ COROUTINE_REGISTERS
Definition: Context.h:15
assert.h
uintptr_t
unsigned int uintptr_t
Definition: win32.h:106
NULL
#define NULL
Definition: _sdbm.c:101
coroutine_start
COROUTINE(* coroutine_start)(struct coroutine_context *from, struct coroutine_context *self)
Definition: Context.h:22
coroutine_context::from
struct coroutine_context * from
Definition: Context.h:37
size
int size
Definition: encoding.c:58
coroutine_context
Definition: Context.h:17
coroutine_transfer
struct coroutine_context * coroutine_transfer(struct coroutine_context *current, struct coroutine_context *target)
Definition: Context.c:115
top
unsigned int top
Definition: nkf.c:4323
coroutine_trampoline
COROUTINE coroutine_trampoline(void *_start, void *_context)
Definition: Context.c:16
coroutine_context::stack_pointer
void ** stack_pointer
Definition: Context.h:19