swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1 | ;;; TODO: figure out if I need to do something special with the GC here. |
| 2 | |
| 3 | [bits 32] |
| 4 | [global _call_list] |
| 5 | [extern length] |
| 6 | [extern elt] |
| 7 | ;;; This function should call it's first argument with the arguments from |
| 8 | ;;; the cons-list passed as its second argument. |
| 9 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 10 | ;;; _call_list(function pointer, cons list, edi) |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 11 | _call_list: |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 12 | ;; esi and edi are callee-saved on x86, these are the only registers |
| 13 | ;; we clobber. |
| 14 | push esi |
| 15 | push edi |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 16 | push ebp |
| 17 | mov ebp, esp |
| 18 | |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 19 | mov edi, [ebp + 20] ; Cons list |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 20 | |
| 21 | push edi |
| 22 | call length ; Length of cons list in eax |
| 23 | add esp, 4 |
| 24 | |
| 25 | mov ecx, eax ; Store length in counter |
| 26 | |
| 27 | ;; Reserve space for all the stack items |
| 28 | shl eax, 2 |
| 29 | sub esp, eax |
| 30 | |
| 31 | mov esi, esp ; Pointer to top of stack |
| 32 | |
| 33 | ;; Skip all of this if there are no arguments |
| 34 | cmp ecx, 0 |
| 35 | je .done |
| 36 | |
| 37 | .loop: |
| 38 | ;; Get the previous item. At the start ecx = the length so to get the last |
| 39 | ;; index we need to subtract 1 |
| 40 | dec ecx |
| 41 | |
| 42 | push ecx |
| 43 | push edi |
| 44 | call elt |
| 45 | add esp, 4 |
| 46 | pop ecx ; This is a scratch register, remember |
| 47 | |
| 48 | ;; We now have the ecx-th item in eax |
| 49 | ;; Remember esi is the top of the stack area reserved, so |
| 50 | mov [esi + 4 * ecx], eax |
| 51 | |
| 52 | jcxz .done |
| 53 | jmp .loop |
| 54 | |
| 55 | .done: |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 56 | mov ebx, [ebp + 16] ; Function pointer |
| 57 | mov edi, [ebp + 24] ; Closure data pointer |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 58 | call ebx |
| 59 | |
| 60 | mov esp, ebp |
| 61 | pop ebp |
swissChili | ddc9754 | 2021-07-04 11:47:42 -0700 | [diff] [blame] | 62 | pop edi |
| 63 | pop esi |
swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 64 | ret |