paul@627 | 1 | A Systems Programming Language Target for Micropython
|
paul@627 | 2 | =====================================================
|
paul@627 | 3 |
|
paul@627 | 4 | Python-compatible syntax for processing using the compiler module.
|
paul@627 | 5 |
|
paul@627 | 6 | The principal focus is on specific machine code generation and not
|
paul@627 | 7 | analysis. Thus, only block generation, address reference generation,
|
paul@627 | 8 | temporary storage administration and other code generation tasks are to be
|
paul@627 | 9 | left to the systems programming language compiler.
|
paul@627 | 10 |
|
paul@670 | 11 | Special Functions
|
paul@670 | 12 | -----------------
|
paul@670 | 13 |
|
paul@670 | 14 | In syspython, the function invocation notation is reserved to specify
|
paul@670 | 15 | primitive operations such as attribute access and actual function invocations,
|
paul@670 | 16 | with the latter being expressed as follows:
|
paul@670 | 17 |
|
paul@670 | 18 | fn(y) # original Python
|
paul@670 | 19 | apply(fn, y) # syspython
|
paul@670 | 20 |
|
paul@670 | 21 | Thus, in syspython, whenever the invocation notation is used, the target of
|
paul@670 | 22 | the invocation is always a special function and not a general Python function
|
paul@670 | 23 | or method. Note that the apply function resembles the Python function of the
|
paul@670 | 24 | same name but is not actually that particular function.
|
paul@670 | 25 |
|
paul@679 | 26 | A family of special functions for invocations exists, addressing optimisation
|
paul@679 | 27 | situations identified by program analysis.
|
paul@679 | 28 |
|
paul@678 | 29 | Low-Level Code
|
paul@678 | 30 | --------------
|
paul@678 | 31 |
|
paul@678 | 32 | Most Python-level program code should be wrapped in special function
|
paul@678 | 33 | invocations, and as a result other syntax features might be used to express
|
paul@678 | 34 | low-level concepts. Low-level operations may also be expressed using other
|
paul@678 | 35 | special functions. For example:
|
paul@678 | 36 |
|
paul@678 | 37 | storelocal(element, loadobjtable(loadattr(obj, classcode), attrcode))
|
paul@678 | 38 |
|
paul@678 | 39 | Here, element holds the raw data provided by the table access involving a base
|
paul@678 | 40 | defined by the classcode of an object and an offset defined by the supplied
|
paul@678 | 41 | attrcode.
|
paul@678 | 42 |
|
paul@678 | 43 | Note that all low-level functions deal only with addresses and offsets, not
|
paul@678 | 44 | symbols. In the above example, loadattr combines the address of obj with the
|
paul@678 | 45 | symbol classcode whose actual value must be substituted by the compiler.
|
paul@678 | 46 | However, the loadobjtable function requires a genuine offset value for the
|
paul@678 | 47 | classcode (which is why loadattr is being used to obtain it), and a genuine
|
paul@678 | 48 | offset for the attrcode (which is provided directly).
|
paul@678 | 49 |
|
paul@636 | 50 | Program Data and Data Structure Definition
|
paul@636 | 51 | ------------------------------------------
|
paul@636 | 52 |
|
paul@627 | 53 | Given that micropython has already deduced object and parameter details,
|
paul@627 | 54 | such information must be communicated in the systems programming language
|
paul@627 | 55 | so that the compiler does not have to deduce it again.
|
paul@627 | 56 |
|
paul@627 | 57 | Explicit constant declaration shall be done at the start of the main
|
paul@627 | 58 | module:
|
paul@627 | 59 |
|
paul@670 | 60 | constants(...)
|
paul@627 | 61 |
|
paul@675 | 62 | Each module may feature keyword arguments, and a list of such names is
|
paul@675 | 63 | provided as follows:
|
paul@675 | 64 |
|
paul@675 | 65 | keywords(...)
|
paul@675 | 66 |
|
paul@627 | 67 | Explicit structure declaration is still performed using class statements,
|
paul@627 | 68 | but base classes are omitted and attributes are declared explicitly as
|
paul@627 | 69 | follows:
|
paul@627 | 70 |
|
paul@627 | 71 | class C:
|
paul@670 | 72 | instattrs(member...)
|
paul@670 | 73 | classattrs(member...)
|
paul@627 | 74 |
|
paul@627 | 75 | Other object table information, such as inherited class attributes and
|
paul@627 | 76 | class compatibility (to support isinstance) are also declared explicitly:
|
paul@627 | 77 |
|
paul@670 | 78 | inherited(superclass, member...)
|
paul@670 | 79 | descendants(class...)
|
paul@627 | 80 |
|
paul@627 | 81 | Other than function definitions, no other code statements shall appear in
|
paul@627 | 82 | class definitions; such statements will appear after classes have been
|
paul@638 | 83 | defined.
|
paul@638 | 84 |
|
paul@638 | 85 | For classes in the module namespace or within other classes, the __main__
|
paul@638 | 86 | function collects together all "loose" (module-level) statements; class
|
paul@638 | 87 | attribute assignments will occur in the __main__ function, and where a name
|
paul@638 | 88 | is associated with a function definition and another object, the function will
|
paul@638 | 89 | also be explicitly assigned in the __main__ function using its full name.
|
paul@638 | 90 |
|
paul@638 | 91 | For classes in function namespaces, the containing function could contain the
|
paul@638 | 92 | "loose" statements at the point at which the class appears. However, such
|
paul@638 | 93 | classes are not currently supported in micropython.
|
paul@637 | 94 |
|
paul@637 | 95 | Any class or function defined once in a namespace need not be assigned to that
|
paul@637 | 96 | namespace in the __main__ function, but where multiple definitions exist and
|
paul@637 | 97 | program logic determines which definition prevails, such definitions must be
|
paul@637 | 98 | assigned in the __main__ function.
|
paul@637 | 99 |
|
paul@637 | 100 | For example:
|
paul@637 | 101 |
|
paul@637 | 102 | class C:
|
paul@637 | 103 | def method(self, ...):
|
paul@637 | 104 | ...
|
paul@637 | 105 | if something:
|
paul@637 | 106 | method = something
|
paul@637 | 107 |
|
paul@637 | 108 | This is represented as follows:
|
paul@637 | 109 |
|
paul@637 | 110 | class C:
|
paul@637 | 111 | ...
|
paul@637 | 112 | def method(self, ...):
|
paul@637 | 113 | ...
|
paul@637 | 114 |
|
paul@637 | 115 | def __main__():
|
paul@670 | 116 | globalnames(...)
|
paul@637 | 117 | ...
|
paul@637 | 118 | if something:
|
paul@670 | 119 | storeattr(module.C, method, something)
|
paul@627 | 120 |
|
paul@636 | 121 | Imports
|
paul@636 | 122 | -------
|
paul@636 | 123 |
|
paul@627 | 124 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 125 | particular scope and are defined as follows:
|
paul@627 | 126 |
|
paul@627 | 127 | # import package
|
paul@627 | 128 | package.__main__()
|
paul@670 | 129 | storelocal(package, static(package))
|
paul@627 | 130 |
|
paul@627 | 131 | # import package.module
|
paul@627 | 132 | package.__main__()
|
paul@627 | 133 | package.module.__main__()
|
paul@670 | 134 | storelocal(package, static(package))
|
paul@627 | 135 |
|
paul@627 | 136 | # from package.module import cls
|
paul@627 | 137 | package.__main__()
|
paul@627 | 138 | package.module.__main__()
|
paul@670 | 139 | storelocal(cls, loadattribute(package.module, cls)) # see below
|
paul@627 | 140 |
|
paul@627 | 141 | Since import statements can appear in code that may be executed more than
|
paul@627 | 142 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 143 | function has already been called.
|
paul@627 | 144 |
|
paul@627 | 145 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 146 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 147 | use of modules.
|
paul@627 | 148 |
|
paul@636 | 149 | Name and Attribute Declarations
|
paul@636 | 150 | -------------------------------
|
paul@636 | 151 |
|
paul@629 | 152 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 153 | explicitly:
|
paul@627 | 154 |
|
paul@670 | 155 | localnames(...)
|
paul@627 | 156 |
|
paul@627 | 157 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 158 | globals refer to module globals:
|
paul@627 | 159 |
|
paul@670 | 160 | globalnames(...)
|
paul@627 | 161 |
|
paul@670 | 162 | At the module level, locals are effectively equivalent to module globals and
|
paul@670 | 163 | are declared as such.
|
paul@629 | 164 |
|
paul@629 | 165 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 166 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 167 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 168 |
|
paul@627 | 169 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 170 | For example:
|
paul@627 | 171 |
|
paul@627 | 172 | def f(a, b):
|
paul@670 | 173 | localnames(a, b, x, y)
|
paul@670 | 174 | globalnames(f, g)
|
paul@627 | 175 |
|
paul@670 | 176 | storelocal(x, 1)
|
paul@670 | 177 | storelocal(y, x)
|
paul@670 | 178 | storelocal(a, b)
|
paul@670 | 179 | storeattr(module, g, f)
|
paul@627 | 180 |
|
paul@636 | 181 | Names and Attributes
|
paul@636 | 182 | --------------------
|
paul@636 | 183 |
|
paul@670 | 184 | Bare names refer to locals or globals according to the localnames and
|
paul@670 | 185 | globalnames declarations, or to constants such as None, True, False and
|
paul@638 | 186 | NotImplemented. Storage of local or global names is done using explicit
|
paul@638 | 187 | functions as follows:
|
paul@638 | 188 |
|
paul@670 | 189 | storelocal(name, value)
|
paul@670 | 190 | storeattr(module, name, value) # see below
|
paul@638 | 191 |
|
paul@627 | 192 | No operator usage: all operators are converted to invocations, including
|
paul@637 | 193 | all attribute access except static references to modules or particular class
|
paul@637 | 194 | or function definitions using the following notation:
|
paul@637 | 195 |
|
paul@670 | 196 | static(package)
|
paul@670 | 197 | static(package.module)
|
paul@670 | 198 | static(package.module.cls)
|
paul@670 | 199 | static(package.module.cls.function)
|
paul@627 | 200 |
|
paul@637 | 201 | A shorthand dot notation could be employed:
|
paul@637 | 202 |
|
paul@637 | 203 | package.module
|
paul@637 | 204 | package.module.cls
|
paul@637 | 205 | package.module.cls.function
|
paul@637 | 206 |
|
paul@637 | 207 | Where multiple definitions of static objects occur, the dot notation cannot be
|
paul@637 | 208 | used, and the full name of such definitions must be quoted. For example:
|
paul@637 | 209 |
|
paul@670 | 210 | static("package.module.cls#1.function")
|
paul@627 | 211 |
|
paul@627 | 212 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 213 | kind of access operation being performed. For example:
|
paul@627 | 214 |
|
paul@676 | 215 | # Instance-related operations:
|
paul@676 | 216 |
|
paul@677 | 217 | loadattr(obj, attrname) # preserve retrieved context
|
paul@676 | 218 |
|
paul@676 | 219 | # Static attribute operations:
|
paul@675 | 220 |
|
paul@677 | 221 | loadaddress(parent, attrname) # preserve retrieved context
|
paul@676 | 222 | loadaddresscontext(parent, attrname, obj) # replace context with obj
|
paul@676 | 223 | loadaddresscontextcond(parent, attrname, obj) # run-time context decision
|
paul@676 | 224 |
|
paul@676 | 225 | # Unoptimised operations:
|
paul@675 | 226 |
|
paul@677 | 227 | loadattrindex(obj, attrname) # preserve retrieved context
|
paul@676 | 228 | loadattrindexcontextcond(obj, attrname) # run-time context decision
|
paul@676 | 229 |
|
paul@676 | 230 | # Instance-related operations:
|
paul@676 | 231 |
|
paul@677 | 232 | storeattr(obj, attrname, value) # preserve context for value
|
paul@627 | 233 |
|
paul@676 | 234 | # Static attribute operations:
|
paul@676 | 235 |
|
paul@677 | 236 | storeaddress(parent, attrname, value) # preserve context for value
|
paul@676 | 237 | storeaddresscontext(parent, attrname, value, obj) # replace context with obj
|
paul@676 | 238 |
|
paul@676 | 239 | # Unoptimised operations:
|
paul@676 | 240 |
|
paul@677 | 241 | storeattrindex(obj, attrname, value) # preserve context for value
|
paul@627 | 242 |
|
paul@675 | 243 | Recall that for loadattrindex family functions, the location of the attribute
|
paul@675 | 244 | is obtained from the object table and the nature of the attribute is
|
paul@675 | 245 | determined from the stored context value.
|
paul@675 | 246 |
|
paul@638 | 247 | Temporary variables could employ similar functions:
|
paul@638 | 248 |
|
paul@670 | 249 | loadtemp(0)
|
paul@670 | 250 | storetemp(0, value)
|
paul@638 | 251 |
|
paul@636 | 252 | Operators and Invocations
|
paul@636 | 253 | -------------------------
|
paul@636 | 254 |
|
paul@627 | 255 | Conventional operators use the operator functions.
|
paul@627 | 256 |
|
paul@627 | 257 | Special operators could also use the operator functions (where available)
|
paul@627 | 258 | but might as well be supported directly:
|
paul@627 | 259 |
|
paul@627 | 260 | __is__(a, b)
|
paul@670 | 261 | __is_not__(a, b)
|
paul@627 | 262 |
|
paul@627 | 263 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 264 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 265 |
|
paul@627 | 266 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 267 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 268 | __or__(...) # returns the first true value or the final value
|
paul@627 | 269 |
|
paul@627 | 270 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 271 |
|
paul@627 | 272 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 273 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 274 |
|
paul@636 | 275 | Advanced Control-Flow
|
paul@636 | 276 | ---------------------
|
paul@636 | 277 |
|
paul@627 | 278 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 279 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 280 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 281 | callable (described below).
|
paul@627 | 282 |
|
paul@627 | 283 | Special functions for low-level operations:
|
paul@627 | 284 |
|
paul@670 | 285 | check(obj, type)
|
paul@670 | 286 | jump(callable)
|
paul@627 | 287 |
|
paul@627 | 288 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 289 | parameters.
|
paul@627 | 290 |
|
paul@627 | 291 | def fn_checked(self, ...):
|
paul@670 | 292 | check(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@670 | 293 | jump(fn_unchecked) # preserves the frame and return address
|
paul@627 | 294 |
|
paul@627 | 295 | def fn_unchecked(self, ...):
|
paul@627 | 296 | ...
|
paul@636 | 297 |
|
paul@670 | 298 | The jump function might also be used for inlining appropriate functions.
|
paul@644 | 299 |
|
paul@636 | 300 | Exceptions must also be handled in the language.
|
paul@644 | 301 |
|
paul@644 | 302 | Object Type Detection
|
paul@644 | 303 | ---------------------
|
paul@644 | 304 |
|
paul@644 | 305 | Occasionally, the type of an object (instance of a particular class, class,
|
paul@644 | 306 | and so on) needs to be determined at run-time:
|
paul@644 | 307 |
|
paul@670 | 308 | isclass(obj)
|