git @ Cat's Eye Technologies SixtyPical / master tests / SixtyPical Fallthru.md
master

Tree @master (Download .tar.gz)

SixtyPical Fallthru.md @masterview rendered · raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
SixtyPical Fallthru
===================

<!--
Copyright (c) 2014-2024, Chris Pressey, Cat's Eye Technologies.
This file is distributed under a 2-clause BSD license.  See LICENSES/ dir.
SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-SixtyPical
-->

This is a test suite, written in [Falderal][] format, for SixtyPical's
ability to detect which routines make tail calls to other routines,
and thus can be re-arranged to simply "fall through" to them.

The theory is as follows.

SixtyPical supports a `goto`, but it can only appear in tail position.
If a routine r1 ends with a unique `goto` to a fixed routine r2 it is said
to *potentially fall through* to r2.

A *unique* `goto` means that there are not multiple different `goto`s in
tail position (which can happen if, for example, an `if` is the last thing
in a routine, and each branch of that `if` ends with a different `goto`.)

A *fixed* routine means, a routine which is known at compile time, not a
`goto` through a vector.

Consider the set R of all available routines in the program.

Every routine either potentially falls through to a single other routine
or it does not potentially fall through to any routine.

More formally, we can say

> fall : R → R ∪ {nil}, fall(r) ≠ r

where `nil` is an atom that represents no routine.

Now consider an operation chain() vaguely similar to a transitive closure
on fall().  Starting with r, we construct a list of r, fall(r),
fall(fall(r)), ... with the following restrictions:

-   we stop when we reach `nil` (because fall(`nil`) is not defined)
-   we stop when we see an element that is not in R.
-   we stop when we see an element that we have already added to the
    list (this is to prevent infinite lists due to cycles.)

With these definitions, our algorithm is something like this.

Treat R as a mutable set and start with an empty list of lists L.  Then,

-   For all r ∈ R, find all chain(r).
-   Pick a longest such chain.  Call it C.
-   Append C to L.
-   Remove all elements occurring in C, from R.
-   Repeat until R is empty.

When time comes to generate code, generate it in the order given by L.
In addition, each sublist in L represents a number of routines to
generate; all except the final routine in such a sublist need not have
any jump instruction generated for its final `goto`.

The tests in this document test against the list L.

Note that this optimization is a feature of the SixtyPical's reference
compiler, not the language.  So an implementation is not required
to pass these tests to be considered an implementation of SixtyPical.

[Falderal]:     http://catseye.tc/node/Falderal

    -> Tests for functionality "Dump fallthru info for SixtyPical program"

A single routine, obviously, falls through to nothing and has nothing fall
through to it.

    | define main routine
    | {
    | }
    = [
    =     [
    =         "main"
    =     ]
    = ]

If `main` does a `goto foo`, then it can fall through to `foo`.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define main routine trashes a, z, n
    | {
    |     goto foo
    | }
    = [
    =     [
    =         "main",
    =         "foo"
    =     ]
    = ]

More than one routine can fall through to a routine.  We pick one
of them to fall through, when selecting the order of routines.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto foo
    | }
    | 
    | define main routine trashes a, z, n
    | {
    |     goto foo
    | }
    = [
    =     [
    =         "main",
    =         "foo"
    =     ],
    =     [
    =         "bar"
    =     ]
    = ]

Because `main` is always serialized first (so that the entry
point of the entire program appears at the beginning of the code),
nothing ever falls through to `main`.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto main
    | }
    | 
    | define main routine trashes a, z, n
    | {
    |     ld a, 1
    | }
    = [
    =     [
    =         "main"
    =     ],
    =     [
    =         "foo"
    =     ]
    = ]

There is nothing stopping two routines from tail-calling each
other, but we will only be able to make one of them, at most,
fall through to the other.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto bar
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto foo
    | }
    | 
    | define main routine trashes a, z, n
    | {
    | }
    = [
    =     [
    =         "main"
    =     ],
    =     [
    =         "foo",
    =         "bar"
    =     ]
    = ]

If a routine does two tail calls (which is possible because they
can be in different branches of an `if`) it cannot fall through to another
routine.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define main routine inputs z trashes a, z, n
    | {
    |     if z {
    |         goto foo
    |     } else {
    |         goto bar
    |     }
    | }
    = [
    =     [
    =         "main"
    =     ],
    =     [
    =         "foo"
    =     ],
    =     [
    =         "bar"
    =     ]
    = ]

If, however, they are the same goto, one can be optimized away.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    |     if z {
    |         ld a, 1
    |         goto bar
    |     } else {
    |         ld a, 2
    |         goto bar
    |     }
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 255
    | }
    | 
    | define main routine trashes a, z, n
    | {
    | }
    = [
    =     [
    =         "main"
    =     ],
    =     [
    =         "foo",
    =         "bar"
    =     ]
    = ]

Similarly, a tail call to a vector can't be turned into a fallthru,
because we don't necessarily know what actual routine the vector contains.

    | vector routine trashes a, z, n
    |   vec
    | 
    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define main routine outputs vec trashes a, z, n
    | {
    |     copy bar, vec
    |     goto vec
    | }
    = [
    =     [
    =         "main"
    =     ],
    =     [
    =         "foo"
    =     ],
    =     [
    =         "bar"
    =     ]
    = ]

Our algorithm might not be strictly optimal, but it does a good job.

    | define r1 routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto r2
    | }
    | 
    | define r2 routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto r3
    | }
    | 
    | define r3 routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto r4
    | }
    | 
    | define r4 routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define r5 routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto r6
    | }
    | 
    | define r6 routine trashes a, z, n
    | {
    |     ld a, 0
    |     goto r3
    | }
    | 
    | define main routine trashes a, z, n
    | {
    |     goto r1
    | }
    = [
    =     [
    =         "main",
    =         "r1",
    =         "r2",
    =         "r3",
    =         "r4"
    =     ],
    =     [
    =         "r5",
    =         "r6"
    =     ]
    = ]

    -> Tests for functionality "Compile SixtyPical program with fallthru optimization"

Basic test for actually applying this optimization when compiling SixtyPical programs.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 255
    |     goto foo
    | }
    | 
    | define main routine trashes a, z, n
    | {
    |     goto foo
    | }
    = $080D   LDA #$00
    = $080F   RTS
    = $0810   LDA #$FF
    = $0812   JMP $080D

It can optimize out one of the `goto`s if they are the same.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    |     if z {
    |         ld a, 1
    |         goto bar
    |     } else {
    |         ld a, 2
    |         goto bar
    |     }
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 255
    | }
    | 
    | define main routine trashes a, z, n
    | {
    | }
    = $080D   RTS
    = $080E   LDA #$00
    = $0810   BNE $0817
    = $0812   LDA #$01
    = $0814   JMP $0819
    = $0817   LDA #$02
    = $0819   LDA #$FF
    = $081B   RTS

It cannot optimize out the `goto`s if they are different.

    | define foo routine trashes a, z, n
    | {
    |     ld a, 0
    |     if z {
    |         ld a, 1
    |         goto bar
    |     } else {
    |         ld a, 2
    |         goto main
    |     }
    | }
    | 
    | define bar routine trashes a, z, n
    | {
    |     ld a, 255
    | }
    | 
    | define main routine trashes a, z, n
    | {
    | }
    = $080D   RTS
    = $080E   LDA #$00
    = $0810   BNE $0817
    = $0812   LDA #$01
    = $0814   JMP $081C
    = $0817   LDA #$02
    = $0819   JMP $080D
    = $081C   LDA #$FF
    = $081E   RTS