Ruby: coroutine/win64/Context.h Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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