make a macro for pushing out thunks
This commit is contained in:
		
							parent
							
								
									5184299c7d
								
							
						
					
					
						commit
						6be5a28bb9
					
				
							
								
								
									
										38
									
								
								apply.s
									
									
									
									
									
								
							
							
						
						
									
										38
									
								
								apply.s
									
									
									
									
									
								
							|  | @ -8,40 +8,22 @@ | ||||||
| 
 | 
 | ||||||
| main: | main: | ||||||
| 	# make an integer | 	# make an integer | ||||||
| 	pushq $1 | 	thunkto %r11, $INT_code, $1 | ||||||
| 	pushq $INT_code |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 | 
 | ||||||
| 	# make a closure for adding stuff | 	# make an empty closure for adding stuff | ||||||
| 	pushq $0 | 	thunkto %r12, $FUN2_code, $plus, $0 | ||||||
| 	pushq $plus |  | ||||||
| 	pushq $FUN2_code |  | ||||||
| 	mov %rsp, %r12 |  | ||||||
| 
 | 
 | ||||||
| 	# apply first argument | 	# apply first argument | ||||||
| 	push %r11 | 	thunkto %r12, $apply1, $2, %r12, %r11 | ||||||
| 	push %r12 | 
 | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $apply1 |  | ||||||
| 	mov %rsp, %r12 |  | ||||||
| 	 |  | ||||||
| 	# apply second argument (the p.a. function part is still in r12) | 	# apply second argument (the p.a. function part is still in r12) | ||||||
| 	push %r11 | 	thunkto %r12, $apply1, $2, %r12, %r11 | ||||||
| 	push %r12 |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $apply1 |  | ||||||
| 	mov %rsp, %r12 |  | ||||||
| 
 | 
 | ||||||
| 	# print the result | 	# print the result | ||||||
| 	push %r12 | 	thunkto %r12, $print, $1, %r12 | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $print |  | ||||||
| 	mov %rsp, %r12 |  | ||||||
| 
 | 
 | ||||||
| 	# make the continuation for main (exit) | 	# make a continuation for main (exit) and set it for print call | ||||||
| 	push %rsi | 	thunkto %rsi, $main_exit, $1, %rsi | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $main_exit |  | ||||||
| 
 | 
 | ||||||
| 	mov %rsp, %rsi | 	# start evaluating the print | ||||||
| 	enter %r12 | 	enter %r12 | ||||||
|  |  | ||||||
							
								
								
									
										28
									
								
								exit_123.s
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								exit_123.s
									
									
									
									
									
								
							|  | @ -5,30 +5,10 @@ | ||||||
| 
 | 
 | ||||||
| # || -> cont | # || -> cont | ||||||
| .thunkcode main
 | .thunkcode main
 | ||||||
| 	# push a new integer | 	thunkto %r11, $INT_code, $100 | ||||||
| 	pushq $100 | 	thunkto %r12, $INT_code, $23 | ||||||
| 	pushq $INT_code | 	thunkto %r13, $plus, $2, %r11, %r12 | ||||||
| 	mov %rsp, %r11 # backup first arg | 	thunkto %rsi, $main_exit, $1 | ||||||
| 
 |  | ||||||
| 	# push another new integer |  | ||||||
| 	pushq $23 |  | ||||||
| 	pushq $INT_code |  | ||||||
| 	mov %rsp, %r12 # backup second arg |  | ||||||
| 
 |  | ||||||
| 	# push the plus |  | ||||||
| 	push %r12 |  | ||||||
| 	push %r11 |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $plus |  | ||||||
| 	mov %rsp, %r13 # backup plus |  | ||||||
| 
 |  | ||||||
| 	# push a cont thunk for main_exit |  | ||||||
| 	push %rsi |  | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $main_exit |  | ||||||
| 
 |  | ||||||
| 	# evaluate into main_exit |  | ||||||
| 	mov %rsp, %rsi |  | ||||||
| 	enter %r13 | 	enter %r13 | ||||||
| 
 | 
 | ||||||
| .include "include/main_exit.s" | .include "include/main_exit.s" | ||||||
|  |  | ||||||
							
								
								
									
										67
									
								
								fibs.s
									
									
									
									
									
								
							
							
						
						
									
										67
									
								
								fibs.s
									
									
									
									
									
								
							|  | @ -9,26 +9,15 @@ | ||||||
| # | lag1 | lag2 | -> cont | # | lag1 | lag2 | -> cont | ||||||
| .thunkcode fibs
 | .thunkcode fibs
 | ||||||
| 	# next value | 	# next value | ||||||
| 	pushq 030(%rbp) | 	thunkto %r11, $plus, $2, 020(%rbp), 030(%rbp) | ||||||
| 	pushq 020(%rbp) |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $plus |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 | 
 | ||||||
| 	# fib call with the next value | 	# fib call with the next value | ||||||
| 	push %r11 | 	thunkto %r12, $fibs, $2, 030(%rbp), %r11 | ||||||
| 	pushq 030(%rbp) |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $fibs |  | ||||||
| 	mov %rsp, %r12 |  | ||||||
| 
 | 
 | ||||||
| 	# cons list with lag1 | 	# cons list with the lagged arg | ||||||
| 	push %r12 | 	thunkto %r13, $LIST_code, $1, 020(%rbp), %r12 | ||||||
| 	push 020(%rbp) |  | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $LIST_code |  | ||||||
| 	mov %rsp, %r13 |  | ||||||
| 
 | 
 | ||||||
|  | 	# TODO simplify | ||||||
| 	# replace self with IND | 	# replace self with IND | ||||||
| 	mov %r13, 010(%rbp) | 	mov %r13, 010(%rbp) | ||||||
| 	movq $IND_code, 0(%rbp) | 	movq $IND_code, 0(%rbp) | ||||||
|  | @ -39,43 +28,15 @@ | ||||||
| 
 | 
 | ||||||
| # || -> cont | # || -> cont | ||||||
| .thunkcode fibs0
 | .thunkcode fibs0
 | ||||||
| 	pushq $1 | 	thunkto %r12, $INT_code, $1 | ||||||
| 	pushq $INT_code | 	thunkto %r11, $INT_code, $0 | ||||||
| 	mov %rsp, %r12 | 	thunkto %rbp, $fibs, $2, %r11, %r12 | ||||||
| 	pushq $0 | 	enter_rbp | ||||||
| 	pushq $INT_code |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 	 |  | ||||||
| 	push %r12 |  | ||||||
| 	push %r11 |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $fibs |  | ||||||
| 
 |  | ||||||
| 	enter %rsp |  | ||||||
| 
 | 
 | ||||||
| .thunkcode main
 | .thunkcode main
 | ||||||
| 	pushq $0 | 	thunkto %r12, $fibs0, $0 | ||||||
| 	pushq $fibs0 | 	thunkto %r11, $INT_code, $20 | ||||||
| 	mov %rsp, %r12 | 	thunkto %r11, $list_int_index, $2, %r11, %r12 | ||||||
| 
 | 	thunkto %r11, $print, $1, %r11 | ||||||
| 	pushq $20 | 	thunkto %rsi, $main_exit, $1, %rsi | ||||||
| 	pushq $INT_code |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 |  | ||||||
| 	push %r12 |  | ||||||
| 	push %r11 |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $list_int_index |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 |  | ||||||
| 	push %r11 |  | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $print |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 |  | ||||||
| 	push %rsi |  | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $main_exit |  | ||||||
| 
 |  | ||||||
| 	mov %rsp, %rsi |  | ||||||
| 	enter %r11 | 	enter %r11 | ||||||
|  |  | ||||||
|  | @ -3,14 +3,7 @@ | ||||||
| 
 | 
 | ||||||
| # | fun | arg | -> cont | # | fun | arg | -> cont | ||||||
| .thunkcode apply1
 | .thunkcode apply1
 | ||||||
| 	# push a thunk for eval | 	thunkto %rsi, $apply1_fini, $3, 030(%rbp), %rbp, %rsi | ||||||
| 	push %rsi # cont |  | ||||||
| 	push %rbp # ret (self) |  | ||||||
| 	pushq 030(%rbp) # arg |  | ||||||
| 	pushq $3 |  | ||||||
| 	pushq $apply1_fini |  | ||||||
| 
 |  | ||||||
| 	mov %rsp, %rsi # return to above thunk |  | ||||||
| 	enter 020(%rbp) # evaluate fun | 	enter 020(%rbp) # evaluate fun | ||||||
| 
 | 
 | ||||||
| # fun -> | arg | ret | cont | | # fun -> | arg | ret | cont | | ||||||
|  | @ -23,7 +16,7 @@ | ||||||
| 	pushq 020(%rbp) #push the new arg | 	pushq 020(%rbp) #push the new arg | ||||||
| 	lea 030(%rsi), %rdx # the end (first arg) | 	lea 030(%rsi), %rdx # the end (first arg) | ||||||
| 	lea (%rdx, %r11, 8), %rbx # address behind the last arg | 	lea (%rdx, %r11, 8), %rbx # address behind the last arg | ||||||
| 	 | 
 | ||||||
