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@636 | 127 | Imports
|
paul@636 | 128 | -------
|
paul@636 | 129 |
|
paul@627 | 130 | Imports act as invocations of module code and name assignments within a
|
paul@627 | 131 | particular scope and are defined as follows:
|
paul@627 | 132 |
|
paul@627 | 133 | # import package
|
paul@627 | 134 | package.__main__()
|
paul@670 | 135 | storelocal(package, static(package))
|
paul@627 | 136 |
|
paul@627 | 137 | # import package.module
|
paul@627 | 138 | package.__main__()
|
paul@627 | 139 | package.module.__main__()
|
paul@670 | 140 | storelocal(package, static(package))
|
paul@627 | 141 |
|
paul@627 | 142 | # from package.module import cls
|
paul@627 | 143 | package.__main__()
|
paul@627 | 144 | package.module.__main__()
|
paul@670 | 145 | storelocal(cls, loadattribute(package.module, cls)) # see below
|
paul@627 | 146 |
|
paul@627 | 147 | Since import statements can appear in code that may be executed more than
|
paul@627 | 148 | once, __main__ functions should test and set a flag indicating whether the
|
paul@627 | 149 | function has already been called.
|
paul@627 | 150 |
|
paul@627 | 151 | Python would arguably be more sensible as a language if imports were
|
paul@627 | 152 | processed separately, but this would then rule out logic controlling the
|
paul@627 | 153 | use of modules.
|
paul@627 | 154 |
|
paul@636 | 155 | Name and Attribute Declarations
|
paul@636 | 156 | -------------------------------
|
paul@636 | 157 |
|
paul@629 | 158 | Assignments and name usage involve locals and globals but usage is declared
|
paul@629 | 159 | explicitly:
|
paul@627 | 160 |
|
paul@670 | 161 | localnames(...)
|
paul@627 | 162 |
|
paul@627 | 163 | At the function level, locals are genuine local name definitions whereas
|
paul@627 | 164 | globals refer to module globals:
|
paul@627 | 165 |
|
paul@670 | 166 | globalnames(...)
|
paul@627 | 167 |
|
paul@670 | 168 | At the module level, locals are effectively equivalent to module globals and
|
paul@670 | 169 | are declared as such.
|
paul@629 | 170 |
|
paul@629 | 171 | Each module's __main__ function will declare any referenced module globals as
|
paul@629 | 172 | globals. Note that the __main__ function is not a genuine attribute of any
|
paul@629 | 173 | module but an internal construct used to initialise modules appropriately.
|
paul@627 | 174 |
|
paul@627 | 175 | Such declarations must appear first in a program unit (module, function).
|
paul@627 | 176 | For example:
|
paul@627 | 177 |
|
paul@627 | 178 | def f(a, b):
|
paul@670 | 179 | localnames(a, b, x, y)
|
paul@670 | 180 | globalnames(f, g)
|
paul@627 | 181 |
|
paul@670 | 182 | storelocal(x, 1)
|
paul@670 | 183 | storelocal(y, x)
|
paul@670 | 184 | storelocal(a, b)
|
paul@670 | 185 | storeattr(module, g, f)
|
paul@627 | 186 |
|
paul@734 | 187 | Assignments
|
paul@734 | 188 | -----------
|
paul@734 | 189 |
|
paul@734 | 190 | Since assignments can rebind names used in the value expression, the evaluated
|
paul@734 | 191 | expression must be captured and referenced when setting the targets. This is
|
paul@734 | 192 | done using the special $expr variable, and so the swap assignment...
|
paul@734 | 193 |
|
paul@734 | 194 | a, b = b, a
|
paul@734 | 195 |
|
paul@734 | 196 | ...would be written (more or less) as...
|
paul@734 | 197 |
|
paul@734 | 198 | $expr = (b, a)
|
paul@734 | 199 | storelocal(a, apply(operator.getitem, $expr, 0))
|
paul@734 | 200 | storelocal(b, apply(operator.getitem, $expr, 1))
|
paul@734 | 201 |
|
paul@636 | 202 | Names and Attributes
|
paul@636 | 203 | --------------------
|
paul@636 | 204 |
|
paul@670 | 205 | Bare names refer to locals or globals according to the localnames and
|
paul@670 | 206 | globalnames declarations, or to constants such as None, True, False and
|
paul@638 | 207 | NotImplemented. Storage of local or global names is done using explicit
|
paul@638 | 208 | functions as follows:
|
paul@638 | 209 |
|
paul@670 | 210 | storelocal(name, value)
|
paul@670 | 211 | storeattr(module, name, value) # see below
|
paul@638 | 212 |
|
paul@627 | 213 | No operator usage: all operators are converted to invocations, including
|
paul@637 | 214 | all attribute access except static references to modules or particular class
|
paul@637 | 215 | or function definitions using the following notation:
|
paul@637 | 216 |
|
paul@670 | 217 | static(package)
|
paul@670 | 218 | static(package.module)
|
paul@670 | 219 | static(package.module.cls)
|
paul@670 | 220 | static(package.module.cls.function)
|
paul@627 | 221 |
|
paul@637 | 222 | A shorthand dot notation could be employed:
|
paul@637 | 223 |
|
paul@637 | 224 | package.module
|
paul@637 | 225 | package.module.cls
|
paul@637 | 226 | package.module.cls.function
|
paul@637 | 227 |
|
paul@637 | 228 | Where multiple definitions of static objects occur, the dot notation cannot be
|
paul@637 | 229 | used, and the full name of such definitions must be quoted. For example:
|
paul@637 | 230 |
|
paul@670 | 231 | static("package.module.cls#1.function")
|
paul@627 | 232 |
|
paul@627 | 233 | In general, attribute access must use an explicit function indicating the
|
paul@627 | 234 | kind of access operation being performed. For example:
|
paul@627 | 235 |
|
paul@676 | 236 | # Instance-related operations:
|
paul@676 | 237 |
|
paul@677 | 238 | loadattr(obj, attrname) # preserve retrieved context
|
paul@676 | 239 |
|
paul@711 | 240 | # Constant attribute operations:
|
paul@711 | 241 |
|
paul@711 | 242 | static(value) # see above
|
paul@711 | 243 | loadconstant(value, obj) # replace context with obj
|
paul@711 | 244 |
|
paul@676 | 245 | # Static attribute operations:
|
paul@675 | 246 |
|
paul@677 | 247 | loadaddress(parent, attrname) # preserve retrieved context
|
paul@676 | 248 | loadaddresscontext(parent, attrname, obj) # replace context with obj
|
paul@676 | 249 | loadaddresscontextcond(parent, attrname, obj) # run-time context decision
|
paul@676 | 250 |
|
paul@676 | 251 | # Unoptimised operations:
|
paul@675 | 252 |
|
paul@677 | 253 | loadattrindex(obj, attrname) # preserve retrieved context
|
paul@676 | 254 | loadattrindexcontextcond(obj, attrname) # run-time context decision
|
paul@676 | 255 |
|
paul@676 | 256 | # Instance-related operations:
|
paul@676 | 257 |
|
paul@677 | 258 | storeattr(obj, attrname, value) # preserve context for value
|
paul@627 | 259 |
|
paul@676 | 260 | # Static attribute operations:
|
paul@676 | 261 |
|
paul@677 | 262 | storeaddress(parent, attrname, value) # preserve context for value
|
paul@676 | 263 | storeaddresscontext(parent, attrname, value, obj) # replace context with obj
|
paul@676 | 264 |
|
paul@676 | 265 | # Unoptimised operations:
|
paul@676 | 266 |
|
paul@677 | 267 | storeattrindex(obj, attrname, value) # preserve context for value
|
paul@627 | 268 |
|
paul@675 | 269 | Recall that for loadattrindex family functions, the location of the attribute
|
paul@675 | 270 | is obtained from the object table and the nature of the attribute is
|
paul@675 | 271 | determined from the stored context value.
|
paul@675 | 272 |
|
paul@638 | 273 | Temporary variables could employ similar functions:
|
paul@638 | 274 |
|
paul@670 | 275 | loadtemp(0)
|
paul@670 | 276 | storetemp(0, value)
|
paul@638 | 277 |
|
paul@636 | 278 | Operators and Invocations
|
paul@636 | 279 | -------------------------
|
paul@636 | 280 |
|
paul@627 | 281 | Conventional operators use the operator functions.
|
paul@627 | 282 |
|
paul@627 | 283 | Special operators could also use the operator functions (where available)
|
paul@627 | 284 | but might as well be supported directly:
|
paul@627 | 285 |
|
paul@627 | 286 | __is__(a, b)
|
paul@670 | 287 | __is_not__(a, b)
|
paul@627 | 288 |
|
paul@627 | 289 | Logical operators involving short-circuit evaluation could be represented
|
paul@627 | 290 | as function calls, but the evaluation semantics would be preserved:
|
paul@627 | 291 |
|
paul@627 | 292 | __and__(...) # returns the first non-true value or the final value
|
paul@627 | 293 | __not__(obj) # returns the inverse of the boolean interpretation of obj
|
paul@627 | 294 | __or__(...) # returns the first true value or the final value
|
paul@627 | 295 |
|
paul@627 | 296 | Comparisons could be rephrased in a verbose fashion:
|
paul@627 | 297 |
|
paul@627 | 298 | a < b < c becomes lt(a, b) and lt(b, c)
|
paul@627 | 299 | or __and__(lt(a, b), lt(b, c))
|
paul@627 | 300 |
|
paul@636 | 301 | Advanced Control-Flow
|
paul@636 | 302 | ---------------------
|
paul@636 | 303 |
|
paul@627 | 304 | Any statements requiring control-flow definition in terms of blocks must
|
paul@627 | 305 | be handled in the language as the notions of labels and blocks are not
|
paul@627 | 306 | introduced earlier apart from the special case of jumping to another
|
paul@627 | 307 | callable (described below).
|
paul@627 | 308 |
|
paul@627 | 309 | Special functions for low-level operations:
|
paul@627 | 310 |
|
paul@670 | 311 | check(obj, type)
|
paul@670 | 312 | jump(callable)
|
paul@627 | 313 |
|
paul@627 | 314 | Function/subroutine definition with entry points for checked and unchecked
|
paul@627 | 315 | parameters.
|
paul@627 | 316 |
|
paul@627 | 317 | def fn_checked(self, ...):
|
paul@670 | 318 | check(self, Type) # raises a TypeError if not isinstance(self, Type)
|
paul@670 | 319 | jump(fn_unchecked) # preserves the frame and return address
|
paul@627 | 320 |
|
paul@627 | 321 | def fn_unchecked(self, ...):
|
paul@627 | 322 | ...
|
paul@636 | 323 |
|
paul@670 | 324 | The jump function might also be used for inlining appropriate functions.
|
paul@644 | 325 |
|
paul@636 | 326 | Exceptions must also be handled in the language.
|
paul@644 | 327 |
|
paul@644 | 328 | Object Type Detection
|
paul@644 | 329 | ---------------------
|
paul@644 | 330 |
|
paul@644 | 331 | Occasionally, the type of an object (instance of a particular class, class,
|
paul@644 | 332 | and so on) needs to be determined at run-time:
|
paul@644 | 333 |
|
paul@670 | 334 | isclass(obj)
|