From 3a3bd78ff4ed8dc52cb20dbe1490f46064ea42ab Mon Sep 17 00:00:00 2001 From: Mirek Kratochvil Date: Wed, 2 Aug 2023 21:28:45 +0200 Subject: [PATCH] it computs --- macros.s | 4 ++- uskel.s | 88 +++++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 80 insertions(+), 12 deletions(-) diff --git a/macros.s b/macros.s index fcb80eb..8bc05da 100644 --- a/macros.s +++ b/macros.s @@ -9,7 +9,9 @@ .endm .macro continue - enter %rdi + mov %rsi, %rax + mov %rbp, %rsi + enter %rax .endm .macro thunkenv arg, dest diff --git a/uskel.s b/uskel.s index d558856..dad9bc5 100644 --- a/uskel.s +++ b/uskel.s @@ -43,8 +43,7 @@ _uskel_start: # save the memory ptr mov %rdi, _memory_state - mov $0, %rsi # no result for main - mov $0, %rdi # set continuation to exit + mov $0, %rsi # set continuation to exit enter %r15 # run the program # Simple values and boxed machine integers @@ -145,22 +144,89 @@ THU_scavenge: # Actual code! # +# || -> cont .makethunk main - # push a thunk for main_exit mov _memory_state, %r15 - mov %r15, %r14 # backup for later + + # push a new integer + mov %r15, %r11 # backup first arg + movq $INT_code, 0x00(%r15) + movq $100, 0x08(%r15) + add $0x10, %r15 + + # push another new integer + mov %r15, %r12 # backup second arg + movq $INT_code, 0x00(%r15) + movq $23, 0x08(%r15) + add $0x10, %r15 + + # push the plus + mov %r15, %r13 # backup plus + movq $plus, 0x00(%r15) + movq $2, 0x08(%r15) + mov %r11, 0x10(%r15) + mov %r12, 0x18(%r15) + add $0x20, %r15 + + # push a cont thunk for main_exit + mov %r15, %r14 # backup cont thunk movq $main_exit, 0x00(%r15) movq $1, 0x08(%r15) - mov %rdi, 0x10(%r15) + mov %rsi, 0x10(%r15) add $0x18, %r15 - mov %r15, _memory_state - # continue 123 - movq $INT_code, (%rbp) - movq $123, 0x8(%rbp) - mov %rbp, %rsi - enter %r14 + mov %r15, _memory_state + + # evaluate into main_exit + mov %r14, %rsi + enter %r13 + +# exitcode -> | cont (unused, should be 0) | .makethunk main_exit mov 0x8(%rsi), %rdi mov $0x3c, %rax syscall # exit %rdi + +# | arg1 | arg2 | -> cont +.makethunk plus + # push a thunk for finishing the plus + mov _memory_state, %r15 + mov %r15, %r14 # plus_step1 origin + movq $plus_step1, 0x00(%r15) + movq $3, 0x08(%r15) + mov 0x18(%rbp), %rax + mov %rax, 0x10(%r15) + mov %rbp, 0x18(%r15) + mov %rsi, 0x20(%r15) + add $0x28, %r15 + mov %r15, _memory_state + + # evaluate arg0 + mov %r14, %rsi + enter 0x10(%rbp) + +# arg0 -> | arg1 | ret | cont | +.makethunk plus_step1 + # this is guaranteed to be entered only once (it's a cont), so we can rewrite the thunk in place + mov 0x10(%rbp), %rax + movq $plus_fini, 0x00(%rbp) + mov %rsi, 0x10(%rbp) + + mov %rbp, %rsi # continue on the rewritten thunk + enter %rax # evaluate arg1 + +# arg1 -> | arg0 | ret | cont | +.makethunk plus_fini + mov 0x8(%rsi), %rax # arg1 + mov 0x10(%rbp), %rsi + add 0x8(%rsi), %rax # + arg0 + + mov 0x18(%rbp), %rsi # rewrite the resulting thunk + movq $INT_code, 0x00(%rsi) + mov %rax, 0x08(%rsi) + # result is in rsi already + enter 0x20(%rbp) + +# Q: are there gonna be functions that have both the argument AND the cont? +# A: No, either stuff is entered as return-continuation (takes res) or as forward call (takes cont) +# (needs validation)