| 	cmp %rdx, %rbx | 	cmp %rdx, %rbx | ||||||
| 	jle apply1_fini_cont | 	jle apply1_fini_cont | ||||||
| apply1_fini_copy: | apply1_fini_copy: | ||||||
|  | @ -43,7 +36,7 @@ apply1_fini_cont: | ||||||
| 	mov -010(%rdi), %r12 # amount of args required to make the thunk | 	mov -010(%rdi), %r12 # amount of args required to make the thunk | ||||||
| 	cmp %r11, %r12 | 	cmp %r11, %r12 | ||||||
| 	jg apply1_fini_feed # not enough args, just make a bigger FUN | 	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 | 	# if there was enough args, we simply have a thunk that we want to | ||||||
| 	# continue evaluating, so let's jump to it. | 	# continue evaluating, so let's jump to it. | ||||||
| 	mov 030(%rbp), %rdi # load the original thunk | 	mov 030(%rbp), %rdi # load the original thunk | ||||||
|  |  | ||||||
|  | @ -4,12 +4,7 @@ _io_s_file: | ||||||
| 
 | 
 | ||||||
| # | int | -> cont | # | int | -> cont | ||||||
| .thunkcode print
 | .thunkcode print
 | ||||||
| 	push %rsi | 	thunkto %rsi, $print_fini, $2, %rbp, %rsi | ||||||
| 	push %rbp |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $print_fini |  | ||||||
| 
 |  | ||||||
| 	mov %rsp, %rsi |  | ||||||
| 	enter 020(%rbp) | 	enter 020(%rbp) | ||||||
| 
 | 
 | ||||||
| # arg -> | ret | cont | | # arg -> | ret | cont | | ||||||
|  |  | ||||||
|  | @ -13,28 +13,20 @@ _listops_s_file: | ||||||
| 	mov 020(%rbp), %rcx | 	mov 020(%rbp), %rcx | ||||||
| 	mov 010(%rcx), %rcx | 	mov 010(%rcx), %rcx | ||||||
| 	test %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 | 	sub $1, %rcx | ||||||
| 	pushq %rcx | 	thunkto %r11, $INT_code, %rcx # decreased index | ||||||
| 	pushq $INT_code | 	thunkto %r11, $list_int_index, $2, %r11, 030(%rsi) # new call on tail | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 |  | ||||||
| 	pushq 030(%rsi) # tail |  | ||||||
| 	push %r11 |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $list_int_index |  | ||||||
| 	mov %rsp, %r11 |  | ||||||
| 
 | 
 | ||||||
| 	primop2_cont_indirect %r11 | 	primop2_cont_indirect %r11 | ||||||
| 
 | 
 | ||||||
| list_int_index_not_found: |  | ||||||
| 	movq 0, %rax #fault |  | ||||||
| 
 |  | ||||||
| list_int_index_found: | list_int_index_found: | ||||||
| 	mov 020(%rsi), %rax #head | 	mov 020(%rsi), %rax #head | ||||||
| 	primop2_cont_indirect %rax | 	primop2_cont_indirect %rax | ||||||
| 	 | 
 | ||||||
|  | list_int_index_not_found: | ||||||
|  | 	movq 0, %rax #fault (TODO: we should have a better fault) | ||||||
| 
 | 
 | ||||||
| .endif # _listops_s_file | .endif # _listops_s_file | ||||||
|  |  | ||||||
|  | @ -39,4 +39,22 @@ _macros_s_file: | ||||||
| 	\name: | 	\name: | ||||||
| .endm | .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 | .endif # _macros_s_file | ||||||
|  |  | ||||||
|  | @ -7,25 +7,19 @@ _primops_s_file: | ||||||
| .macro .primop2 name
 | .macro .primop2 name
 | ||||||
| # | arg1 | arg2 | -> cont | # | arg1 | arg2 | -> cont | ||||||
| .thunkcode \name | .thunkcode \name | ||||||
| 	# push a thunk for finishing the plus | 	# push a thunk for collecting the first arg and set it as continuation | ||||||
| 	push %rsi # cont | 	thunkto %rsi, $\name\()_step1, $3, 030(%rbp), %rbp, %rsi | ||||||
| 	push %rbp # ret (self) |  | ||||||
| 	pushq 030(%rbp) # arg2 |  | ||||||
| 	pushq $3 |  | ||||||
| 	pushq $\name\()_step1 |  | ||||||
| 
 |  | ||||||
| 	mov %rsp, %rsi # continue to the new thunk |  | ||||||
| 	enter 0x10(%rbp) # evaluate arg1 | 	enter 0x10(%rbp) # evaluate arg1 | ||||||
| 
 | 
 | ||||||
