* Minor tuning
[fw/sdcc] / doc / choices.txt
1 Some of the implementation choices
2 ----------------------------------
3
4 gbz80:
5
6 Load from direct space:
7   Alternatives:
8   1.  Via HL
9         ld hl,#dir
10         ld x,(hl)
11         inc hl
12         ld y,(hl)
13   2.  Via a
14         ld a,(dir)
15         ld x,a
16         ld a,(dir+1)
17         ld x,a
18   1 is bad when x or y involve HL (1b)
19                                         8       16      32
20      1 = 12 + n*(8+8) - 8               20      36      68
21      1b = n*(12+12+8)                   32      64      128
22      2 = n*(16+4)                       20      40      80
23   So choose 2.
24
25   Hmm.  (2) is too hard to support in the current model.
26
27 On stack word push
28    1.    lda  hl,x(sp)
29          ld   a,(hl+)
30          ld   h,(hl)
31          ld   l,a
32          push hl
33    2.    lda  hl,x(sp)
34          ld   e,(hl)
35          inc  hl
36          ld   d,(hl)
37    1 = d + 8 + 8 + 4
38    2 = d + 8 + 8 + 8
39
40 Structure member get:
41    Normally fetch pair
42    Then add pair and constant with result in hl
43
44    ld   l,c     ; 4
45    ld   h,b     ; 4
46    inc  hl ..   ; 6     = 8 + 6n
47 or
48    ld   l,c     ; 4
49    ld   h,b     ; 4
50    ld   a,#0x06 ; 7
51    add  a,c     ; 4
52    ld   l,a     ; 4
53    ld   a,#0x00 ; 7
54    adc  a,b     ; 4
55    ld   h,a     ; 4     = 38
56 alt: (only when result=hl and left, rigth = pair, const)
57    ld      hl,#const    ; 10
58    add     hl,pair      ; 11    = 21
59
60 So (1) is best for n <= 2, (2) is just bad, (3) is good n > 2
61
62 How about:
63     pair = pair + constant:
64 1:
65     ld  a,#0x08 ; 7
66     add a,c     ; 4
67     ld  c,a     ; 4
68     ld  a,#0x00 ; 7
69     adc a,b     ; 4
70     ld  b,a     ; 4     = 30
71 2:
72         ld      hl,#const       ; 10
73         add     hl,pair         ; 11
74         ld      c,l             ; 4
75         ld      b,h             ; 4     = 29
76 One cycle.  If I cache HL later it will throw away the advantage.  Choose 1.
77
78 PlusIncr on pairs:
79 1:
80          inc    pair            ; 6     = 6n
81 2:
82         ld      a,#0x04         ; 7
83         add     a,c             ; 4
84         ld      c,a             ; 4
85         ld      a,#0x00         ; 7
86         adc     a,b             ; 4
87         ld      b,a             ; 4     = 30
88 So n <= 5 (1) is better.