git @ Cat's Eye Technologies SixtyPical / 07ef1e2
backpatch_constraint_labels can resolve ForwardReferences. Chris Pressey 3 years ago
2 changed file(s) with 20 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
2626
2727 class RoutineType(Type):
2828 """This memory location contains the code for a routine."""
29 def __init__(self, inputs=None, outputs=None, trashes=None):
29 def __init__(self, inputs, outputs, trashes):
3030 self.name = 'routine'
31 self.inputs = inputs or set()
32 self.outputs = outputs or set()
33 self.trashes = trashes or set()
31 self.inputs = inputs
32 self.outputs = outputs
33 self.trashes = trashes
3434
3535 def __repr__(self):
3636 return '%s(%r, inputs=%r, outputs=%r, trashes=%r)' % (
7373 def clear_statics(self):
7474 self.context.statics = {}
7575
76 # ---- symbol resolution
77
7678 def resolve_symbols(self, program):
77
78 def backpatch_constraint_labels(type_, resolver):
79 # This could stand to be better unified.
80
81 def backpatch_constraint_labels(type_):
7982 def resolve(w):
80 if not isinstance(w, str):
83 if not isinstance(w, ForwardReference):
8184 return w
82 return resolver(w)
85 return self.lookup(w.name)
8386 if isinstance(type_, TableType):
84 backpatch_constraint_labels(type_.of_type, resolver)
87 backpatch_constraint_labels(type_.of_type)
8588 elif isinstance(type_, VectorType):
86 backpatch_constraint_labels(type_.of_type, resolver)
89 backpatch_constraint_labels(type_.of_type)
8790 elif isinstance(type_, RoutineType):
8891 type_.inputs = set([resolve(w) for w in type_.inputs])
8992 type_.outputs = set([resolve(w) for w in type_.outputs])
9093 type_.trashes = set([resolve(w) for w in type_.trashes])
9194
9295 for defn in program.defns:
93 backpatch_constraint_labels(defn.location.type, lambda w: self.lookup(w))
96 backpatch_constraint_labels(defn.location.type)
9497 for routine in program.routines:
95 backpatch_constraint_labels(routine.location.type, lambda w: self.lookup(w))
98 backpatch_constraint_labels(routine.location.type)
9699
97100 def resolve_fwd_reference(obj, field):
98101 field_value = getattr(obj, field, None)
278281 outputs = set(self.labels())
279282 if self.scanner.consume('trashes'):
280283 trashes = set(self.labels())
281 return (inputs, outputs, trashes)
284 return (
285 set([ForwardReference(n) for n in inputs]),
286 set([ForwardReference(n) for n in outputs]),
287 set([ForwardReference(n) for n in trashes])
288 )
282289
283290 def legacy_routine(self):
284291 self.scanner.expect('routine')