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@759 | 26 | apply(fn, ...) # general invocation
|
paul@759 | 27 |
|
paul@679 | 28 | A family of special functions for invocations exists, addressing optimisation
|
paul@747 | 29 | situations identified by program analysis:
|
paul@747 | 30 |
|
paul@759 | 31 | applyclass(cls, ...) # direct invocation of an instantiator
|
paul@759 | 32 | applyfunction(fn, ...) # function-specific invocation
|
paul@759 | 33 | applystaticmethod(fn, obj, ...) # specific invocation of a method via a class
|
paul@759 | 34 | applymethod(fn, obj, ...) # specific invocation of a method via self
|
paul@759 | 35 |
|
paul@759 | 36 | Where dynamic functions are to be invoked, the context providing the defaults
|
paul@759 | 37 | needs to be supplied to the function or method, but this can be done using the
|
paul@759 | 38 | above special functions as follows:
|
paul@759 | 39 |
|
paul@759 | 40 | applyclass(cls, __context__, ...)
|
paul@759 | 41 | applyfunction(fn, __context__, ...)
|
paul@759 | 42 | applystaticmethod(fn, __context__, obj, ...)
|
paul@759 | 43 | applymethod(fn, __context__, obj, ...)
|
paul@759 | 44 |
|
paul@759 | 45 | Where optimisation possibilities cannot be identified in advance, the apply
|
paul@759 | 46 | function must deal with the following aspects of invocation:
|
paul@759 | 47 |
|
paul@759 | 48 | * Whether a context argument is required
|
paul@759 | 49 | * Whether a dynamic function is being invoked, thus requiring a context for
|
paul@759 | 50 | access to defaults
|
paul@759 | 51 | * Whether an appropriate number of arguments have been provided
|
paul@679 | 52 |
|
paul@678 | 53 | Low-Level Code
|
paul@678 | 54 | --------------
|
paul@678 | 55 |
|
paul@678 | 56 | Most Python-level program code should be wrapped in special function
|
paul@678 | 57 | invocations, and as a result other syntax features might be used to express
|
paul@678 | 58 | low-level concepts. Low-level operations may also be expressed using other
|
paul@678 | 59 | special functions. For example:
|
paul@678 | 60 |
|
paul@678 | 61 | storelocal(element, loadobjtable(loadattr(obj, classcode), attrcode))
|
paul@678 | 62 |
|
paul@678 | 63 | Here, element holds the raw data provided by the table access involving a base
|
paul@678 | 64 | defined by the classcode of an object and an offset defined by the supplied
|
paul@678 | 65 | attrcode.
|
paul@678 | 66 |
|
paul@678 | 67 | Note that all low-level functions deal only with addresses and offsets, not
|
paul@678 | 68 | symbols. In the above example, loadattr combines the address of obj with the
|
paul@678 | 69 | symbol classcode whose actual value must be substituted by the compiler.
|
paul@678 | 70 | However, the loadobjtable function requires a genuine offset value for the
|
paul@678 | 71 | classcode (which is why loadattr is being used to obtain it), and a genuine
|
paul@678 | 72 | offset for the attrcode (which is provided directly).
|
paul@678 | 73 |
|
paul@636 | 74 | Program Data and Data Structure Definition
|
paul@636 | 75 | ------------------------------------------
|
paul@636 | 76 |
|
paul@627 | 77 | Given that micropython has already deduced object and parameter details,
|
paul@627 | 78 | such information must be communicated in the systems programming language
|
paul@627 | 79 | so that the compiler does not have to deduce it again.
|
paul@627 | 80 |
|
paul@627 | 81 | Explicit constant declaration shall be done at the start of the main
|
paul@627 | 82 | module:
|
paul@627 | 83 |
|
paul@670 | 84 | constants(...)
|
paul@627 | 85 |
|
paul@675 | 86 | Each module may feature keyword arguments, and a list of such names is
|
paul@675 | 87 | provided as follows:
|
paul@675 | 88 |
|
paul@675 | 89 | keywords(...)
|
paul@675 | 90 |
|
paul@627 | 91 | Explicit structure declaration is still performed using class statements,
|
paul@627 | 92 | but base classes are omitted and attributes are declared explicitly as
|
paul@627 | 93 | follows:
|
paul@627 | 94 |
|
paul@627 | 95 | class C:
|
paul@670 | 96 | instattrs(member...)
|
paul@670 | 97 | classattrs(member...)
|
paul@627 | 98 |
|
paul@627 | 99 | Other object table information, such as inherited class attributes and
|
paul@627 | 100 | class compatibility (to support isinstance) are also declared explicitly:
|
paul@627 | 101 |
|
paul@670 | 102 | inherited(superclass, member...)
|
paul@670 | 103 | descendants(class...)
|
paul@627 | 104 |
|
paul@627 | 105 | Other than function definitions, no other code statements shall appear in
|
paul@627 | 106 | class definitions; such statements will appear after classes have been
|
paul@638 | 107 | defined.
|
paul@638 | 108 |
|
paul@638 | 109 | For classes in the module namespace or within other classes, the __main__
|
paul@638 | 110 | function collects together all "loose" (module-level) statements; class
|
paul@638 | 111 | attribute assignments will occur in the __main__ function, and where a name
|
paul@638 | 112 | is associated with a function definition and another object, the function will
|
paul@638 | 113 | also be explicitly assigned in the __main__ function using its full name.
|
paul@638 | 114 |
|
paul@638 | 115 | For classes in function namespaces, the containing function could contain the
|
paul@638 | 116 | "loose" statements at the point at which the class appears. However, such
|
paul@638 | 117 | classes are not currently supported in micropython.
|
paul@637 | 118 |
|
paul@637 | 119 | Any class or function defined once in a namespace need not be assigned to that
|
paul@637 | 120 | namespace in the __main__ function, but where multiple definitions exist and
|
paul@637 | 121 | program logic determines which definition prevails, such definitions must be
|
paul@637 | 122 | assigned in the __main__ function.
|
paul@637 | 123 |
|
paul@637 | 124 | For example:
|
paul@637 | 125 |
|
paul@637 | 126 | class C:
|
paul@637 | 127 | def method(self, ...):
|
paul@637 | 128 | ...
|
paul@637 | 129 | if something:
|
paul@637 | 130 | method = something
|
paul@637 | 131 |
|
paul@637 | 132 | This is represented as follows:
|
paul@637 | 133 |
|
paul@637 | 134 | class C:
|
paul@637 | 135 | ...
|
paul@637 | 136 | def method(self, ...):
|
paul@637 | 137 | ...
|
paul@637 | 138 |
|
paul@637 | 139 | def __main__():
|
paul@670 | 140 | globalnames(...)
|
paul@637 | 141 | ...
|
paul@637 | 142 | if something:
|
paul@670 | 143 | storeattr(module.C, method, something)
|
paul@627 | 144 |
|
paul@756 | 145 | Local class or function definitions are also handled in a similar fashion to
|
paul@756 | 146 | those at the module level, although there is no explicit __main__ function
|
paul@756 | 147 | within each function.
|
paul@756 | 148 |
|
paul@756 | 149 | For example:
|
paul@756 | 150 |
|
paul@756 | 151 | def outer(x):
|
paul@756 | 152 | if something:
|
paul@756 | 153 | def inner(...):
|
paul@756 | 154 | ...
|
paul@756 | 155 | else:
|
paul@756 | 156 | def inner(...):
|
paul@756 | 157 | ...
|
paul@756 | 158 | return inner
|
paul@756 | 159 |
|
paul@756 | 160 | This is represented as follows:
|
paul@756 | 161 |
|
paul@756 | 162 | def outer(x):
|
paul@756 | 163 | def inner(...):
|
paul@756 | 164 | ...
|
paul@756 | 165 | def inner(...):
|
paul@756 | 166 | ...
|
paul@756 | 167 |
|
paul@756 | 168 | if something:
|
paul@756 | 169 | storelocal(inner, static(outer.inner))
|
paul@756 | 170 | else:
|
paul@756 | 171 | storelocal(inner, static("outer.inner#2"))
|
paul@756 | 172 | return inner
|
paul@756 | 173 |
|
paul@756 | 174 | Where functions are dynamic - that is they have additional state associated
|
paul@756 | 175 | with them, such as defaults (or potentially closures if supported) that are
|
paul@756 | 176 | not static (such as constant values) - suitable objects must be created using
|
paul@756 | 177 | references to such functions.
|
paul@756 | 178 |
|
paul@756 | 179 | For example:
|
paul@756 | 180 |
|
paul@756 | 181 | def outer(x):
|
paul@756 | 182 | def inner(y, z=x):
|
paul@756 | 183 | ...
|
paul@756 | 184 | return inner
|
paul@756 | 185 |
|
paul@756 | 186 | This is represented as follows:
|
paul@756 | 187 |
|
paul@756 | 188 | def outer(x):
|
paul@756 | 189 | def inner(__context__, y, z=x):
|
paul@756 | 190 | localnames(__context__, y, z)
|
paul@756 | 191 | ...
|
paul@756 | 192 | storelocal(inner, makedynamic(static(outer.inner), x))
|
paul@756 | 193 | return inner
|
paul@756 | 194 |
|
paul@756 | 195 | The special makedynamic invocation creates an object referring to the function
|
paul@756 | 196 | and incorporating any specified defaults as attributes of that object. The
|
paul@756 | 197 | function itself uses a special __context__ parameter that acts somewhat like
|
paul@756 | 198 | the self parameter in methods: when invoked, the __context__ provides access
|
paul@756 | 199 | to any default information that needs to be transferred to the local
|
paul@756 | 200 | namespace.
|
paul@756 | 201 |
|
paul@636 | 202 | Imports
|
paul@636 | 203 | -------
|
paul@636 | 204 |
|
paul@627 | 205 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 206 | particular scope and are defined as follows:
|
paul@627 | 207 |
|
paul@627 | 208 | # import package
|
paul@627 | 209 | package.__main__()
|
paul@670 | 210 | storelocal(package, static(package))
|
paul@627 | 211 |
|
paul@627 | 212 | # import package.module
|
paul@627 | 213 | package.__main__()
|
paul@627 | 214 | package.module.__main__()
|
paul@670 | 215 | storelocal(package, static(package))
|
paul@627 | 216 |
|
paul@627 | 217 | # from package.module import cls
|
paul@627 | 218 | package.__main__()
|
paul@627 | 219 | package.module.__main__()
|
paul@670 | 220 | storelocal(cls, loadattribute(package.module, cls)) # see below
|
paul@627 | 221 |
|
paul@627 | 222 | Since import statements can appear in code that may be executed more than
|
paul@627 | 223 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 224 | function has already been called.
|
paul@627 | 225 |
|
paul@627 | 226 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 227 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 228 | use of modules.
|
paul@627 | 229 |
|
paul@636 | 230 | Name and Attribute Declarations
|
paul@636 | 231 | -------------------------------
|
paul@636 | 232 |
|
paul@629 | 233 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 234 | explicitly:
|
paul@627 | 235 |
|
paul@670 | 236 | localnames(...)
|
paul@627 | 237 |
|
paul@627 | 238 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 239 | globals refer to module globals:
|
paul@627 | 240 |
|
paul@670 | 241 | globalnames(...)
|
paul@627 | 242 |
|
paul@670 | 243 | At the module level, locals are effectively equivalent to module globals and
|
paul@670 | 244 | are declared as such.
|
paul@629 | 245 |
|
paul@629 | 246 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 247 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 248 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 249 |
|
paul@627 | 250 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 251 | For example:
|
paul@627 | 252 |
|
paul@627 | 253 | def f(a, b):
|
paul@670 | 254 | localnames(a, b, x, y)
|
paul@670 | 255 | globalnames(f, g)
|
paul@627 | 256 |
|
paul@670 | 257 | storelocal(x, 1)
|
paul@670 | 258 | storelocal(y, x)
|
paul@670 | 259 | storelocal(a, b)
|
paul@670 | 260 | storeattr(module, g, f)
|
paul@627 | 261 |
|
paul@734 | 262 | Assignments
|
paul@734 | 263 | -----------
|
paul@734 | 264 |
|
paul@734 | 265 | Since assignments can rebind names used in the value expression, the evaluated
|
paul@734 | 266 | expression must be captured and referenced when setting the targets. This is
|
paul@734 | 267 | done using the special $expr variable, and so the swap assignment...
|
paul@734 | 268 |
|
paul@734 | 269 | a, b = b, a
|
paul@734 | 270 |
|
paul@734 | 271 | ...would be written (more or less) as...
|
paul@734 | 272 |
|
paul@734 | 273 | $expr = (b, a)
|
paul@734 | 274 | storelocal(a, apply(operator.getitem, $expr, 0))
|
paul@734 | 275 | storelocal(b, apply(operator.getitem, $expr, 1))
|
paul@734 | 276 |
|
paul@636 | 277 | Names and Attributes
|
paul@636 | 278 | --------------------
|
paul@636 | 279 |
|
paul@670 | 280 | Bare names refer to locals or globals according to the localnames and
|
paul@670 | 281 | globalnames declarations, or to constants such as None, True, False and
|
paul@638 | 282 | NotImplemented. Storage of local or global names is done using explicit
|
paul@638 | 283 | functions as follows:
|
paul@638 | 284 |
|
paul@670 | 285 | storelocal(name, value)
|
paul@670 | 286 | storeattr(module, name, value) # see below
|
paul@638 | 287 |
|
paul@627 | 288 | No operator usage: all operators are converted to invocations, including
|
paul@637 | 289 | all attribute access except static references to modules or particular class
|
paul@637 | 290 | or function definitions using the following notation:
|
paul@637 | 291 |
|
paul@670 | 292 | static(package)
|
paul@670 | 293 | static(package.module)
|
paul@670 | 294 | static(package.module.cls)
|
paul@670 | 295 | static(package.module.cls.function)
|
paul@627 | 296 |
|
paul@637 | 297 | A shorthand dot notation could be employed:
|
paul@637 | 298 |
|
paul@637 | 299 | package.module
|
paul@637 | 300 | package.module.cls
|
paul@637 | 301 | package.module.cls.function
|
paul@637 | 302 |
|
paul@637 | 303 | Where multiple definitions of static objects occur, the dot notation cannot be
|
paul@637 | 304 | used, and the full name of such definitions must be quoted. For example:
|
paul@637 | 305 |
|
paul@670 | 306 | static("package.module.cls#1.function")
|
paul@627 | 307 |
|
paul@627 | 308 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 309 | kind of access operation being performed. For example:
|
paul@627 | 310 |
|
paul@676 | 311 | # Instance-related operations:
|
paul@676 | 312 |
|
paul@677 | 313 | loadattr(obj, attrname) # preserve retrieved context
|
paul@676 | 314 |
|
paul@711 | 315 | # Constant attribute operations:
|
paul@711 | 316 |
|
paul@711 | 317 | static(value) # see above
|
paul@711 | 318 | loadconstant(value, obj) # replace context with obj
|
paul@711 | 319 |
|
paul@676 | 320 | # Static attribute operations:
|
paul@675 | 321 |
|
paul@677 | 322 | loadaddress(parent, attrname) # preserve retrieved context
|
paul@676 | 323 | loadaddresscontext(parent, attrname, obj) # replace context with obj
|
paul@676 | 324 | loadaddresscontextcond(parent, attrname, obj) # run-time context decision
|
paul@676 | 325 |
|
paul@676 | 326 | # Unoptimised operations:
|
paul@675 | 327 |
|
paul@677 | 328 | loadattrindex(obj, attrname) # preserve retrieved context
|
paul@676 | 329 | loadattrindexcontextcond(obj, attrname) # run-time context decision
|
paul@676 | 330 |
|
paul@676 | 331 | # Instance-related operations:
|
paul@676 | 332 |
|
paul@677 | 333 | storeattr(obj, attrname, value) # preserve context for value
|
paul@627 | 334 |
|
paul@676 | 335 | # Static attribute operations:
|
paul@676 | 336 |
|
paul@677 | 337 | storeaddress(parent, attrname, value) # preserve context for value
|
paul@676 | 338 | storeaddresscontext(parent, attrname, value, obj) # replace context with obj
|
paul@676 | 339 |
|
paul@676 | 340 | # Unoptimised operations:
|
paul@676 | 341 |
|
paul@677 | 342 | storeattrindex(obj, attrname, value) # preserve context for value
|
paul@627 | 343 |
|
paul@675 | 344 | Recall that for loadattrindex family functions, the location of the attribute
|
paul@675 | 345 | is obtained from the object table and the nature of the attribute is
|
paul@675 | 346 | determined from the stored context value.
|
paul@675 | 347 |
|
paul@638 | 348 | Temporary variables could employ similar functions:
|
paul@638 | 349 |
|
paul@670 | 350 | loadtemp(0)
|
paul@670 | 351 | storetemp(0, value)
|
paul@638 | 352 |
|
paul@636 | 353 | Operators and Invocations
|
paul@636 | 354 | -------------------------
|
paul@636 | 355 |
|
paul@627 | 356 | Conventional operators use the operator functions.
|
paul@627 | 357 |
|
paul@627 | 358 | Special operators could also use the operator functions (where available)
|
paul@627 | 359 | but might as well be supported directly:
|
paul@627 | 360 |
|
paul@627 | 361 | __is__(a, b)
|
paul@670 | 362 | __is_not__(a, b)
|
paul@627 | 363 |
|
paul@627 | 364 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 365 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 366 |
|
paul@627 | 367 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 368 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 369 | __or__(...) # returns the first true value or the final value
|
paul@627 | 370 |
|
paul@627 | 371 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 372 |
|
paul@627 | 373 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 374 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 375 |
|
paul@636 | 376 | Advanced Control-Flow
|
paul@636 | 377 | ---------------------
|
paul@636 | 378 |
|
paul@627 | 379 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 380 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 381 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 382 | callable (described below).
|
paul@627 | 383 |
|
paul@627 | 384 | Special functions for low-level operations:
|
paul@627 | 385 |
|
paul@670 | 386 | check(obj, type)
|
paul@670 | 387 | jump(callable)
|
paul@627 | 388 |
|
paul@627 | 389 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 390 | parameters.
|
paul@627 | 391 |
|
paul@627 | 392 | def fn_checked(self, ...):
|
paul@670 | 393 | check(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@670 | 394 | jump(fn_unchecked) # preserves the frame and return address
|
paul@627 | 395 |
|
paul@627 | 396 | def fn_unchecked(self, ...):
|
paul@627 | 397 | ...
|
paul@636 | 398 |
|
paul@670 | 399 | The jump function might also be used for inlining appropriate functions.
|
paul@644 | 400 |
|
paul@636 | 401 | Exceptions must also be handled in the language.
|
paul@644 | 402 |
|
paul@644 | 403 | Object Type Detection
|
paul@644 | 404 | ---------------------
|
paul@644 | 405 |
|
paul@644 | 406 | Occasionally, the type of an object (instance of a particular class, class,
|
paul@644 | 407 | and so on) needs to be determined at run-time:
|
paul@644 | 408 |
|
paul@670 | 409 | isclass(obj)
|