Static Typescript an Implementation of a Static Compiler for the Typescript Language

Static Typescript an Implementation of a Static Compiler for the Typescript Language

Static TypeScript An Implementation of a Static Compiler for the TypeScript Language Thomas Ball Peli de Halleux Michał Moskal Microsoft Research Microsoft Research Microsoft Research Redmond, WA, United States Redmond, WA, United States Redmond, WA, United States [email protected] [email protected] [email protected] Abstract Conference on Managed Programming Languages and Runtimes While the programming of microcontroller-based embed- (MPLR ’19), October 21–22, 2019, Athens, Greece. ACM, New York, dable devices typically is the realm of the C language, such NY, USA, 12 pages. https://doi.org/10.1145/3357390.3361032 devices are now finding their way into the classroom forCS education, even at the level of middle school. As a result, the 1 Introduction use of scripting languages (such as JavaScript and Python) for microcontrollers is on the rise. Recently, physical computing has been making headway in We present Static TypeScript (STS), a subset of TypeScript the classroom, engaging children to build simple interactive (itself, a gradually typed superset of JavaScript), and its com- embedded systems. For example, Figure1(a) shows the BBC piler/linker toolchain, which is implemented fully in Type- micro:bit [1], a small programmable Arduino-inspired com- Script and runs in the web browser. STS is designed to be use- puter with an integrated 5x5 LED display, several sensors ful in practice (especially in education), while being amenable and Bluetooth Low Energy (BLE) radio technology. The de- to static compilation targeting small devices. A user’s STS vice first rolled out in 2015 to all year 7 students (age 10to program is compiled to machine code in the browser and 11) in the UK and has since gone global, with four million linked against a precompiled C++ runtime, producing an ex- units distributed worldwide to date via the micro:bit Educa- ecutable that is more efficient than the prevalent embedded tion Foundation (https://microbit.org). Figure1(b) shows a interpreter approach, extending battery life and making it different educational device featuring RGB LEDs: Adafruit’s possible to run on devices with as little as 16 kB of RAM Circuit Playground Express (CPX). (such as the BBC micro:bit). Research suggests that using such devices in computer This paper is primarily a description of the STS system science education increases engagement, especially among and the technical challenges of implementing embedded girls, increases confidence in both students and teachers, and programming platforms in the classroom. makes lessons more fun [2, 16]. To keep costs low for schools, these devices typically em- CCS Concepts • Software and its engineering → Com- ploy 32 bit ARM Cortex-M microcontrollers (MCUs) with pilers; Runtime environments; • Computer systems or- 16-256kB of RAM and are programmed using an external ganization → Embedded software. computer (usually a laptop or desktop). Programming such devices in a classroom presents a number of technical chal- Keywords JavaScript, TypeScript, compiler, interpreter, mi- lenges: crocontrollers, virtual machine (1) the selection/design of an age-appropriate program- ACM Reference Format: ming language and environment; Thomas Ball, Peli de Halleux, and Michał Moskal. 2019. Static Type- (2) classroom computers running outdated operating sys- Script: An Implementation of a Static Compiler for the TypeScript tems, having intermittent and slow internet connec- Language. In Proceedings of the 16th ACM SIGPLAN International tivity, and locked down by school IT administrators, Permission to make digital or hard copies of all or part of this work for which makes native app installation difficult; personal or classroom use is granted without fee provided that copies (3) the transfer of the student’s program from the com- are not made or distributed for profit or commercial advantage and that puter to the device, where it can run on battery power copies bear this notice and the full citation on the first page. Copyrights (as many projects embed the device in an experiment for components of this work owned by others than the author(s) must or “make”). be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific With respect to these challenges, there are various embed- permission and/or a fee. Request permissions from [email protected]. ded interpreters for popular scripting languages, such as MPLR ’19, October 21–22, 2019, Athens, Greece JavaScript (JerryScript [8, 15], Duktape [22], Espruino [23], © 2019 Copyright held by the owner/author(s). Publication rights licensed to ACM. mJS [20], and MuJS [19]) and Python (MicroPython [9] and ACM ISBN 978-1-4503-6977-0/19/10...$15.00 its fork CircuitPython [12]). The interpreters run directly on https://doi.org/10.1145/3357390.3361032 the MCU, requiring just the transfer of program text from the MPLR ’19, October 21–22, 2019, Athens, Greece Thomas Ball, Peli de Halleux, and Michał Moskal 1.1 Static TypeScript As an alternative to embedded interpreters, we present Static TypeScript (STS), a syntactic subset of TypeScript,[3] sup- ported by a compiler (written in TypeScript) that generates machine code that runs efficiently on MCUs in the target RAM range of 16-256kB. The design of STS and its compiler and supporting runtime were dictated primarily by the above (a) (b) three challenges. In particular: • STS eliminates most of the “bad parts” of JavaScript; Figure 1. Two Cortex-M0 microcontroller-based educational following StrongScript [14], STS uses nominal typing devices: (a) the BBC micro:bit has a Nordic nRF51822 MCU for statically declared classes and supports efficient with 16 kB RAM and 256 kB flash; (b) Adafruit’s Circuit Play- compilation of classes using classic techniques for v- ground Express (https://adafruit.com/products/3333) has an tables. Atmel SAMD21 MCU with 32 kB RAM and 256 kB flash. • the STS toolchain runs offline, once loaded into aweb browser, without the need for a C/C++ compiler – the toolchain, implemented in TypeScript, compiles STS to Thumb machine code and links this code against a pre-compiled C++ runtime in the browser, which is often the only available execution environment in schools. • the STS compiler generates surprisingly efficient and compact machine code, which unlocks a range of ap- plication domains such as game programming for low- resource devices such as those in Figure2, all of which were enabled by STS. Deployment of STS user programs to embedded devices does not require app or device driver installation, just access to a web browser. Compiled programs appear as downloads, which are then transferred manually by the user to the device, which appears as a USB mass storage device, via file copy (or directly through WebUSB, an upcoming standard for connecting websites to physical devices). The relatively simple compilation scheme for STS (pre- sented in Section3) leads to surprisingly good performance Figure 2. Three microcontroller-based game handhelds with on a collection of small JavaScript benchmarks, often com- 160x120 color screens. These boards use ARM’s Cortex-M4F parable to advanced, state of the art JIT compilers like V8, core: the ATSAMD51G19 (192kB RAM, running at 120Mhz) with orders of magnitude smaller memory requirements (see and STM32F401RE (96kB RAM, running at 84Mhz). Section4). It is also at least an order of magnitude faster than the embedded interpreted approach. A novel aspect of evaluation is a comparison of different strategies for dealing with field/method lookup spanning classes, interfaces, and host computer, but forego the benefits of advanced optimiz- dynamic maps. ing JIT compilers (such as V8) that require about two orders of magnitude more memory than is available on MCUs. 1.2 MakeCode: Easy Embedded for Education Unfortunately, such embedded interpreters are between STS is the core language supported by the MakeCode Frame- one and three orders of magnitude slower than V8 (see Sec- work.1 MakeCode enables creation of custom programming tion4), affecting responsiveness and battery life. Even more experiences for MCU-based devices. Each MakeCode experi- importantly, due to the representation of objects in memory ence (we often call them editors, though they also bundle a as dynamic key-value mappings, the memory footprint can simulator, APIs, tutorials, documentation, etc.) targets pro- be several times that of an equivalent C program. This can gramming of a specific device or device class via STS. [7] severely limit the applications that can be deployed on low- memory devices such as the micro:bit (16 kB RAM) and CPX 1See https://makecode.com. The framework, along with many editors, is (32 kB RAM). open source under MIT license, see https://github.com/microsoft/pxt. Static TypeScript MPLR ’19, October 21–22, 2019, Athens, Greece Figure 3. MakeCode Arcade editor. The left pane is the simulator for the arcade device; the middle pane is the categories of APIs available in the editor; the right pane is the Monaco editor with STS user code for a platformer game (https://makecode. com/85409-23773-98992-33605). The toggle on top is used switch between Blocks and Static TypeScript (labelled JavaScript for marketing reasons). Most MakeCode editors are deployed primarily as web deployed to the three devices - it is a simple platformer apps, including a full-featured text editor for developing STS game). MakeCode Arcade includes a game engine written programs based on Monaco (the editor component of Visual almost completely in STS, and thus places high requirements Studio Code), as well as a graphical programming interface on code efficiency to achieve pleasing visual effects athigh based on Google’s Blockly framework (STS metadata in com- frame rates. The game engine includes the game loop, stack ments defines the mapping from STS APIs to Blockly and of event contexts, physics engine, text and line drawing, MakeCode translates between Blockly and STS).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us