Static Typescript
Total Page:16
File Type:pdf, Size:1020Kb
1 Static TypeScript 56 2 57 3 An Implementation of a Static Compiler for the TypeScript Language 58 4 59 5 60 6 Thomas Ball Peli de Halleux Michał Moskal 61 7 Microsoft Research Microsoft Research Microsoft Research 62 8 Redmond, WA, United States Redmond, WA, United States Redmond, WA, United States 63 9 [email protected] [email protected] [email protected] 64 10 Abstract 65 11 66 12 While the programming of microcontroller-based embed- 67 13 dable devices typically is the realm of the C language, such 68 14 devices are now finding their way into the classroom forCS 69 15 education, even at the level of middle school. As a result, the 70 16 use of scripting languages (such as JavaScript and Python) 71 17 for microcontrollers is on the rise. 72 18 We present Static TypeScript (STS), a subset of TypeScript (a) (b) 73 19 (itself, a gradually typed superset of JavaScript), and its com- 74 20 piler/linker toolchain, which is implemented fully in Type- Figure 1. Two Cortex-M0 microcontroller-based educational 75 21 Script and runs in the web browser. STS is designed to be use- devices: (a) the BBC micro:bit has a Nordic nRF51822 MCU 76 22 ful in practice (especially in education), while being amenable with 16 kB RAM and 256 kB flash; (b) Adafruit’s Circuit Play- 77 23 to static compilation targeting small devices. A user’s STS ground Express (https://adafruit.com/products/3333) has an 78 24 program is compiled to machine code in the browser and Atmel SAMD21 MCU with 32 kB RAM and 256 kB flash. 79 25 linked against a precompiled C++ runtime, producing an ex- 80 26 ecutable that is more efficient than the prevalent embedded 81 interpreter approach, extending battery life and making it 27 girls, increases confidence in both students and teachers, and 82 possible to run on devices with as little as 16 kB of RAM 28 makes lessons more fun [2, 16]. 83 (such as the BBC micro:bit). 29 To keep costs low for schools, these devices typically em- 84 This paper is primarily a description of the STS system 30 ploy 32 bit ARM Cortex-M microcontrollers (MCUs) with 85 and the technical challenges of implementing embedded 31 16-256kB of RAM and are programmed using an external 86 programming platforms in the classroom. 32 computer (usually a laptop or desktop). Programming such 87 33 Keywords JavaScript, TypeScript, compiler, interpreter, mi- devices in a classroom presents a number of technical chal- 88 34 crocontrollers, virtual machine lenges: 89 35 90 (1) the selection/design of an age-appropriate program- 36 91 ming language and environment; 37 1 Introduction 92 (2) classroom computers running outdated operating sys- 38 Recently, physical computing has been making headway in 93 tems, having intermittent and slow internet connec- 39 the classroom, engaging children to build simple interactive 94 tivity, and locked down by school IT administrators, 40 embedded systems. For example, Figure 1(a) shows the BBC 95 which makes native app installation difficult; 41 micro:bit [1], a small programmable Arduino-inspired com- 96 (3) the transfer of the student’s program from the com- 42 puter with an integrated 5x5 LED display, several sensors 97 puter to the device, where it can run on battery power 43 and Bluetooth Low Energy (BLE) radio technology. The de- 98 (as many projects embed the device in an experiment 44 vice first rolled out in 2015 to all year 7 students (age 10to 99 or “make”). 45 11) in the UK and has since gone global, with four million 100 46 units distributed worldwide to date via the micro:bit Educa- With respect to these challenges, there are various embed- 101 47 tion Foundation (https://microbit.org). Figure 1(b) shows a ded interpreters for popular scripting languages, such as 102 48 different educational device featuring RGB LEDs: Adafruit’s JavaScript (JerryScript [8, 15], Duktape [22], Espruino [23], 103 49 Circuit Playground Express (CPX). mJS [20], and MuJS [19]) and Python (MicroPython [9] and 104 50 Research suggests that using such devices in computer its fork CircuitPython [12]). The interpreters run directly on 105 51 science education increases engagement, especially among the MCU, requiring just the transfer of program text from the 106 52 host computer, but forego the benefits of advanced optimiz- 107 53 MPLR 2019, Under submission, ing JIT compilers (such as V8) that require about two orders 108 54 . of magnitude more memory than is available on MCUs. 109 55 1 110 MPLR 2019, Under submission, Thomas Ball, Peli de Halleux, and Michał Moskal 111 • the STS compiler generates surprisingly efficient and 166 112 compact machine code, which unlocks a range of ap- 167 113 plication domains such as game programming for low- 168 114 resource devices such as those in Figure 2, all of which 169 115 were enabled by STS. 170 116 Deployment of STS user programs to embedded devices 171 117 does not require app or device driver installation, just access 172 118 to a web browser. Compiled programs appear as downloads, 173 119 which are then transferred manually by the user to the device, 174 120 which appears as a USB mass storage device, via file copy 175 121 (or directly through WebUSB, an upcoming standard for 176 122 connecting websites to physical devices). 177 123 The relatively simple compilation scheme for STS (pre- 178 124 sented in Section 3) leads to surprisingly good performance 179 125 on a collection of small JavaScript benchmarks, often com- 180 126 parable to advanced, state of the art JIT compilers like V8, 181 127 with orders of magnitude smaller memory requirements (see 182 128 Section 4). It is also at least an order of magnitude faster 183 129 Figure 2. Three microcontroller-based game handhelds with than the embedded interpreted approach. A novel aspect of 184 130 160x120 color screens. These boards use ARM’s Cortex-M4F evaluation is a comparison of different strategies for dealing 185 131 core: the ATSAMD51G19 (192kB RAM, running at 120Mhz) with field/method lookup spanning classes, interfaces, and 186 132 and STM32F401RE (96kB RAM, running at 84Mhz). dynamic maps. 187 133 188 134 1.2 MakeCode: Easy Embedded for Education 189 135 Unfortunately, such embedded interpreters are between 190 STS is the core language supported by the MakeCode Frame- 136 one and three orders of magnitude slower than V8 (see Sec- 191 work.1 MakeCode enables creation of custom programming 137 tion 4), affecting responsiveness and battery life. Even more 192 experiences for MCU-based devices. Each MakeCode experi- 138 importantly, due to the representation of objects in memory 193 ence (we often call them editors, though they also bundle a 139 as dynamic key-value mappings, the memory footprint can 194 simulator, APIs, tutorials, documentation, etc.) targets pro- 140 be several times that of an equivalent C program. This can 195 gramming of a specific device or device class via STS. [7] 141 severely limit the applications that can be deployed on low- 196 Most MakeCode editors are deployed primarily as web 142 memory devices such as the micro:bit (16 kB RAM) and CPX 197 apps, including a full-featured text editor for developing STS 143 (32 kB RAM). 198 programs based on Monaco (the editor component of Visual 144 199 1.1 Static TypeScript Studio Code), as well as a graphical programming interface 145 200 based on Google’s Blockly framework (STS metadata in com- 146 As an alternative to embedded interpreters, we present Static 201 ments defines the mapping from STS APIs to Blockly and 147 TypeScript (STS), a syntactic subset of TypeScript,[3] sup- 202 MakeCode translates between Blockly and STS). 148 ported by a compiler (written in TypeScript) that generates 203 The MakeCode editors, including the primary coding ex- 149 machine code that runs efficiently on MCUs in the target 204 periences for BBC micro:bit and for Adafruit CPX,2 have 150 RAM range of 16-256kB. The design of STS and its compiler 205 been used by millions of students and teachers worldwide 151 and supporting runtime were dictated primarily by the above 206 to date. 152 three challenges. In particular: 207 STS supports the concept of a package, a collection of STS, 153 • STS eliminates most of the “bad parts” of JavaScript; 208 C++ and assembly files, that also can list other packages as 154 following StrongScript [14], STS uses nominal typing 209 dependencies. This capability has been used by third parties 155 for statically declared classes and supports efficient 210 to extend the MakeCode editors, mainly to accommodate 156 compilation of classes using classic techniques for v- 211 hardware peripherals for various boards.3 Notably, most 157 tables. 212 of the packages avoid pitfalls of unsafe C/C++ completely 158 • the STS toolchain runs offline, once loaded into aweb 213 and are authored solely in STS, due to the efficiency of the 159 browser, without the need for a C/C++ compiler – the 214 STS compiler and the availability of low-level STS APIs for 160 toolchain, implemented in TypeScript, compiles STS 215 161 to Thumb machine code and links this code against 216 1See https://makecode.com. The framework, along with many editors, is 162 217 a pre-compiled C++ runtime in the browser, which open source under MIT license, see https://github.com/microsoft/pxt. 163 is often the only available execution environment in 2See https://makecode.microbit.org and https://makecode.adafruit.com. 218 164 schools. 3For example for micro:bit, see https://makecode.microbit.org/extensions 219 165 2 220 Static TypeScript MPLR 2019, Under submission, 221 276 222 277 223 278 224 279 225 280 226 281 227 282 228 283 229 284 230 285 231 286 232 287 233 288 234 289 235 290 236 291 237 292 238 293 239 294 240 295 241 296 242 297 243 Figure 3.