git @ Cat's Eye Technologies SixtyPical / 6192a6a
Add specific error for uninit outputs. Sketch `if` analysis. Chris Pressey 6 years ago
2 changed file(s) with 23 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
1414
1515
1616 class UninitializedAccessError(StaticAnalysisError):
17 pass
18
19
20 class UninitializedOutputError(StaticAnalysisError):
1721 pass
1822
1923
4347 self._store.setdefault(ref.name, UNINITIALIZED)
4448 self._writeables.add(ref.name)
4549
46 def assertInitialized(self, *refs):
50 def assertInitialized(self, *refs, **kwargs):
51 exception_class = kwargs.get('exception_class', UninitializedAccessError)
4752 for ref in refs:
4853 if isinstance(ref, ConstantRef):
4954 pass
5055 elif isinstance(ref, LocationRef):
5156 if self.get(ref) != INITIALIZED:
52 raise UninitializedAccessError(ref.name)
57 raise exception_class(ref.name)
5358 else:
5459 raise ValueError(ref)
5560
9398 context = Context(routine.inputs, routine.outputs, routine.trashes)
9499 analyze_block(routine.block, context, routines)
95100 for ref in routine.outputs:
96 context.assertInitialized(ref)
101 context.assertInitialized(ref, exception_class=UninitializedOutputError)
97102
98103
99104 def analyze_block(block, context, routines):
153158 context.assertWriteable(ref)
154159 context.setUninitialized(ref)
155160 elif opcode == 'if':
156 pass
161 context1 = context.clone()
162 context2 = context.clone()
163 analyze_block(instr.block1, context1, routines)
164 analyze_block(instr.block2, context2, routines)
165 reconcile_contexts(context1, context2, output=context)
157166 else:
158167 raise NotImplementedError
0 Sixtypical Analysis
0 SixtyPical Analysis
11 ===================
22
3 This is a test suite, written in [Falderal][] format, for the Sixtypical
3 This is a test suite, written in [Falderal][] format, for the SixtyPical
44 static analysis rules.
55
66 [Falderal]: http://catseye.tc/node/Falderal
77
8 -> Functionality "Analyze Sixtypical program" is implemented by
8 -> Functionality "Analyze SixtyPical program" is implemented by
99 -> shell command "bin/sixtypical --analyze %(test-body-file)"
1010
11 -> Tests for functionality "Analyze Sixtypical program"
11 -> Tests for functionality "Analyze SixtyPical program"
1212
1313 ### Rudiments ###
1414
4141 | {
4242 | ld x, 0
4343 | }
44 ? UninitializedAccessError: a
44 ? UninitializedOutputError: a
4545
4646 | routine main
4747 | inputs a
317317 | ld x, 0
318318 | call foo
319319 | }
320 ? UninitializedAccessError: lives
320 ? UninitializedOutputError: lives
321321
322322 ...unless you write to it yourself afterwards.
323323
401401
402402 | routine foo
403403 | inputs a
404 | outputs a
405 | trashes z, n, c
404 | outputs x
405 | trashes a, z, n, c
406406 | {
407407 | cmp a, 42
408408 | if z {
409 | ld a, 7
409 | ld x, 7
410410 | } else {
411 | ld a, 23
411 | ld x, 23
412412 | }
413413 | }
414414 = ok