From e5118fa9fb6b1925f785f810767ca642097622d3 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Wed, 13 May 2020 00:08:18 -0700 Subject: handle nulls in lookup Cleaner abstraction, but adds 3 instructions to our overhead for handles, including one potentially-hard-to-predict jump :/ I wish I could have put the alloc id in eax for the comparison as well, to save a few bytes of instruction space. But that messes up the non-null case. --- 069allocate.subx | 40 ++++++++++++++++++++++++++++++++++++---- apps/assort | Bin 44102 -> 44213 bytes apps/braces | Bin 45973 -> 46084 bytes apps/calls | Bin 50634 -> 50745 bytes apps/crenshaw2-1 | Bin 43453 -> 43564 bytes apps/crenshaw2-1b | Bin 44000 -> 44111 bytes apps/dquotes | Bin 47724 -> 47835 bytes apps/factorial | Bin 42546 -> 42657 bytes apps/hex | Bin 46292 -> 46403 bytes apps/pack | Bin 56437 -> 56548 bytes apps/sigils | Bin 58358 -> 58469 bytes apps/survey | Bin 54048 -> 54159 bytes apps/tests | Bin 42874 -> 42985 bytes 13 files changed, 36 insertions(+), 4 deletions(-) diff --git a/069allocate.subx b/069allocate.subx index 2a40866e..31afc4ff 100644 --- a/069allocate.subx +++ b/069allocate.subx @@ -123,6 +123,7 @@ allocate-raw: # ad: (addr allocation-descriptor), n: int, out: (addr handle) 89/copy 0/mod/indirect 2/rm32/edx . . . 0/r32/eax . . # copy eax to *edx # out->payload = ad->curr 8b/copy 0/mod/indirect 1/rm32/ecx . . . 0/r32/eax . . # copy *ecx to eax +$allocate-raw:save-payload-in-eax: 89/copy 1/mod/*+disp8 2/rm32/edx . . . 0/r32/eax 4/disp8 . # copy eax to *(edx+4) # *out->payload = Next-alloc-id 8b/copy 1/mod/*+disp8 2/rm32/edx . . . 7/r32/edi 4/disp8 . # copy *(edx+4) to edi @@ -260,8 +261,13 @@ lookup: # h: (handle T) -> eax: (addr T) 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp # . save registers 51/push-ecx + # eax = 0 + 31/xor 3/mod/direct 0/rm32/eax . . . 0/r32/eax . . # clear eax # ecx = handle->alloc_id 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 1/r32/ecx 8/disp8 . # copy *(ebp+8) to ecx + # if (ecx == 0) return 0 + 81 7/subop/compare 3/mod/direct 1/rm32/ecx . . . . . 0/imm32 # compare ecx + 74/jump-if-= $lookup:end/disp8 # eax = handle->address (payload) 8b/copy 1/mod/*+disp8 5/rm32/ebp . . . 0/r32/eax 0xc/disp8 . # copy *(ebp+12) to eax # if (ecx != *eax) abort @@ -295,7 +301,6 @@ test-lookup-success: # . prologue 55/push-ebp 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp - # . save registers # var ad/ebx: allocation-descriptor 68/push 0/imm32/limit 68/push 0/imm32/curr @@ -344,9 +349,36 @@ test-lookup-success: 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # clean up c7 0/subop/copy 0/mod/indirect 5/rm32/.disp32 . . . Next-alloc-id/disp32 0x100/imm32 # copy to *Next-alloc-id - # . restore registers - 5a/pop-to-edx - 59/pop-to-ecx + # . epilogue + 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp + 5d/pop-to-ebp + c3/return + +test-lookup-null-returns-null: + # . prologue + 55/push-ebp + 89/copy 3/mod/direct 5/rm32/ebp . . . 4/r32/esp . . # copy esp to ebp + # var handle/ecx: handle + 68/push 0/imm32/address + 68/push 0/imm32/alloc-id + 89/copy 3/mod/direct 1/rm32/ecx . . . 4/r32/esp . . # copy esp to ecx + # eax = lookup(handle) + # . . push args + ff 6/subop/push 1/mod/*+disp8 1/rm32/ecx . . . . 4/disp8 . # push *(ecx+4) + ff 6/subop/push 0/mod/indirect 1/rm32/ecx . . . . . . # push *ecx + # . . call + e8/call lookup/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 8/imm32 # add to esp + # check-ints-equal(eax, 0, msg) + # . . push args + 68/push "F - test-lookup-null-returns-null"/imm32 + 68/push 0/imm32 + 50/push-eax + # . . call + e8/call check-ints-equal/disp32 + # . . discard args + 81 0/subop/add 3/mod/direct 4/rm32/esp . . . . . 0xc/imm32 # add to esp # . epilogue 89/copy 3/mod/direct 4/rm32/esp . . . 5/r32/ebp . . # copy ebp to esp 5d/pop-to-ebp diff --git a/apps/assort b/apps/assort index 7bd69ebf..6789c614 100755 Binary files a/apps/assort and b/apps/assort differ diff --git a/apps/braces b/apps/braces index a79cfb55..9df9e53b 100755 Binary files a/apps/braces and b/apps/braces differ diff --git a/apps/calls b/apps/calls index 240972dd..706f1300 100755 Binary files a/apps/calls and b/apps/calls differ diff --git a/apps/crenshaw2-1 b/apps/crenshaw2-1 index 8d362a1e..57dd8c64 100755 Binary files a/apps/crenshaw2-1 and b/apps/crenshaw2-1 differ diff --git a/apps/crenshaw2-1b b/apps/crenshaw2-1b index 3d700cc6..84fbf568 100755 Binary files a/apps/crenshaw2-1b and b/apps/crenshaw2-1b differ diff --git a/apps/dquotes b/apps/dquotes index 9ee4a375..b3b7eae8 100755 Binary files a/apps/dquotes and b/apps/dquotes differ diff --git a/apps/factorial b/apps/factorial index 6e240627..e3b984cd 100755 Binary files a/apps/factorial and b/apps/factorial differ diff --git a/apps/hex b/apps/hex index 8d8bfe6e..24669d2a 100755 Binary files a/apps/hex and b/apps/hex differ diff --git a/apps/pack b/apps/pack index 05c15b24..67812b05 100755 Binary files a/apps/pack and b/apps/pack differ diff --git a/apps/sigils b/apps/sigils index 9f33941e..be9992ec 100755 Binary files a/apps/sigils and b/apps/sigils differ diff --git a/apps/survey b/apps/survey index 7584e54c..9baaadb8 100755 Binary files a/apps/survey and b/apps/survey differ diff --git a/apps/tests b/apps/tests index dec346d2..8b90a55b 100755 Binary files a/apps/tests and b/apps/tests differ -- cgit 1.4.1-2-gfad0 39' href='#n139'>139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235