1 # Helpers for decimal ints.
3 # if slice doesn't contain a decimal number, return 0
4 parse-decimal-int-from-slice: # in: (addr slice) -> out/eax: int
11 8b/-> *(ebp+8) 1/r32/ecx
13 (parse-decimal-int-helper *ecx *(ecx+4)) # => eax
14 $parse-decimal-int-from-slice:end:
22 # if slice doesn't contain a decimal number, return 0
23 parse-decimal-int: # in: (addr array byte) -> result/eax: int
31 8b/-> *(ebp+8) 0/r32/eax
32 # var start/ecx: (addr byte) = &in->data
33 8d/copy-address *(eax+4) 1/r32/ecx
34 # var end/edx: (addr byte) = &in->data[in->size]
36 8d/copy-address *(eax+edx+4) 2/r32/edx
38 (parse-decimal-int-helper %ecx %edx) # => eax
39 $parse-decimal-int:end:
48 parse-decimal-int-from-stream: # in: (addr stream byte) -> result/eax: int
56 8b/-> *(ebp+8) 0/r32/eax
57 # var start/ecx: (addr byte) = &in->data[in->read]
58 8b/-> *(eax+4) 1/r32/ecx
59 8d/copy-address *(eax+ecx+0xc) 1/r32/ecx
60 # var end/edx: (addr byte) = &in->data[in->write]
62 8d/copy-address *(eax+edx+0xc) 2/r32/edx
63 # trim a trailing newline
67 # if it's a newline, break
68 8a/byte-> *edx 0/r32/eax
69 25/and-eax-with 0xff/imm32
70 3d/compare-eax-and 0xa/imm32/newline
71 74/jump-if-= break/disp8
72 # not a newline, so restore it
76 (parse-decimal-int-helper %ecx %edx) # => eax
77 $parse-decimal-int-from-stream:end:
86 parse-decimal-int-helper: # start: (addr byte), end: (addr byte) -> result/eax: int
96 # var curr/esi: (addr byte) = start
97 8b/-> *(ebp+8) 6/r32/esi
99 8b/-> *(ebp+0xc) 7/r32/edi
100 # var negate?/edx: boolean = false
101 ba/copy-to-edx 0/imm32/false
102 # if (*curr == '-') ++curr, negate = true
104 $parse-decimal-int-helper:negative:
105 b8/copy-to-eax 0/imm32
106 8a/copy-byte *esi 0/r32/AL
107 3d/compare-eax-and 0x2d/imm32/-
108 75/jump-if-!= break/disp8
112 ba/copy-to-edx 1/imm32/true
116 # var result/eax: int = 0
117 b8/copy-to-eax 0/imm32
118 # var digit/ecx: int = 0
119 b9/copy-to-ecx 0/imm32
120 # const TEN/ebx: int = 10
121 bb/copy-to-ebx 0xa/imm32
123 $parse-decimal-int-helper:loop:
124 # if (curr >= in->end) break
125 39/compare %esi 7/r32/edi
126 73/jump-if-addr>= break/disp8
127 # if !decimal-digit?(*curr) return 0
128 8a/copy-byte *esi 1/r32/CL
130 (decimal-digit? %ecx) # => eax
132 3d/compare-eax-and 0/imm32/false
133 75/jump-if-!= break/disp8
135 b8/copy-to-eax 0/imm32
136 eb/jump $parse-decimal-int-helper:negate/disp8
139 # digit = from-decimal-char(*curr)
140 81 5/subop/subtract %ecx 0x30/imm32/zero
141 # TODO: error checking
142 # result = result * 10 + digit
143 ba/copy-to-edx 0/imm32
144 f7 4/subop/multiply-into-edx-eax %ebx
145 # TODO: check edx for overflow
146 01/add %eax 1/r32/ecx
152 $parse-decimal-int-helper:negate:
153 # if (negate?) result = -result
156 81 7/subop/compare %edx 0/imm32/false
157 74/jump-if-= break/disp8
158 f7 3/subop/negate %eax
160 $parse-decimal-int-helper:end:
161 # . restore registers
172 test-parse-decimal-int-from-slice-single-digit:
180 b8/copy-to-eax "3"/imm32
182 8d/copy-address *(eax+ecx+4) 1/r32/ecx
183 05/add-to-eax 4/imm32
184 # var slice/ecx: slice = {eax, ecx}
189 (parse-decimal-int-from-slice %ecx) # => eax
190 (check-ints-equal %eax 3 "F - test-parse-decimal-int-from-slice-single-digit")
191 $test-parse-decimal-int-helper-single-digit:end:
192 # . restore registers
200 test-parse-decimal-int-from-slice-multi-digit:
208 b8/copy-to-eax "34"/imm32
210 8d/copy-address *(eax+ecx+4) 1/r32/ecx
211 05/add-to-eax 4/imm32
212 # var slice/ecx: slice = {eax, ecx}
217 (parse-decimal-int-from-slice %ecx) # => eax
218 (check-ints-equal %eax 0x22 "F - test-parse-decimal-int-from-slice-multi-digit") # 34 in hex
219 $test-parse-decimal-int-helper-multi-digit:end:
220 # . restore registers
228 test-parse-decimal-int-from-slice-zero:
236 b8/copy-to-eax "00"/imm32
238 8d/copy-address *(eax+ecx+4) 1/r32/ecx
239 05/add-to-eax 4/imm32
240 # var slice/ecx: slice = {eax, ecx}
245 (parse-decimal-int-from-slice %ecx) # => eax
246 (check-ints-equal %eax 0 "F - test-parse-decimal-int-from-slice-zero")
247 $test-parse-decimal-int-helper-zero:end:
248 # . restore registers
256 test-parse-decimal-int-from-slice-negative:
264 b8/copy-to-eax "-3"/imm32
266 8d/copy-address *(eax+ecx+4) 1/r32/ecx
267 05/add-to-eax 4/imm32
268 # var slice/ecx: slice = {eax, ecx}
273 (parse-decimal-int-from-slice %ecx) # => eax
274 (check-ints-equal %eax -3 "F - test-parse-decimal-int-from-slice-negative")
275 $test-parse-decimal-int-helper-negative:end:
276 # . restore registers
284 test-parse-decimal-int-from-slice-multi-digit-negative:
292 b8/copy-to-eax "-32"/imm32
294 8d/copy-address *(eax+ecx+4) 1/r32/ecx
295 05/add-to-eax 4/imm32
296 # var slice/ecx: slice = {eax, ecx}
301 (parse-decimal-int-from-slice %ecx) # => eax
302 (check-ints-equal %eax -0x20 "F - test-parse-decimal-int-from-slice-multi-digit-negative") # -32 in hex
303 $test-parse-decimal-int-helper-multi-digit-negative:end:
304 # . restore registers
312 decimal-size: # n: int -> result/eax: int
321 # eax, edx = eax/10, eax%10
323 # if (eax == 0) break
334 bf/copy-to-edi 0/imm32
336 8b/-> *(ebp+8) 0/r32/eax
337 # if (n < 0) negate n, increment edi
339 3d/compare-eax-with 0/imm32
340 7d/jump-if->= break/disp8
341 f7 3/subop/negate %eax
345 b9/copy-to-ecx 0xa/imm32
347 ba/copy-to-edx 0/imm32
348 f7 7/subop/idiv-edx-eax-by %ecx # eax = edx:eax/10; edx = edx:eax%10
350 3d/compare-eax-and 0/imm32
351 75/jump-if-!= loop/disp8
355 # . restore registers
364 test-decimal-size-of-zero:
369 (decimal-size 0) # => eax
370 (check-ints-equal %eax 1 "F - test-decimal-size-of-zero")
371 $test-decimal-size-of-zero:end:
377 test-decimal-size-single-digit:
382 (decimal-size 4) # => eax
383 (check-ints-equal %eax 1 "F - test-decimal-size-single-digit")
384 $test-decimal-size-single-digit:end:
390 test-decimal-size-multi-digit:
395 (decimal-size 0xa) # => eax
396 (check-ints-equal %eax 2 "F - test-decimal-size-multi-digit")
397 $test-decimal-size-multi-digit:end:
403 test-decimal-size-single-digit-negative:
408 (decimal-size -4) # => eax
409 (check-ints-equal %eax 2 "F - test-decimal-size-single-digit-negative")
410 $test-decimal-size-single-digit-negative:end:
416 test-decimal-size-multi-digit-negative:
421 (decimal-size -0xa) # => eax
422 (check-ints-equal %eax 3 "F - test-decimal-size-multi-digit-negative")
423 $test-decimal-size-multi-digit-negative:end:
429 _parse-array-of-decimal-ints: # ad: (addr allocation-descriptor), s: (addr array byte), out: (addr handle array int)
431 # end = &s->data[s->size]
435 # if (curr >= end) break
436 # curr = skip-chars-matching-in-slice(curr, end, ' ')
437 # if (curr >= end) break
438 # curr = skip-chars-not-matching-in-slice(curr, end, ' ')
440 # allocate-array(ad, size*4, out)
441 # var slice: slice = {s->data, 0}
442 # curr = lookup(out)->data
444 # if (slice->start >= end) break
445 # slice->start = skip-chars-matching-in-slice(slice->start, end, ' ')
446 # if (slice->start >= end) break
447 # slice->end = skip-chars-not-matching-in-slice(slice->start, end, ' ')
448 # *curr = parse-hex-int-from-slice(slice)
450 # slice->start = slice->end
464 8b/-> *(ebp+0xc) 6/r32/esi
465 # var curr/ecx: (addr byte) = s->data
466 8d/copy-address *(esi+4) 1/r32/ecx
467 # var end/edx: (addr byte) = &s->data[s->size]
471 01/add-to %edx 1/r32/ecx
472 # var size/ebx: int = 0
473 31/xor-with %ebx 3/r32/ebx
474 $_parse-array-of-decimal-ints:loop1:
475 # if (curr >= end) break
476 39/compare %ecx 2/r32/edx
477 73/jump-if-addr>= $_parse-array-of-decimal-ints:break1/disp8
478 # curr = skip-chars-matching-in-slice(curr, end, ' ')
479 (skip-chars-matching-in-slice %ecx %edx 0x20) # => eax
481 # if (curr >= end) break
482 39/compare %ecx 2/r32/edx
483 73/jump-if-addr>= $_parse-array-of-decimal-ints:break1/disp8
484 # curr = skip-chars-not-matching-in-slice(curr, end, ' ')
485 (skip-chars-not-matching-in-slice %ecx %edx 0x20) # => eax
488 81 0/subop/add %ebx 4/imm32
489 eb/jump $_parse-array-of-decimal-ints:loop1/disp8
490 $_parse-array-of-decimal-ints:break1:
491 (allocate-array *(ebp+8) %ebx *(ebp+0x10))
492 $_parse-array-of-decimal-ints:pass2:
493 # var slice/edi: slice = {s->data, 0}
495 8d/copy-address *(esi+4) 7/r32/edi
498 # curr = lookup(out)->data
499 8b/-> *(ebp+0x10) 0/r32/eax
500 (lookup *eax *(eax+4)) # => eax
501 8d/copy-address *(eax+4) 1/r32/ecx
502 $_parse-array-of-decimal-ints:loop2:
503 # if (slice->start >= end) break
504 39/compare *edi 2/r32/edx
505 73/jump-if-addr>= $_parse-array-of-decimal-ints:end/disp8
506 # slice->start = skip-chars-matching-in-slice(slice->start, end, ' ')
507 (skip-chars-matching-in-slice *edi %edx 0x20) # => eax
509 # if (slice->start >= end) break
510 39/compare *edi 2/r32/edx
511 73/jump-if-addr>= $_parse-array-of-decimal-ints:end/disp8
512 # slice->end = skip-chars-not-matching-in-slice(slice->start, end, ' ')
513 (skip-chars-not-matching-in-slice *edi %edx 0x20) # => eax
514 89/<- *(edi+4) 0/r32/eax
515 # *curr = parse-hex-int-from-slice(slice)
516 (parse-decimal-int-from-slice %edi)
519 81 0/subop/add %ecx 4/imm32
520 # slice->start = slice->end
521 8b/-> *(edi+4) 0/r32/eax
523 eb/jump $_parse-array-of-decimal-ints:loop2/disp8
524 $_parse-array-of-decimal-ints:end:
526 81 0/subop/add %esp 8/imm32
527 # . restore registers
539 test-parse-array-of-decimal-ints:
543 # var h/esi: (handle array int)
547 # var ecx: (array int) = [1, 2, 3]
551 68/push 0xc/imm32/size
554 (_parse-array-of-decimal-ints Heap "1 2 3" %esi)
555 (lookup *esi *(esi+4)) # => eax
556 (array-equal? %ecx %eax) # => eax
557 (check-ints-equal %eax 1 "F - test-parse-array-of-decimal-ints")
563 test-parse-array-of-decimal-ints-empty:
564 # - empty string = empty array
573 (_parse-array-of-decimal-ints Heap "" %esi)
574 (lookup *esi *(esi+4)) # => eax
575 (check-ints-equal *eax 0 "F - test-parse-array-of-decimal-ints-empty")
581 test-parse-array-of-decimal-ints-just-whitespace:
582 # - just whitespace = empty array
591 (_parse-array-of-decimal-ints Heap Space %esi)
592 (lookup *esi *(esi+4)) # => eax
593 (check-ints-equal *eax 0 "F - test-parse-array-of-decimal-ints-just-whitespace")
599 test-parse-array-of-decimal-ints-extra-whitespace:
607 # var ecx: (array int) = [1, 2, 3]
611 68/push 0xc/imm32/size
614 (_parse-array-of-decimal-ints Heap " 1 2 3 " %esi)
615 (lookup *esi *(esi+4)) # => eax
616 (array-equal? %ecx %eax) # => eax
617 (check-ints-equal %eax 1 "F - test-parse-array-of-decimal-ints-extra-whitespace")
623 parse-array-of-decimal-ints: # s: (addr array byte), out: (addr handle array int)
628 (_parse-array-of-decimal-ints Heap *(ebp+8) *(ebp+0xc))
629 $parse-array-of-decimal-ints:end: