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