| # arg1 -> | arg2 | ret | cont | | # arg1 -> | arg2 | ret | cont | | ||||||
| .thunkcode \name\()_step1 | .thunkcode \name\()_step1 | ||||||
| 	# this is guaranteed to be entered only once (it's a cont), so we can rewrite the thunk in place | 	# this is guaranteed to be entered only once (it's a case cont), so we | ||||||
| 	mov 020(%rbp), %rax | 	# can rewrite the thunk in place | ||||||
| 	movq $\name\()_fini, 0(%rbp) | 	xchg %rsi, 020(%rbp) # store arg1, get arg 2 | ||||||
| 	mov %rsi, 020(%rbp) | 	movq $\name\()_fini, 0(%rbp) # continue with finishing | ||||||
| 
 | 
 | ||||||
| 	mov %rbp, %rsi # continue on the rewritten thunk | 	xchg %rbp, %rsi # continue here, evaluate arg 2 | ||||||
| 	enter %rax # evaluate arg1 | 	enter_rbp | ||||||
| 
 | 
 | ||||||
| # arg2 -> | arg1 | ret | cont | | # arg2 -> | arg1 | ret | cont | | ||||||
| .thunkcode \name\()_fini | .thunkcode \name\()_fini | ||||||
|  | @ -35,8 +29,9 @@ _primops_s_file: | ||||||
| .endm | .endm | ||||||
| 
 | 
 | ||||||
| .macro primop2_ret_int val | .macro primop2_ret_int val | ||||||
|  | 	# TODO: try to generalize | ||||||
| 	# the result should now be in %rax | 	# 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 \val, 010(%rsi) | ||||||
| 	movq $INT_code, 0(%rsi) | 	movq $INT_code, 0(%rsi) | ||||||
| 
 | 
 | ||||||
|  | @ -44,6 +39,7 @@ _primops_s_file: | ||||||
| .endm | .endm | ||||||
| 
 | 
 | ||||||
| .macro primop2_cont_indirect new | .macro primop2_cont_indirect new | ||||||
|  | 	# TODO: try to generalize | ||||||
| 	mov 030(%rbp), %rdi # load the original thunk | 	mov 030(%rbp), %rdi # load the original thunk | ||||||
| 	mov 040(%rbp), %rsi # set the continuation | 	mov 040(%rbp), %rsi # set the continuation | ||||||
| 	movq \new, 010(%rdi) # set the indirect to the new thunk | 	movq \new, 010(%rdi) # set the indirect to the new thunk | ||||||
|  |  | ||||||
							
								
								
									
										26
									
								
								print_123.s
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								print_123.s
									
									
									
									
									
								
							|  | @ -8,35 +8,21 @@ | ||||||
| # || -> cont | # || -> cont | ||||||
| .thunkcode main
 | .thunkcode main
 | ||||||
| 	# push a new integer | 	# push a new integer | ||||||
| 	pushq $100 | 	thunkto %r11, $INT_code, $100 | ||||||
| 	pushq $INT_code |  | ||||||
| 	mov %rsp, %r11 # backup first arg |  | ||||||
| 
 | 
 | ||||||
| 	# push another new integer | 	# push another new integer | ||||||
| 	pushq $23 | 	thunkto %r12, $INT_code, $23 | ||||||
| 	pushq $INT_code |  | ||||||
| 	mov %rsp, %r12 # backup second arg |  | ||||||
| 
 | 
 | ||||||
| 	# push the plus | 	# push the plus | ||||||
| 	push %r12 | 	thunkto %r11, $plus, $2, %r11, %r12 | ||||||
| 	push %r11 |  | ||||||
| 	pushq $2 |  | ||||||
| 	pushq $plus |  | ||||||
| 	mov %rsp, %r11 # backup plus |  | ||||||
| 
 | 
 | ||||||
| 	# push the print | 	# push the print | ||||||
| 	push %r11 | 	thunkto %r11, $print, $1, %r11 | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $print |  | ||||||
| 	mov %rsp, %r11 # backup print |  | ||||||
| 
 | 
 | ||||||
| 	# push a cont thunk for main_exit | 	# push a cont thunk for main_exit and set continuation for main_exit | ||||||
| 	push %rsi | 	thunkto %rsi, $main_exit, $1, %rsi | ||||||
| 	pushq $1 |  | ||||||
| 	pushq $main_exit |  | ||||||
| 
 | 
 | ||||||
| 	# evaluate into main_exit | 	# evaluate into main_exit | ||||||
| 	mov %rsp, %rsi |  | ||||||
| 	enter %r11 | 	enter %r11 | ||||||
| 
 | 
 | ||||||
| .include "include/main_exit.s" | .include "include/main_exit.s" | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue