Skip to content

Commit 6e5d422

Browse files
authored
Merge pull request #8 from oli-obk/fiber
Use fibers on windows
2 parents 0f116f3 + 2c3befa commit 6e5d422

File tree

11 files changed

+389
-178
lines changed

11 files changed

+389
-178
lines changed

Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@ doctest = false
1919
test = false
2020

2121
[dependencies]
22-
cfg-if = "0.1"
23-
libc = "0.2"
22+
cfg-if = "0.1.6"
23+
libc = "0.2.45"
24+
25+
[target.'cfg(windows)'.dependencies.winapi]
26+
version = "0.3.6"
27+
features = [
28+
'memoryapi',
29+
'winbase',
30+
'fibersapi',
31+
'processthreadsapi',
32+
'minwindef',
33+
]
2434

2535
[build-dependencies]
2636
cc = "1.0"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
A stack-growth library for Rust. Enables annotating fixed points in programs
99
where the stack may want to grow larger. Spills over to the heap if the stack
10-
has it its limit.
10+
has hit its limit.
1111

1212
This library is intended on helping implement recursive algorithms.
1313

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
cfg.define("APPLE", None);
1515
} else if target.contains("windows") {
1616
cfg.define("WINDOWS", None);
17+
cfg.file("src/arch/windows.c");
1718
} else {
1819
panic!("\n\nusing currently unsupported target triple with \
1920
stacker: {}\n\n", target);

src/arch/i686.S

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,25 @@
22

33
.text
44

5-
GLOBAL(__stacker_black_box):
6-
ret
7-
85
GLOBAL(__stacker_stack_pointer):
96
mov %esp, %eax
107
ret
118

12-
#if defined(WINDOWS)
13-
GLOBAL(__stacker_get_tib_32):
14-
mov %fs:0x18, %eax
15-
ret
16-
#endif
17-
189
GLOBAL(__stacker_switch_stacks):
10+
// CFI instructions tells the unwinder how to unwind this function
11+
// This enables unwinding through our extended stacks and also
12+
// backtrackes
13+
.cfi_startproc
1914
push %ebp
15+
.cfi_def_cfa_offset 8 // restore esp by adding 8
16+
.cfi_offset ebp, -8 // restore ebp from the stack
2017
mov %esp, %ebp
21-
mov 8(%ebp), %esp // switch to our new stack
18+
.cfi_def_cfa_register ebp // restore esp from ebp
19+
mov 16(%ebp), %esp // switch to our new stack
2220
mov 12(%ebp), %eax // load function we're going to call
23-
push 16(%ebp) // push argument to first function
21+
push 8(%ebp) // push argument to first function
2422
call *%eax // call our function pointer
2523
mov %ebp, %esp // restore the old stack pointer
2624
pop %ebp
2725
ret
26+
.cfi_endproc

src/arch/i686.asm

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,9 @@
22
.MODEL FLAT, C
33
.CODE
44

5-
__stacker_black_box PROC
6-
RET
7-
__stacker_black_box ENDP
8-
95
__stacker_stack_pointer PROC
106
MOV EAX, ESP
117
RET
128
__stacker_stack_pointer ENDP
139

14-
__stacker_get_tib_32 PROC
15-
ASSUME FS:NOTHING
16-
MOV EAX, FS:[24]
17-
ASSUME FS:ERROR
18-
RET
19-
__stacker_get_tib_32 ENDP
20-
21-
__stacker_switch_stacks PROC
22-
PUSH EBP
23-
MOV EBP, ESP
24-
MOV ESP, [EBP + 8] ; switch stacks
25-
MOV EAX, [EBP + 12] ; load the function we're going to call
26-
PUSH [EBP + 16] ; push the argument to this function
27-
CALL EAX ; call the next function
28-
MOV ESP, EBP ; restore the old stack pointer
29-
POP EBP
30-
RET
31-
__stacker_switch_stacks ENDP
32-
3310
END

src/arch/windows.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <windows.h>
2+
3+
PVOID __stacker_get_current_fiber() {
4+
return GetCurrentFiber();
5+
}

src/arch/x86_64.S

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,23 @@
22

33
.text
44

5-
GLOBAL(__stacker_black_box):
6-
ret
7-
85
GLOBAL(__stacker_stack_pointer):
96
movq %rsp, %rax
107
ret
118

12-
#if defined(WINDOWS)
13-
#define ARG1 %rcx
14-
#define ARG2 %rdx
15-
#define ARG3 %r8
16-
#else
17-
#define ARG1 %rdi
18-
#define ARG2 %rsi
19-
#define ARG3 %rdx
20-
#endif
21-
22-
#if defined(WINDOWS)
23-
GLOBAL(__stacker_get_tib_64):
24-
mov %gs:0x30, %rax
25-
ret
26-
#endif
27-
289
GLOBAL(__stacker_switch_stacks):
10+
// CFI instructions tells the unwinder how to unwind this function
11+
// This enables unwinding through our extended stacks and also
12+
// backtrackes
13+
.cfi_startproc
2914
push %rbp
15+
.cfi_def_cfa_offset 16 // restore rsp by adding 16
16+
.cfi_offset rbp, -16 // restore rbp from the stack
3017
mov %rsp, %rbp
31-
mov ARG1, %rsp // switch to our new stack
32-
mov ARG3, ARG1 // move the data pointer to the first argument
33-
call *ARG2 // call our function pointer
18+
.cfi_def_cfa_register rbp // restore rsp from rbp
19+
mov %rdx, %rsp // switch to our new stack
20+
call *%rsi // call our function pointer, data argument in %rdi
3421
mov %rbp, %rsp // restore the old stack pointer
3522
pop %rbp
3623
ret
24+
.cfi_endproc

src/arch/x86_64.asm

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
_text SEGMENT
22

3-
__stacker_black_box PROC
4-
RET
5-
__stacker_black_box ENDP
6-
73
__stacker_stack_pointer PROC
84
MOV RAX, RSP
95
RET
106
__stacker_stack_pointer ENDP
117

12-
__stacker_switch_stacks PROC
13-
PUSH RBP
14-
MOV RBP, RSP
15-
MOV RSP, RCX ; switch to our new stack
16-
MOV RCX, R8 ; move the data pointer to the first argument
17-
CALL RDX ; call our function pointer
18-
MOV RSP, RBP ; restore the old stack pointer
19-
POP RBP
20-
RET
21-
__stacker_switch_stacks ENDP
22-
23-
END
8+
END

0 commit comments

Comments
 (0)