git @ Cat's Eye Technologies SixtyPical / 6dbce20
`goto` is no longer restricted to appearing in tail position. Chris Pressey 3 years ago
4 changed file(s) with 55 addition(s) and 19 deletion(s). Raw diff Collapse all Expand all
44 ----
55
66 * Syntactically, `goto` may only appear at the end of a block.
7 It need no longer be the final instruction in a routine,
8 as long as the type context is consistent at every exit.
79 * Fixed pathological memory use in the lexical scanner - should
810 be much less inefficient now when parsing large source files.
911
6868 If a block ends in a `call` can that be converted to end in a `goto`? Why not? I think it can,
6969 if the block is in tail position. The constraints should iron out the same both ways.
7070
71 And - once we have this - why do we need `goto` to be in tail position, strictly?
7271 As long as the routine has consistent type context every place it exits, that should be fine.
7372
7473 ### "Include" directives
431431 opcode = instr.opcode
432432 dest = instr.dest
433433 src = instr.src
434
435 if context.encountered_gotos():
436 raise IllegalJumpError(instr, instr)
437434
438435 if opcode == 'ld':
439436 if isinstance(src, IndexedRef):
734731 if instr.src is not None:
735732 context.assert_meaningful(instr.src)
736733
734 if context.encountered_gotos():
735 raise IllegalJumpError(instr, instr)
736
737737 def analyze_for(self, instr, context):
738738 context.assert_meaningful(instr.dest)
739739 context.assert_writeable(instr.dest)
22792279 | }
22802280 ? TypeMismatchError
22812281
2282 A `goto` cannot appear within a `save` block, even if it is otherwise in tail position.
2282 A `goto` cannot appear within a `save` block.
22832283
22842284 | define other routine
22852285 | trashes a, z, n
23242324 | }
23252325 = ok
23262326
2327 A `goto` cannot appear within a `with interrupts` block, even if it is
2328 otherwise in tail position.
2327 A `goto` cannot appear within a `with interrupts` block.
23292328
23302329 | vector routine
23312330 | inputs x
29722971 | }
29732972 ? UnmeaningfulOutputError: x
29742973
2975 `goto`, if present, must be in tail position (the final instruction in a routine.)
2974 For now at least, you cannot have a `goto` inside a loop.
2975
2976 | define bar routine trashes x, z, n {
2977 | ld x, 200
2978 | }
2979 |
2980 | define main routine trashes x, z, n {
2981 | ld x, 0
2982 | repeat {
2983 | inc x
2984 | goto bar
2985 | } until z
2986 | }
2987 ? IllegalJumpError
2988
2989 `goto`, as a matter of syntax, can only appear at the end
2990 of a block; but it need not be the final instruction in a
2991 routine. It is only important that the type context at every
2992 `goto` is compatible with the type context at the end of
2993 the routine.
29762994
29772995 | define bar routine trashes x, z, n {
29782996 | ld x, 200
29943012 | ld x, 1
29953013 | goto bar
29963014 | }
2997 | ld x, 0
2998 | }
2999 ? IllegalJumpError
3015 | }
3016 = ok
30003017
30013018 | define bar routine trashes x, z, n {
30023019 | ld x, 200
30083025 | ld x, 1
30093026 | goto bar
30103027 | }
3028 | goto bar
30113029 | }
30123030 = ok
30133031
30233041 | }
30243042 | ld x, 0
30253043 | }
3026 ? IllegalJumpError
3044 = ok
30273045
30283046 | define bar routine trashes x, z, n {
30293047 | ld x, 200
30563074 | }
30573075 = ok
30583076
3059 For the purposes of `goto`, the end of a loop is never tail position.
3060
30613077 | define bar routine trashes x, z, n {
30623078 | ld x, 200
30633079 | }
30643080 |
30653081 | define main routine trashes x, z, n {
30663082 | ld x, 0
3067 | repeat {
3068 | inc x
3083 | if z {
3084 | ld x, 1
30693085 | goto bar
3070 | } until z
3071 | }
3072 ? IllegalJumpError
3086 | } else {
3087 | ld x, 0
3088 | }
3089 | ld x, 0
3090 | }
3091 = ok
3092
3093 | define bar routine trashes x, z, n {
3094 | ld x, 200
3095 | }
3096 |
3097 | define main routine trashes x, z, n {
3098 | ld x, 0
3099 | if z {
3100 | ld x, 1
3101 | goto bar
3102 | } else {
3103 | ld x, 0
3104 | }
3105 | goto bar
3106 | }
3107 = ok
30733108
30743109 Can't `goto` a routine that outputs or trashes more than the current routine.
30753110