1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/micropython/native.py Thu Nov 17 01:49:57 2011 +0100
1.3 @@ -0,0 +1,156 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Native library generation.
1.8 +
1.9 +Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from micropython.code import Assembler
1.26 +from micropython.rsvp import *
1.27 +
1.28 +class NativeLibrary(Assembler):
1.29 +
1.30 + "A native library generator providing common routines."
1.31 +
1.32 + def __init__(self, program):
1.33 +
1.34 + """
1.35 + Initialise a native library for the given 'program'.
1.36 + """
1.37 +
1.38 + Assembler.__init__(self, program)
1.39 +
1.40 + self.importer = self.program.get_importer()
1.41 + self.builtins = None
1.42 +
1.43 + self.unknown_target_block = None
1.44 + self.unknown_target_testable_self_block = None
1.45 + self.known_target_testable_self_block = None
1.46 +
1.47 + # Reset the assembler.
1.48 +
1.49 + self.reset()
1.50 +
1.51 + def get_native_code(self):
1.52 + self.builtins = self.importer.modules.get("__builtins__")
1.53 +
1.54 + if self.unknown_target_block is not None:
1.55 + self._generateCallFuncUnknownTarget()
1.56 +
1.57 + if self.unknown_target_testable_self_block is not None:
1.58 + self._generateCallFuncUnknownTargetTestableSelf()
1.59 +
1.60 + if self.known_target_testable_self_block is not None:
1.61 + self._generateCallFuncKnownTargetTestableSelf()
1.62 +
1.63 + return self.blocks
1.64 +
1.65 + def _commonCallFunc(self, adjust_block, continue_block):
1.66 + self.new_op(CheckContext(working="source", target="status"))
1.67 + self.new_op(JumpIfFalse(adjust_block, working="status"))
1.68 +
1.69 + # Skip adjustment and tests if the context is not a class.
1.70 + # Classes themselves employ a placeholder context so that
1.71 + # instantiators can be callable with a context which will be
1.72 + # overwritten in the frame.
1.73 +
1.74 + # Here, the source value should still refer to the context.
1.75 +
1.76 + self.new_op(CheckClass(working="source", target="status"))
1.77 + self.new_op(JumpIfFalse(continue_block, working="status"))
1.78 +
1.79 + def _commonTestContext(self, success_block):
1.80 + self.new_op(CheckInstance(source="source", target="status"))
1.81 + self.new_op(JumpIfTrue(success_block, working="status"))
1.82 +
1.83 + # Where the context is inappropriate, drop the incomplete frame and
1.84 + # raise an exception.
1.85 +
1.86 + self.new_op(DropFrame())
1.87 +
1.88 + self.make_exception("TypeError")
1.89 + self.set_target("exception")
1.90 + self.new_op(RaiseException())
1.91 +
1.92 + def _commonAdjustFrame(self, adjust_block, continue_block):
1.93 + self.set_block(adjust_block)
1.94 + self.new_op(AdjustFrame(1))
1.95 + self.set_block(continue_block)
1.96 +
1.97 + def getCallFuncUnknownTarget(self):
1.98 + if self.unknown_target_block is None:
1.99 + self.unknown_target_block = self.new_block()
1.100 +
1.101 + return self.unknown_target_block
1.102 +
1.103 + def _generateCallFuncUnknownTarget(self):
1.104 +
1.105 + """
1.106 + Some preliminary tests where the target of an invocation is not known.
1.107 + Requires the context in the working value register.
1.108 + """
1.109 +
1.110 + adjust_block = self.new_block()
1.111 + continue_block = self.new_block()
1.112 +
1.113 + self.set_block(self.unknown_target_block)
1.114 + self._commonCallFunc(adjust_block, continue_block)
1.115 + self._commonAdjustFrame(adjust_block, continue_block)
1.116 + self.new_op(Return())
1.117 +
1.118 + def getCallFuncUnknownTargetTestableSelf(self):
1.119 + if self.unknown_target_testable_self_block is None:
1.120 + self.unknown_target_testable_self_block = self.new_block()
1.121 +
1.122 + return self.unknown_target_testable_self_block
1.123 +
1.124 + def _generateCallFuncUnknownTargetTestableSelf(self):
1.125 +
1.126 + """
1.127 + Test any explicit first argument against the context.
1.128 + """
1.129 +
1.130 + adjust_block = self.new_block()
1.131 + continue_block = self.new_block()
1.132 +
1.133 + self.set_block(self.unknown_target_testable_self_block)
1.134 + self._commonCallFunc(adjust_block, continue_block)
1.135 + self._commonTestContext(adjust_block)
1.136 + self._commonAdjustFrame(adjust_block, continue_block)
1.137 + self.new_op(Return())
1.138 +
1.139 + def getCallFuncKnownTargetTestableSelf(self):
1.140 + if self.known_target_testable_self_block is None:
1.141 + self.known_target_testable_self_block = self.new_block()
1.142 +
1.143 + return self.known_target_testable_self_block
1.144 +
1.145 + def _generateCallFuncKnownTargetTestableSelf(self):
1.146 +
1.147 + """
1.148 + Test any explicit first argument against the context.
1.149 + """
1.150 +
1.151 + adjust_block = self.new_block()
1.152 + continue_block = self.new_block()
1.153 +
1.154 + self.set_block(self.known_target_testable_self_block)
1.155 + self._commonTestContext(continue_block)
1.156 + self._commonAdjustFrame(adjust_block, continue_block)
1.157 + self.new_op(Return())
1.158 +
1.159 +# vim: tabstop=4 expandtab shiftwidth=4