aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/apply.s13
-rw-r--r--include/io.s7
-rw-r--r--include/listops.s22
-rw-r--r--include/macros.s18
-rw-r--r--include/primops.s26
5 files changed, 40 insertions, 46 deletions
diff --git a/include/apply.s b/include/apply.s
index 124e434..2b88dd0 100644
--- a/include/apply.s
+++ b/include/apply.s
@@ -3,14 +3,7 @@
# | fun | arg | -> cont
.thunkcode apply1
- # push a thunk for eval
- push %rsi # cont
- push %rbp # ret (self)
- pushq 030(%rbp) # arg
- pushq $3
- pushq $apply1_fini
-
- mov %rsp, %rsi # return to above thunk
+ thunkto %rsi, $apply1_fini, $3, 030(%rbp), %rbp, %rsi
enter 020(%rbp) # evaluate fun
# fun -> | arg | ret | cont |
@@ -23,7 +16,7 @@
pushq 020(%rbp) #push the new arg
lea 030(%rsi), %rdx # the end (first arg)
lea (%rdx, %r11, 8), %rbx # address behind the last arg
-
+
cmp %rdx, %rbx
jle apply1_fini_cont
apply1_fini_copy:
@@ -43,7 +36,7 @@ apply1_fini_cont:
mov -010(%rdi), %r12 # amount of args required to make the thunk
cmp %r11, %r12
jg apply1_fini_feed # not enough args, just make a bigger FUN
-
+
# if there was enough args, we simply have a thunk that we want to
# continue evaluating, so let's jump to it.
mov 030(%rbp), %rdi # load the original thunk
diff --git a/include/io.s b/include/io.s
index c348e7f..7e78c1b 100644
--- a/include/io.s
+++ b/include/io.s
@@ -4,12 +4,7 @@ _io_s_file:
# | int | -> cont
.thunkcode print
- push %rsi
- push %rbp
- pushq $2
- pushq $print_fini
-
- mov %rsp, %rsi
+ thunkto %rsi, $print_fini, $2, %rbp, %rsi
enter 020(%rbp)
# arg -> | ret | cont |
diff --git a/include/listops.s b/include/listops.s
index eb8f6cf..447c125 100644
--- a/include/listops.s
+++ b/include/listops.s
@@ -13,28 +13,20 @@ _listops_s_file:
mov 020(%rbp), %rcx
mov 010(%rcx), %rcx
test %rcx, %rcx
- jz list_int_index_found #we are taking 0, all happy, return it
+ jz list_int_index_found # we are taking the head, all happy, return it
- #more probably we need to continue, make replacement thunks
+ # more likely, we need to continue -- make new args thunks
sub $1, %rcx
- pushq %rcx
- pushq $INT_code
- mov %rsp, %r11
-
- pushq 030(%rsi) # tail
- push %r11
- pushq $2
- pushq $list_int_index
- mov %rsp, %r11
+ thunkto %r11, $INT_code, %rcx # decreased index
+ thunkto %r11, $list_int_index, $2, %r11, 030(%rsi) # new call on tail
primop2_cont_indirect %r11
-list_int_index_not_found:
- movq 0, %rax #fault
-
list_int_index_found:
mov 020(%rsi), %rax #head
primop2_cont_indirect %rax
-
+
+list_int_index_not_found:
+ movq 0, %rax #fault (TODO: we should have a better fault)
.endif # _listops_s_file
diff --git a/include/macros.s b/include/macros.s
index c857995..c52f6b0 100644
--- a/include/macros.s
+++ b/include/macros.s
@@ -39,4 +39,22 @@ _macros_s_file:
\name:
.endm
+.macro thunkargs arg,args:vararg
+ .ifnb \arg
+ thunkargs \args
+ pushq \arg
+ .endif
+.endm
+
+.macro thunk code:req,n:req,args:vararg
+ thunkargs \args
+ pushq \n
+ pushq \code
+.endm
+
+.macro thunkto reg:req,code:req,n:req,args:vararg
+ thunk \code,\n,\args
+ mov %rsp, \reg
+.endm
+
.endif # _macros_s_file
diff --git a/include/primops.s b/include/primops.s
index f019bca..3fb03b8 100644
--- a/include/primops.s
+++ b/include/primops.s
@@ -7,25 +7,19 @@ _primops_s_file:
.macro .primop2 name
# | arg1 | arg2 | -> cont
.thunkcode \name
- # push a thunk for finishing the plus
- push %rsi # cont
- push %rbp # ret (self)
- pushq 030(%rbp) # arg2
- pushq $3
- pushq $\name\()_step1
-
- mov %rsp, %rsi # continue to the new thunk
+ # push a thunk for collecting the first arg and set it as continuation
+ thunkto %rsi, $\name\()_step1, $3, 030(%rbp), %rbp, %rsi
enter 0x10(%rbp) # evaluate arg1
# arg1 -> | arg2 | ret | cont |
.thunkcode \name\()_step1
- # this is guaranteed to be entered only once (it's a cont), so we can rewrite the thunk in place
- mov 020(%rbp), %rax
- movq $\name\()_fini, 0(%rbp)
- mov %rsi, 020(%rbp)
+ # this is guaranteed to be entered only once (it's a case cont), so we
+ # can rewrite the thunk in place
+ xchg %rsi, 020(%rbp) # store arg1, get arg 2
+ movq $\name\()_fini, 0(%rbp) # continue with finishing
- mov %rbp, %rsi # continue on the rewritten thunk
- enter %rax # evaluate arg1
+ xchg %rbp, %rsi # continue here, evaluate arg 2
+ enter_rbp
# arg2 -> | arg1 | ret | cont |
.thunkcode \name\()_fini
@@ -35,8 +29,9 @@ _primops_s_file:
.endm
.macro primop2_ret_int val
+ # TODO: try to generalize
# the result should now be in %rax
- mov 030(%rbp), %rsi # save result to the original plus thunk
+ mov 030(%rbp), %rsi # save result to the original primop2 thunk
movq \val, 010(%rsi)
movq $INT_code, 0(%rsi)
@@ -44,6 +39,7 @@ _primops_s_file:
.endm
.macro primop2_cont_indirect new
+ # TODO: try to generalize
mov 030(%rbp), %rdi # load the original thunk
mov 040(%rbp), %rsi # set the continuation
movq \new, 010(%rdi) # set the indirect to the new thunk