0% found this document useful (0 votes)
24 views20 pages

Easy Compilation From TouchDevelop To ARM Cortex-M0 Using C++11 - Jonathan Protzenko - CppCon 2015

Uploaded by

alan88w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views20 pages

Easy Compilation From TouchDevelop To ARM Cortex-M0 Using C++11 - Jonathan Protzenko - CppCon 2015

Uploaded by

alan88w
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Going from TouchDevelop to

Cortex-M0: the BBC micro:bit


And making sure $(CPP) does all the work!

J. Protzenko
Some background (1)

• BBC has a “Make it Digital” campaign this year


• Goal: distribute 1 million devices to kids (11/12-year olds)
• Variety of partners
• Microsoft (programming environment)
• ARM / Farnell (device)
• U. of Lancaster (C++ runtime system)
• Samsung (Android app)

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 2


A picture!

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 3


Some background (2)

• The goal: introduce the CS curriculum using a fun & simple device
• Support from Computer At School (CAS) association
• Goal: engage teachers, parents, kids

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 4


Client Computer
Web browser
TouchDevelop program USB
1

User codes TouchDevelop Drive


Compiler
TouchDevelop
1 C++
program and presses
“compile” button
3 User drags ARM binary
C++ program ARM binary to drive for Micro Bit

C++
Compiler
mbed C++ SDK
2
ARM 2 User accepts download
of ARM binary
mbed compile service
ARM binary
Azure
TouchDevelop-the-language

• Syntax-directed editor, JavaScript-inspired


• Statically type-checked
• Garbage-collected

• Closures (capture-by-value, à la JavaScript)


• Does not prevent uninitialized values (run-time errors)
• Everything happily mutually-recursive

• Primitive support for “libraries”


9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 6
Compiling to C++: a good idea?

• Option #1: write an interpreter on the device


• Option #2: run the byte-code compiler in the browser and run a byte-
code interpreter on the device
• Option #3: write our own compilation toolchain 😸

As often happens, constraints on time and engineering resources led us


into Option #4.

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 7


Types and variables (1)

TouchDevelop program Generated code


Number incr(Number x) {
return number::plus(x, 1);
}

typedef int Number;


typedef bool Boolean;
typedef ManagedString String;
typedef void (*Action)();
Hand-written glue

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 8


Types and variables (2)

• Generate human-readable, valid C++ identifiers


• Wrap everything in a namespace (to limit pollution)
• Fill out definitions for types (typedef) and functions

That’s the basic idea.

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 9


« Shimming »

• {shim:…} annotations: can be referenced,


but no implementation generated

void main() {
micro_bit::scrollString(
touch_develop::mk_string(
"I ♥ CppCon"), 150);
}

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 10


Memory management

• We went for ref-counting (why?)


• Now, advanced scenarios can create cycles (that’s too bad)
• Custom ref-counting class (why? why?!!)

memory leak on copy assign of an 'empty'


object created with the default constructor
so 'now' was being created with no value,
std::shared_ptr!
but had a reference counter. the subsequent
line assigned a value, and adopted a new
reference counter, but never freed the old
one.

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 11


Built-in library types

void main() {
Collection<Number> coll;
coll = create::collection_of<Number>();
collection::add(coll, 2015);
}

#if __cplusplus > 199711L


template <typename T> using Collection = ManagedType<vector<T>>;
namespace create {
template<typename T> Collection<T> collection_of() {
return ManagedType<vector<T>>(new vector<T>());
}
#endif

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 12


User-defined types (1)

namespace user_types {
struct list_;
typedef ManagedType<list_> list;
// …
// every other type definition in the current program
}

namespace user_types {
struct list_ {
Number data;
user_types::list next;
};
}

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 13


User-defined types (2)

Number length(user_types::list l) {
if (list::is_invalid(l)){
return 0;
} else {
return number::plus(
1,
length((l.get() != NULL ? l->next : (uBit.panic(…), l->next))));
}
}

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 14


Recursion hell
namespace user_types {
• Global compilation, every time struct list_;
typedef ManagedType<list_> list;
• Within a “library”; // more here…
}
• Functions are mutually recursive namespace my_library {
• Types are mutually recursive namespace user_types {
Number length(user_types::list l);
// more here…
• They can have the same name // more here…
}
}
• “Libraries” are not mutually recursive namespace my_library {
namespace user_types {
// more here…
• Each library is wrapped in a C++ namespace struct list_ {
}
// …
• Can reference types/functions from other libraries };
// more here…
Number length(user_types::list l) {
}
// …
namespace my_library {
}
namespace user_types {
// more here
// more here…
9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 } 15
}
Closures!

Implement
closure-
Better use conversion?
C++11! Oh noes!

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 16


Closures! (2)

Ref<Number> y;
y = create::ref_of<Number>();
ref::_set(y, 0);
auto _body_0_ = [=] () -> void {
micro_bit::scrollNumber(ref::_get(y), 150);
};
auto _body_0 =
new std::function<void ()>(_body_0_);
micro_bit::onButtonPressed(
MICROBIT_ID_BUTTON_B, _body_0);
ref::_set(y, 1);

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 17


Closures (3)

• Closures that do not capture anything are lifted


• Closures that do capture currently not implemented due to bad
semantics

• The “ARMCC” killer. (GCC it is, now – just 300k wasted SRAM.)

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 18


Looking forward

• C++: as usual, a high-level language for low-level targets


• Generic translation; repurpose for Arduino, for instance?
• Extensible: uses Yotta (a.k.a. “node.js for hardware”)
• Hardware projects: displays, connectors, Bluetooth Low Energy

9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 19


Looking forward (2)

• We launched the website; board delayed (unsurprisingly)


• Hardware targets: TouchDevelop’s raison d’être?
• Hip and chic: IOT

Thanks!
9/21/2015 J. Protzenko: Compiling TouchDevelop to C++11 / CppCon’15 20

You might also like