1.1 --- a/micropython/code.py Tue Nov 01 01:16:09 2011 +0100
1.2 +++ b/micropython/code.py Thu Nov 17 01:49:57 2011 +0100
1.3 @@ -29,13 +29,18 @@
1.4
1.5 "Support for generating low-level code."
1.6
1.7 - def __init__(self, optimisations):
1.8 + def __init__(self, program):
1.9
1.10 - "Initialise the assembler with an optimiser and status attributes."
1.11 + """
1.12 + Initialise the assembler with a program, optimiser and status
1.13 + attributes.
1.14 + """
1.15 +
1.16 + self.program = program
1.17
1.18 # Optimisation.
1.19
1.20 - self.optimiser = Optimiser(self, optimisations)
1.21 + self.optimiser = Optimiser(self, program.optimisations)
1.22
1.23 # The current unit being translated.
1.24
1.25 @@ -425,4 +430,62 @@
1.26 except IndexError:
1.27 return None
1.28
1.29 + # Allocation-related methods.
1.30 +
1.31 + def make_instance(self, cls, n):
1.32 +
1.33 + """
1.34 + Request a new instance using the given class 'cls' and with 'n'
1.35 + attributes.
1.36 + """
1.37 +
1.38 + # Load the class in order to locate the instance template.
1.39 +
1.40 + self.new_op(LoadConst(cls))
1.41 +
1.42 + # NOTE: Instance headers are one location.
1.43 +
1.44 + self.new_op(MakeInstance(n + 1))
1.45 +
1.46 + def make_exception(self, name):
1.47 +
1.48 + "Make an exception of the given 'name' using 'node'."
1.49 +
1.50 + # NOTE: Reserving an attribute.
1.51 +
1.52 + self.make_instance(self.get_builtin_class(name), 1)
1.53 +
1.54 + # Name-related methods.
1.55 +
1.56 + def get_scope(self, name):
1.57 +
1.58 + "Return the scope for the given 'name'."
1.59 +
1.60 + attr, scope, from_name = self.unit._get_with_scope(name)
1.61 + return scope
1.62 +
1.63 + def load_builtin(self, name, node):
1.64 +
1.65 + "Generate an instruction loading 'name' for the given 'node'."
1.66 +
1.67 + self.new_op(LoadAddress(self.get_builtin(name)))
1.68 +
1.69 + def get_builtin_class(self, name):
1.70 +
1.71 + "Return the built-in class with the given 'name'."
1.72 +
1.73 + return self.get_builtin(name).get_value()
1.74 +
1.75 + def get_builtin(self, name):
1.76 +
1.77 + "Return the built-in module definition for the given 'name'."
1.78 +
1.79 + if self.builtins is not None:
1.80 + try:
1.81 + return self.builtins[name]
1.82 + except KeyError:
1.83 + raise TranslateError("No __builtins__ definition is available for name %r." % name)
1.84 + else:
1.85 + raise TranslateError("No __builtins__ module is available for name %r." % name)
1.86 +
1.87 # vim: tabstop=4 expandtab shiftwidth=4