Ada for the Embedded C Developer Quentin Ochem Robert Tice Gustavo A
Total Page:16
File Type:pdf, Size:1020Kb
Ada for the Embedded C Developer Quentin Ochem Robert Tice Gustavo A. Hoffmann Patrick Rogers Ada for the Embedded C Developer Release 2021-09 Quentin Ochem Robert Tice Gustavo A. Hoffmann Patrick Rogers. Sep 17, 2021 CONTENTS 1 Introduction 3 1.1 So, what is this Ada thing anyway? ............................... 3 1.2 Ada — The Technical Details .................................. 5 2 The C Developer's Perspective on Ada 7 2.1 What we mean by Embedded Software ............................ 7 2.2 The GNAT Toolchain ....................................... 7 2.3 The GNAT Toolchain for Embedded Targets ......................... 7 2.4 Hello World in Ada ........................................ 8 2.5 The Ada Syntax .......................................... 9 2.6 Compilation Unit Structure ................................... 10 2.7 Packages .............................................. 10 2.7.1 Declaration Protection ................................. 11 2.7.2 Hierarchical Packages ................................. 12 2.7.3 Using Entities from Packages ............................. 12 2.8 Statements and Declarations .................................. 13 2.9 Conditions ............................................. 18 2.10 Loops ............................................... 21 2.11 Type System ........................................... 27 2.11.1 Strong Typing ...................................... 27 2.11.2 Language-Defined Types ................................ 31 2.11.3 Application-Defined Types ............................... 31 2.11.4 Type Ranges ....................................... 34 2.11.5 Unsigned And Modular Types ............................. 36 2.11.6 Attributes ......................................... 40 2.11.7 Arrays and Strings ................................... 41 2.11.8 Heterogeneous Data Structures ........................... 48 2.11.9 Pointers .......................................... 50 2.12 Functions and Procedures ................................... 54 2.12.1 General Form ...................................... 54 2.12.2 Overloading ....................................... 57 2.12.3 Aspects .......................................... 60 3 Concurrency and Real-Time 63 3.1 Understanding the various options .............................. 63 3.2 Tasks ................................................ 63 3.3 Rendezvous ............................................ 66 3.4 Selective Rendezvous ...................................... 67 3.5 Protected Objects ........................................ 69 3.6 Ravenscar ............................................. 73 4 Writing Ada on Embedded Systems 77 4.1 Understanding the Ada Run-Time ............................... 77 4.2 Low Level Programming ..................................... 78 i 4.2.1 Representation Clauses ................................ 78 4.2.2 Embedded Assembly Code .............................. 79 4.3 Interrupt Handling ........................................ 80 4.4 Dealing with Absence of FPU with Fixed Point ........................ 82 4.5 Volatile and Atomic data .................................... 86 4.5.1 Volatile .......................................... 86 4.5.2 Atomic .......................................... 88 4.6 Interfacing with Devices ..................................... 89 4.6.1 Size aspect and attribute ............................... 90 4.6.2 Register overlays .................................... 91 4.6.3 Data streams ...................................... 94 4.7 ARM and svd2ada ........................................ 99 5 Enhancing Verification with SPARK and Ada 101 5.1 Understanding Exceptions and Dynamic Checks ...................... 101 5.2 Understanding Dynamic Checks versus Formal Proof ................... 107 5.3 Initialization and Correct Data Flow .............................. 110 5.4 Contract-Based Programming ................................. 111 5.5 Replacing Defensive Code ................................... 114 5.6 Proving Absence of Run-Time Errors ............................. 116 5.7 Proving Abstract Properties .................................. 117 5.8 Final Comments ......................................... 117 6 C to Ada Translation Patterns 119 6.1 Naming conventions and casing considerations ....................... 119 6.2 Manually interfacing C and Ada ................................ 119 6.3 Building and Debugging mixed language code ....................... 121 6.4 Automatic interfacing ...................................... 122 6.5 Using Arrays in C interfaces ................................... 122 6.6 By-value vs. by-reference types ................................ 124 6.7 Naming and prefixes ...................................... 125 6.8 Pointers .............................................. 126 6.9 Bitwise Operations ........................................ 128 6.10 Mapping Structures to Bit-Fields ................................ 130 6.10.1 Overlays vs. Unchecked Conversions ........................ 143 7 Handling Variability and Re-usability 147 7.1 Understanding static and dynamic variability ........................ 147 7.2 Handling variability & reusability statically .......................... 147 7.2.1 Genericity ........................................ 147 7.2.2 Simple derivation .................................... 150 7.2.3 Configuration pragma files .............................. 155 7.2.4 Configuration packages ................................ 157 7.3 Handling variability & reusability dynamically ........................ 161 7.3.1 Records with discriminants .............................. 161 7.3.2 Variant records ..................................... 163 7.3.2.1 Variant records and unions .......................... 165 7.3.2.2 Optional components ............................. 166 7.3.2.3 Optional output information ......................... 167 7.3.3 Object orientation .................................... 170 7.3.3.1 Type extension ................................. 170 7.3.3.2 Overriding subprograms ........................... 171 7.3.3.3 Comparing untagged and tagged types .................. 171 7.3.3.4 Dispatching calls ................................ 174 7.3.3.5 Interfaces .................................... 175 7.3.3.6 Deriving from multiple interfaces ...................... 177 7.3.3.7 Abstract tagged types ............................. 178 7.3.3.8 From simple derivation to OOP ....................... 180 7.3.3.9 Further resources ............................... 182 ii 7.3.4 Pointer to subprograms ................................ 182 7.4 Design by components using dynamic libraries ....................... 187 8 Performance considerations 191 8.1 Overall expectations ....................................... 191 8.2 Switches and optimizations ................................... 191 8.2.1 Optimizations levels .................................. 191 8.2.2 Inlining .......................................... 192 8.3 Checks and assertions ...................................... 193 8.3.1 Checks .......................................... 193 8.3.2 Assertions ........................................ 196 8.4 Dynamic vs. static structures .................................. 197 8.5 Pointers vs. data copies ..................................... 198 8.5.1 Function returns .................................... 201 9 Argumentation and Business Perspectives 205 9.1 What's the expected ROI of a C to Ada transition? ..................... 205 9.2 Who is using Ada today? .................................... 206 9.3 What is the future of the Ada technology? .......................... 206 9.4 Is the Ada toolset complete? .................................. 207 9.5 Where can I find Ada or SPARK developers? ......................... 207 9.6 How to introduce Ada and SPARK in an existing code base? ................ 208 10 Conclusion 209 11 Appendix A: Hands-On Object-Oriented Programming 213 11.1 System Overview ......................................... 213 11.2 Non Object-Oriented Approach ................................ 214 11.2.1 Starting point in C .................................... 214 11.2.2 Initial translation to Ada ................................ 217 11.2.3 Improved Ada implementation ............................ 221 11.3 First Object-Oriented Approach ................................ 225 11.3.1 Interfaces ......................................... 225 11.3.2 Base type ......................................... 225 11.3.3 Derived types ...................................... 226 11.3.4 Subprograms from parent ............................... 227 11.3.5 Type AB .......................................... 228 11.3.6 Updated source-code .................................. 228 11.4 Further Improvements ..................................... 232 11.4.1 Dispatching calls .................................... 232 11.4.2 Dynamic allocation ................................... 233 11.4.3 Limited controlled types ................................ 234 11.4.4 Updated source-code .................................. 235 Bibliography 241 iii iv Ada for the Embedded C Developer, Release 2021-09 Copyright © 2020 – 2021, AdaCore This book is published under a CC BY-SA license, which means that you can copy, redistribute, remix, transform, and build upon the content for any purpose, even commercially, as long as you give appropriate credit, provide a link to the license, and indicate if changes were made. If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the