1.1 --- a/micropython/__init__.py Sun Jan 13 02:14:46 2008 +0100
1.2 +++ b/micropython/__init__.py Sun Jan 13 02:50:21 2008 +0100
1.3 @@ -90,9 +90,14 @@
1.4 module.location = pos
1.5 trans = micropython.ast.Translation(module, objtable, paramtable)
1.6
1.7 + # Add header details.
1.8 +
1.9 + image.append(module)
1.10 + pos += 1
1.11 +
1.12 # Append constants to the image.
1.13
1.14 - for const in module.constants:
1.15 + for const in module.constants():
1.16 const.location = pos
1.17 image.append(const)
1.18 pos += 1
1.19 @@ -113,7 +118,12 @@
1.20
1.21 # Position the class in the image.
1.22
1.23 - obj.code_location = obj.location = pos
1.24 + obj.location = pos
1.25 +
1.26 + # Add header details.
1.27 +
1.28 + image.append(obj)
1.29 + pos += 1
1.30
1.31 # Append class attributes to the image.
1.32
1.33 @@ -131,6 +141,7 @@
1.34 # NOTE: methods and only other attribute-related code being
1.35 # NOTE: generated here.
1.36
1.37 + obj.code_location = obj.location = pos
1.38 code = trans.get_code(obj)
1.39 image += code
1.40 pos += len(code)
1.41 @@ -141,10 +152,16 @@
1.42
1.43 # Position the function in the image.
1.44
1.45 - obj.code_location = obj.location = pos
1.46 + obj.location = pos
1.47 +
1.48 + # Add header details.
1.49 +
1.50 + image.append(obj)
1.51 + pos += 1
1.52
1.53 # Append the function code to the image.
1.54
1.55 + obj.code_location = obj.location = pos
1.56 code = trans.get_code(obj)
1.57 image += code
1.58 pos += len(code)
2.1 --- a/micropython/ast.py Sun Jan 13 02:14:46 2008 +0100
2.2 +++ b/micropython/ast.py Sun Jan 13 02:50:21 2008 +0100
2.3 @@ -64,7 +64,6 @@
2.4 self.objtable = objtable
2.5 self.paramtable = paramtable
2.6 self.unit = None
2.7 - self.constants = {}
2.8
2.9 # Wiring within the code.
2.10
2.11 @@ -120,9 +119,7 @@
2.12 label.location = len(self.code) + self.unit.code_location
2.13
2.14 def new_op(self, op):
2.15 - print self.module.name, len(self.code) + self.unit.code_location,
2.16 self.code.append(op)
2.17 - print op
2.18
2.19 # Visitor methods.
2.20
2.21 @@ -217,8 +214,14 @@
2.22
2.23 # Evaluate the arguments.
2.24
2.25 + positional = 1
2.26 +
2.27 for i, arg in enumerate(node.args):
2.28 if isinstance(arg, compiler.ast.Keyword):
2.29 + if positional:
2.30 + self.new_op(ReserveFrame(len(node.args) - i))
2.31 + positional = 0
2.32 +
2.33 self.dispatch(arg.expr)
2.34
2.35 # Combine the target details with the name to get the location.
2.36 @@ -256,7 +259,9 @@
2.37 # Provide short-circuiting.
2.38 """
2.39
2.40 - def visitConst(self, node): pass
2.41 + def visitConst(self, node):
2.42 + const = self.module.constant_values[node.value]
2.43 + self.new_op(LoadConst(const))
2.44
2.45 def visitContinue(self, node): pass
2.46
2.47 @@ -288,6 +293,9 @@
2.48 if self.unit is self.module:
2.49 self.new_op(LoadConst(node.function))
2.50 self._visitName(node, (StoreName, StoreAttr))
2.51 +
2.52 + # Visiting of the code occurs when get_code is invoked on this node.
2.53 +
2.54 else:
2.55 self.dispatch(node.code)
2.56 self.new_op(Return())
3.1 --- a/micropython/inspect.py Sun Jan 13 02:14:46 2008 +0100
3.2 +++ b/micropython/inspect.py Sun Jan 13 02:50:21 2008 +0100
3.3 @@ -391,6 +391,11 @@
3.4
3.5 self.all_objects = []
3.6
3.7 + # Constant records.
3.8 +
3.9 + self.constant_values = {}
3.10 + self.constant_list = None # cache for constants
3.11 +
3.12 # Image generation details.
3.13
3.14 self.location = None
3.15 @@ -428,6 +433,15 @@
3.16
3.17 return self.modattr
3.18
3.19 + def constants(self):
3.20 +
3.21 + "Return a list of constants."
3.22 +
3.23 + if self.constant_list is None:
3.24 + self.constant_list = list(self.constant_values.values())
3.25 +
3.26 + return self.constant_list
3.27 +
3.28 # Program visitors.
3.29
3.30 class InspectedModule(ASTVisitor, Module):
3.31 @@ -454,7 +468,6 @@
3.32 self.in_init = 0
3.33 self.namespaces = []
3.34 self.module = None
3.35 - self.constants = []
3.36
3.37 def parse(self, filename):
3.38
3.39 @@ -604,8 +617,7 @@
3.40
3.41 def visitConst(self, node):
3.42 const = Const(node.value)
3.43 - if not const in self.constants:
3.44 - self.constants.append(const)
3.45 + self.constant_values[node.value] = const
3.46
3.47 visitContinue = NOP
3.48
4.1 --- a/micropython/rsvp.py Sun Jan 13 02:14:46 2008 +0100
4.2 +++ b/micropython/rsvp.py Sun Jan 13 02:50:21 2008 +0100
4.3 @@ -48,6 +48,7 @@
4.4 # Access to invocation frames in preparation.
4.5
4.6 class MakeFrame(Instruction): pass
4.7 +class ReserveFrame(Instruction): pass
4.8 class DropFrame(Instruction): pass
4.9 class StoreFrame(Instruction): pass
4.10
5.1 --- a/micropython/table.py Sun Jan 13 02:14:46 2008 +0100
5.2 +++ b/micropython/table.py Sun Jan 13 02:50:21 2008 +0100
5.3 @@ -138,6 +138,7 @@
5.4 def __init__(self):
5.5 self.attributes = set()
5.6 self.table = {}
5.7 + self.objnames = []
5.8 self.names = []
5.9 self.displaced_list = None
5.10
5.11 @@ -152,6 +153,13 @@
5.12 if self.displaced_list is not None:
5.13 self.displaced_list.add(objname, self.matrix_row(attributes))
5.14
5.15 + def object_names(self):
5.16 +
5.17 + "Return the object names used in the table."
5.18 +
5.19 + self.objnames = self.objnames or list(self.table.keys())
5.20 + return self.objnames
5.21 +
5.22 def attribute_names(self):
5.23
5.24 "Return the attribute names used in the table."
5.25 @@ -159,6 +167,12 @@
5.26 self.names = self.names or list(self.attributes)
5.27 return self.names
5.28
5.29 + def get_code(self, name):
5.30 +
5.31 + "Return the code of the given 'name'."
5.32 +
5.33 + return self.object_names().index(name)
5.34 +
5.35 def get_index(self, name):
5.36
5.37 "Return the index of the given 'name'."
6.1 --- a/test.py Sun Jan 13 02:14:46 2008 +0100
6.2 +++ b/test.py Sun Jan 13 02:50:21 2008 +0100
6.3 @@ -3,6 +3,10 @@
6.4 import micropython
6.5 import sys
6.6
6.7 +def show(importer):
6.8 + for i, x in enumerate(importer.get_image()):
6.9 + print i, x
6.10 +
6.11 i = micropython.Importer(sys.path)
6.12 if len(sys.argv) < 2:
6.13 m = i.load("micropython")