SlideShare a Scribd company logo
Device­specific Clang Tooling for
Embedded Systems
Jackie Kay
jackieokay.com
emBO++, 18/2/17
Goal:
Feed device memory maps to the compiler.
How?
1. Parse CMSIS SVD file to get device memory map.
2. Write Clang AST matchers to locate raw memory accesses
and fix­its to refactor accesses for type­safety and readability.
3. Run matchers over code.
Step One
To parse CMSIS SVD files, use the same Python backend that
generates Kvasir header files.
namespace clock_hfclkrun {
using Addr = bit::Address<0x40000408, 0xfffffffe,
                          0x00000000, unsigned>;
enum class StatusVal : unsigned {
    nottriggered=0x00000000,
    triggered=0x00000001,
};
constexpr Register::FieldLocation<Addr,
  bit::maskFromRange(0, 0),
  bit::ReadWriteAccess, StatusVal> status{};
/* ... */
Generate YAML that associates registers, fields, and values.
­ address: 0x40000408
  name: clock_hfclkrun
  fields:
  ­ mask: 0x1
    name: status
    values:
    ­ name: nottriggered
      value: 0x0
    ­ name: triggered
      value: 0x1
Step Two
We want to find this:
*(volatile uint32_t*)0x40000408 = 0x1;
And turn it into this:
apply(write(clock_hfclkrun::statusValC::triggered));
Writing a  clang­tidy  check offers a powerful framework for:
Registering AST Matchers
Configuration and command line options
Pretty­printing diagnostics
Refactoring with fix­its
Writing an AST Matcher
auto volatileDereferenceMatcher = allOf(
  unaryOperator(hasOperatorName("*"),
    hasUnaryOperand(ignoringParenImpCasts(
      explicitCastExpr(
        hasDestinationType(isAnyPointer()),
        hasSourceExpression(expr().bind("address")))
    ))
  ),
  expr(hasType(isVolatileQualified()))
);
Finds this:
*(volatile uint32_t*)0x40000408
Load YAML representation into a map
Check if matched expressions can be evaluated at compile­
time as register values in our memory map
Try to decompose the assigned literal into known values if
applicable
Fix­it hints
diag(MatchedLocation­>getLocStart(),
  "Found write to register via volatile cast")
  << FixItHint::CreateReplacement(
    ReplacementRange, replacement);
Result:
test.c:5:3: warning: Found write to register via
volatile cast [embedded­typesafe­register­write]
  *(volatile std::uint32_t*)0x40000408 = 0x1;
  ^
  apply(write(clock_hfclkrun::statusValC::triggered))
Step Three
How to compile code for the Cortex M0 with Clang from the
command line on Linux?
clang ­target arm­v6m­unknown­none­eabi 
  ­ccc­gcc­name arm­none­eabi­gcc 
  ­mcpu=cortex­m0 ­mfloat­abi=soft 
  ­mthumb ­fshort­enums ­specs=nosys.specs 
  ­I/usr/include/newlib/c++/4.9.3 
  ­I/usr/include/newlib/c++/4.9.3/arm­none­eabi/armv6­m
  blinky.c
Clang tooling requires " compile_commands.json ", which is
generated by CMake ( ­DCMAKE_EXPORT_COMPILE_COMMANDS )
If using  make , use Bear
May need to tweak Bear's output to include Clang­specific
flags for cross­compiling
Give YAML file to .clang­tidy:
CheckOptions:
­ key: embedded­typesafe­register­write.DescriptionFile
  value: nrf51.yaml
To run the tool:
clang­tidy ­checks=embedded­typesafe­register­write 
  ­p compile_commands.json blinky.c
In reality, the naive AST matcher often isn't enough.
__STATIC_INLINE void nrf_clock_task_trigger(
  nrf_clock_task_t task)
{
  *((volatile uint32_t *)((uint8_t *)NRF_CLOCK + task))
    = NRF_CLOCK_TASK_TRIGGER;
}
Base peripheral offset is a literal (defined by a macro) but  task 
cannot be evaluated at compile time.
Could try to analyze at call site.
Step Four
The more we can check statically/at compile time, the better.
Register access checks are just the beginning.
The tools are already here, though one must design them with
awareness of existing patterns.
Maybe we don't need to give up on legacy code if we can write
great refactoring tools.
Resources
github.com/jacquelinekay/embo2017presentation
Implementation of linting tool on Github
Writing a clang­tidy check
CMSIS SVD Specification
Cross­compilation using Clang
nrf5 SDK from Nordic Semiconductor
Kvasir Hardware Description File Generator

More Related Content

What's hot (20)

PDF
YOW2020 Linux Systems Performance
Brendan Gregg
 
PDF
Performance Profiling in Rust
InfluxData
 
PDF
eBPF Perf Tools 2019
Brendan Gregg
 
PDF
Performance Wins with eBPF: Getting Started (2021)
Brendan Gregg
 
PDF
Kernel Recipes 2017: Using Linux perf at Netflix
Brendan Gregg
 
PDF
Performance Wins with BPF: Getting Started
Brendan Gregg
 
PDF
FreeBSD 2014 Flame Graphs
Brendan Gregg
 
PPTX
Profiling & Testing with Spark
Roger Rafanell Mas
 
PDF
Tuning parallelcodeonsolaris005
dflexer
 
PDF
re:Invent 2019 BPF Performance Analysis at Netflix
Brendan Gregg
 
PDF
Kernel development
Nuno Martins
 
PDF
USENIX ATC 2017: Visualizing Performance with Flame Graphs
Brendan Gregg
 
PDF
LPC2019 BPF Tracing Tools
Brendan Gregg
 
PDF
Java Performance Analysis on Linux with Flame Graphs
Brendan Gregg
 
PDF
Netflix: From Clouds to Roots
Brendan Gregg
 
PDF
計算機性能の限界点とその考え方
Naoto MATSUMOTO
 
PPTX
Modern Linux Tracing Landscape
Sasha Goldshtein
 
PDF
Performance tweaks and tools for Linux (Joe Damato)
Ontico
 
PDF
Open Source Systems Performance
Brendan Gregg
 
PPTX
QCon 2015 Broken Performance Tools
Brendan Gregg
 
YOW2020 Linux Systems Performance
Brendan Gregg
 
Performance Profiling in Rust
InfluxData
 
eBPF Perf Tools 2019
Brendan Gregg
 
Performance Wins with eBPF: Getting Started (2021)
Brendan Gregg
 
Kernel Recipes 2017: Using Linux perf at Netflix
Brendan Gregg
 
Performance Wins with BPF: Getting Started
Brendan Gregg
 
FreeBSD 2014 Flame Graphs
Brendan Gregg
 
Profiling & Testing with Spark
Roger Rafanell Mas
 
Tuning parallelcodeonsolaris005
dflexer
 
re:Invent 2019 BPF Performance Analysis at Netflix
Brendan Gregg
 
Kernel development
Nuno Martins
 
USENIX ATC 2017: Visualizing Performance with Flame Graphs
Brendan Gregg
 
LPC2019 BPF Tracing Tools
Brendan Gregg
 
Java Performance Analysis on Linux with Flame Graphs
Brendan Gregg
 
Netflix: From Clouds to Roots
Brendan Gregg
 
計算機性能の限界点とその考え方
Naoto MATSUMOTO
 
Modern Linux Tracing Landscape
Sasha Goldshtein
 
Performance tweaks and tools for Linux (Joe Damato)
Ontico
 
Open Source Systems Performance
Brendan Gregg
 
QCon 2015 Broken Performance Tools
Brendan Gregg
 

Recently uploaded (20)

PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Advancing WebDriver BiDi support in WebKit
Igalia
 
PDF
July Patch Tuesday
Ivanti
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Advancing WebDriver BiDi support in WebKit
Igalia
 
July Patch Tuesday
Ivanti
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Biography of Daniel Podor.pdf
Daniel Podor
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Ad

Device-specific Clang Tooling for Embedded Systems