Optimal Monkey C

Over the past two years, the Connect IQ team has been rewriting the Monkey C build tools from scratch to create a more modern, advanced toolchain. The first preview of the tools came in 2020 when we announced Monkey Types. Today, we are providing our first look at the new and improved Monkey C toolchain.

Here are some of the improvements:

Optimizing Code Generator

The compiler now has a new optimization pass. This optimization pass will perform constant folding, constant substitution, branch elimination and a few other optimizations before generating your executable. These optimizations will make your code smaller and more performant without any code changes.

You can use the -O command line option to set the optimization level. You can set the command line option in the Visual Studio Code plug in from the Workspace settings:

Currently, level range from 0 (no optimizations) to 2 (release). Level 2 should bring the most code space reduction. 

Typing by Default

We want to build the tools your need to enable you to create the best apps for your customers. One of those tools is the Monkey Types, the gradual type system for Monkey C, that we announced at GDVC 2020. Since that first version, it's ability to detect errors at compile time has only grown. We believe the system is ready for prime time.

We are enabling Monkey Types to type check code at the gradual level by default. This level operates between dynamic and static typing, checking the API but not requiring you to type your application. You can learn more about the Monkey Types system and how to configure it for your project here.

Typing is not required for optimizations, and can be disabled within the Visual Studio Code project settings or with -l 0 from the command line.

Improved Code Analysis

The old compiler leaned heavily on the device virtual machine, transforming your code to bytecode with limited analysis in between. The new compiler features now transform your source into an intermediate representation (IR) that is used through the rest of the compilation phases. While still operating as a dynamic language, the IR has enough information to perform the complex analysis necessary for type checking and code generation. The compiler will be able to find many more errors up front, so you don't have to find them at runtime.

Import and Object Instantiation

With the old compiler, import behaved differently between type syntax and functional syntax. With the type syntax, the module name of referenced classes could be inferred by the list of imports; but in function code, it had to be explicitly stated:

import B;

// Type syntax - no module
function usageExample() as ClassInB {
    // Functional syntax - required module
    return B.ClassInB();
}

With the new compiler, import works the same with types and functional syntax:

import B;

// Type syntax - no module
function usageExample() as ClassInB {
    // Functional syntax - no more module
    return ClassInB();
}

Note that using statements retain the module requirement for backwards compatibility.

Improved Logging

If you encounter a bug with the monkeyc compiler you will need to generate logs to provide with your report. Here are the options to create debug logs:

Long Option Argument Description
--debug-log-level 0 = Errors, 1 = Basic Debug, 2 = Intermediate Debug, 3 = Verbose Debug Specifies the verbosity level for the output.
--debug-log-output Path to log file to generate Specifies the path to the log file to create
--debug-log-device Device identifier Allows limiting logs to a specific device when building a Monkey Barrel.

Be aware that the log levels include increasingly more information about your source project. If you want to keep your project information private, limit the logging level to 1 or less. More verbosity will make it easier for Garmin to debug reported issues.

Try it Today

We have several improvements planned but wanted to let you try it for yourselves. You can find the beta in the SDK manager under "Connect IQ 4.1.4.Compiler 2 Beta":

Please provide feedback on the forums. We look forward to your feedback.