-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.c
165 lines (146 loc) · 3.93 KB
/
generator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <threads.h>
#include <sys/mman.h>
#include <unistd.h>
#define NOB_IMPLEMENTATION
#define NOB_STRIP_PREFIX
#include "nob.h"
#include "generator.h"
#define da_last(da) (NOB_ASSERT((da)->count > 0), (da)->items[(da)->count-1])
#define GENERATOR_STACK_CAPACITY (1024*getpagesize())
typedef struct {
Generator **items;
size_t count;
size_t capacity;
} Generator_Stack;
thread_local Generator_Stack generator_stack = {0};
void generator_init(void)
{
Generator *g = malloc(sizeof(Generator));
assert(g != NULL && "Buy more RAM lol");
memset(g, 0, sizeof(*g));
da_append(&generator_stack, g);
}
// Linux x86_64 call convention
// %rdi, %rsi, %rdx, %rcx, %r8, and %r9
void* __attribute__((naked)) generator_next(Generator *g, void *arg)
{
UNUSED(g);
UNUSED(arg);
// @arch
if(g->dead){asm("xor %rax, %rax\n ret\n");}
asm(
" pushq %rdi\n"
" pushq %rbp\n"
" pushq %rbx\n"
" pushq %r12\n"
" pushq %r13\n"
" pushq %r14\n"
" pushq %r15\n"
" movq %rsp, %rdx\n" // rsp
" jmp generator_switch_context\n");
}
void __attribute__((naked)) generator_restore_context(void *rsp)
{
// @arch
(void)rsp;
asm(
" movq %rdi, %rsp\n"
" popq %r15\n"
" popq %r14\n"
" popq %r13\n"
" popq %r12\n"
" popq %rbx\n"
" popq %rbp\n"
" popq %rdi\n"
" ret\n");
}
void __attribute__((naked)) generator_restore_context_with_return(void *rsp, void *arg)
{
// @arch
UNUSED(rsp);
UNUSED(arg);
asm(
" movq %rdi, %rsp\n"
" movq %rsi, %rax\n"
" popq %r15\n"
" popq %r14\n"
" popq %r13\n"
" popq %r12\n"
" popq %rbx\n"
" popq %rbp\n"
" popq %rdi\n"
" ret\n");
}
void generator_switch_context(Generator *g, void *arg, void *rsp)
{
da_last(&generator_stack)->rsp = rsp;
da_append(&generator_stack, g);
if (g->fresh) {
g->fresh = false;
// ******************************
// ^ ^rsp
// stack_base
void **rsp = (void**)((char*)g->stack_base + GENERATOR_STACK_CAPACITY);
*(rsp-3) = arg;
generator_restore_context(g->rsp);
} else {
generator_restore_context_with_return(g->rsp, arg);
}
}
void *__attribute__((naked)) generator_yield(void *arg)
{
UNUSED(arg);
// @arch
asm(
" pushq %rdi\n"
" pushq %rbp\n"
" pushq %rbx\n"
" pushq %r12\n"
" pushq %r13\n"
" pushq %r14\n"
" pushq %r15\n"
" movq %rsp, %rsi\n" // rsp
" jmp generator_return\n");
}
void generator_return(void *arg, void *rsp)
{
da_last(&generator_stack)->rsp = rsp;
generator_stack.count -= 1;
generator_restore_context_with_return(da_last(&generator_stack)->rsp, arg);
}
void generator__finish_current(void)
{
da_last(&generator_stack)->dead = true;
generator_stack.count -= 1;
generator_restore_context_with_return(da_last(&generator_stack)->rsp, NULL);
}
Generator *generator_create(void (*f)(void*))
{
Generator *g = malloc(sizeof(Generator));
assert(g != NULL && "Buy more RAM lol");
memset(g, 0, sizeof(*g));
g->stack_base = mmap(NULL, GENERATOR_STACK_CAPACITY, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_STACK|MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0);
assert(g->stack_base != MAP_FAILED);
void **rsp = (void**)((char*)g->stack_base + GENERATOR_STACK_CAPACITY);
*(--rsp) = generator__finish_current;
*(--rsp) = f;
*(--rsp) = 0; // push rdi
*(--rsp) = 0; // push rbx
*(--rsp) = 0; // push rbp
*(--rsp) = 0; // push r12
*(--rsp) = 0; // push r13
*(--rsp) = 0; // push r14
*(--rsp) = 0; // push r15
g->rsp = rsp;
g->fresh = true;
return g;
}
void generator_destroy(Generator *g)
{
munmap(g->stack_base, GENERATOR_STACK_CAPACITY);
free(g);
}