git @ Cat's Eye Technologies SixtyPical / 80c4648
Saving anything except `a` trashes `a`. Chris Pressey 3 years ago
2 changed file(s) with 57 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
776776 self.analyze_block(instr.block, context)
777777 # TODO assert no goto was encountered
778778 context.re_introduce(baton)
779
780 if location == REG_A:
781 pass
782 else:
783 context.set_touched(REG_A)
784 context.set_unmeaningful(REG_A)
19541954 | }
19551955 = ok
19561956
1957 Saving any location (other than `a`) will trash `a`.
1958
1959 | routine main
1960 | inputs a, x
1961 | outputs a, x
1962 | trashes z, n
1963 | {
1964 | ld a, 1
1965 | save x {
1966 | ld a, 2
1967 | }
1968 | }
1969 ? UnmeaningfulOutputError
1970
1971 Saving `a` does not trash anything.
1972
1973 | routine main
1974 | inputs a, x
1975 | outputs a, x
1976 | trashes z, n
1977 | {
1978 | ld x, 1
1979 | save a {
1980 | ld x, 2
1981 | }
1982 | ld x, 3
1983 | }
1984 = ok
1985
19571986 A defined value that has been saved can be trashed inside the block.
19581987 It will continue to be defined outside the block.
19591988
19601989 | routine main
1961 | inputs a
1962 | outputs a, x
1963 | trashes z, n
1990 | outputs x, y
1991 | trashes a, z, n
19641992 | {
19651993 | ld x, 0
19661994 | save x {
1967 | ld a, 0
1995 | ld y, 0
19681996 | trash x
19691997 | }
19701998 | }
19912019 routine; and can appear in a `save` exactly because a `save` preserves it.
19922020
19932021 | routine main
2022 | outputs y
2023 | trashes a, z, n
2024 | {
2025 | save x {
2026 | ld y, 0
2027 | ld x, 1
2028 | }
2029 | }
2030 = ok
2031
2032 Because saving anything except `a` trashes `a`, a common idiom is to save `a`
2033 first in a nested series of `save`s.
2034
2035 | routine main
19942036 | inputs a
19952037 | outputs a
19962038 | trashes z, n
19972039 | {
1998 | save x {
1999 | ld a, 0
2000 | ld x, 1
2040 | save a {
2041 | save x {
2042 | ld a, 0
2043 | ld x, 1
2044 | }
20012045 | }
20022046 | }
20032047 = ok