SlideShare a Scribd company logo
1 of 17
Download to read offline
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
A modern tool for interfacing and creating
bare bone C programmes
Jordan Hrycaj
<jordan@mjh-it.co.uk>
Camsec - 26/01/2017
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Overview
Code Generation Tools For C
My Problem: Embedded/Multi-Platform Compiler
Why I would stay with C
Observations after choosing NIM
Working with NIM
The Language
NIM Workflow
Examples
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Motivation
Support for operating systems
Windows, Posix (Linux, Darwin), BSD, embedded
Support for architectures
Intel/AMD, ARM, probably GPU
Write code once, use it everywhere
JVM not an option on small systems
support hardware (mm registers, mmu)
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Checking out well known systems
Comparing some alternatives to C.
C++
The industry standard, has evolved considerably
Still not happy with C++ (eg. memory management)
Older systems might be unsupported (as of C++11/14)
Bloated binaries (think of IoT)
Alternative Rust
Modern, seems to tick most boxes (ref counts, no GC)
Feels a bit like designed-by-committee
Older systems are unsupported
Alternative GO
Many good ideas
A bit more low-level (than Rust)
GCC/GO not available on Windows
More examples ...
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Using plain/bare bone C
Why using C at all?
Coding in C can be a pain
Bloated code (but small binaries)
... like crawling when you could use a car
Bare bone C++ is possible
eg. w/o traps/exceptions
highly dependent on target system compiler/linker
... but plain C
It is universally supported, profiling, optimisers etc.
Fall back strategies (older systems, missing features)
Small language (compared to C++), features in libraries
... so generate C code, practised already widely
CPP, code generators (bison, flex, re2c etc.)
C itself
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Solution: Compile to C
Compilation: high level language → C → binary
Wish list for a compiler: Must Have
Functional support, closures
Convincing memory management (GC, ref count)
Important
Complex but clean data structures
Generic C support (eg. inline, FFI)
Cross-compiling made easy
Templates/Macros
Optional
Multi threading support (actors, pools, etc.)
Built-in OO
Remark: I found some really interesting stuff but many
compilers produce C++ code which is what I wanted to
avoid.
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Useful tools to generate C code
Plain C compilers
Chicken Scheme (since 2000)
Compiler/interpreter
Rich library (items called “eggs”)
Small runtime library
Vala OO compiler (since 2006)
C# like programming language
GLib objects (can do without on a subset of features)
GLib seems to be portable but is bloated and big
NIM (formerly Nimrod, since 2008)
Imperative, statically typed, functional
Influenced by Ada, C++, Lisp, C#, etc.
AST exposed for meta/macro programming
Produces C code ready for target system
Small runtime library (unless GC is avoided)
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
I ended up choosing NIM
But Chicken would have been OK as well
Advantages
Seems to be used in some industry/business applications
Expressive syntax feels more like scripting (Perl/Python)
Simple but (sometimes too) powerful way of coding
Generics (~C++ templates), templates (simple macros)
Macros (AST programming, ~Lisp macros) for DSL
Can also produce C++, Obj C, and JS (probably others)
Caveats
Still experimental version
Set up by a benevolent dictator + crew of enthusiasts
C coding experience needed for NIM to be most useful
No backing funds like for Go, Rust
Verdict after using it for several months
Useful even if support stops
Most features I need are available
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Hello World
NIM is elegant (I am not aware of another elegant language)
The programme
echo "Hello World"
MAIN calling a function
proc helloWorld() =
‌‌ echo "Hello World"
helloWorld()
MAIN calling a function with optional argument
proc helloWorld(text = “Hello World”) =
echo text
“Hello People”.helloWorld
Note that the type of text is string – inferred by its
default argument. A more complete way of stating the
argument would be: text:string=“Hello World”
See http://nim-by-example.github.io
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Oddities/Niceties
Things that are different in NIM
Symbol names (1st character case important)
theSymbol, the_symbol, theEsymboL are equal
TheSymbol and theSymbol are different
Closure support often needs annotation
For C the pragma {.procvar.} usually works
Compiler needs to figure out for potential concurrency
Results in plain C (no run time lib needed)
Many NIM features are are available at compile time
Can process files to create complex static data
Functional filters and operators but no OO
Sequence functions head() and tail() are missing
proc tail*[T](s: openArray[T]): seq[T] {.inline.} =
if 0 < s.len: (@s)[1 .. <s.len] else: @[]
Only finite sequence types supported
No tail recursion for formally unbounded sequences
Sequence type ad-hoc extensible
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Speed/Time Comparisons
http://arthurtw.github.io/2015/01/12/quick-comparison-nim-vs-
rust.html
Game of Life Rust Nim/boundChecks:on n=30000
with map print 1x 1.75x / 1.87x 1x=3.33s
without map print 1x 1.15x / 1.72x 1x=0.78s
http://togototo.wordpress.com/2013/08/23/benchmarks-round-two-
parallel-go-rust-d-scala-and-nimrod/
Lang Compiler Speed/s %Fastest Res.Mem/KiB
D ldc2 0.812 116.38% 26,536
C++ clang++ 0.945 100.00% 25,552
Nimrod clang 0.980 96.43% 25,932
C++ g++ 1.025 92.20% 25,532
Rust rustc 1.109 85.21% 47,708
Go 6g 1.184 79.81% 30,768
C clang 1.199 78.82% 25,796
Scala scala 1.228 76.95% 72,960
Go gccgo 2.710 34.87% 69,120
(excerpt)
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Code Generation
Compile-and-run a programme with
nim c -r helloworld.nim
what happens in the background is
The compiler builds up an AST
several compiler passes
imported code libraries are merged into the AST
Depending on the target code – assume C for now
C code files are generated
placed into the ./nimcache directory
one C source per imported library
The compiler starts a C compiler on the C sources
optimised for GCC, Clang, Vcc (fallback: tiny CC)
produces binary
The binary is started
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
NIM Tools
Besides Compiler
Rudimentary REPL
Install: nimble install nrpl
NIM embedded debugger
endb, outdated
GDB
Compile NIM with line pragmas enabled
Works fine for experienced C coder
nim2c
Convert C code to NIM code
Handy tool, needs manual post-processing
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Simple Functional Stuff
code walk over misc/nimsrc.nim
generic function: tail()
generic T: any data type, normally inferred (see tests)
openArray: ordered data items of all the same type
type inference: .. else @[]
pattern matching for C optimisation
seq and openArray
check generated code
function: cnfValue()
input cnfTable(): seq[] of string pairs (AVP list)
add item that always matches: concat(@[(s,””)])
filter out first match: filterIt(it[0] == s)
get first match: head()
extract pair from sequence: [0], get value: [1]
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
Compile Time Coding
code walk over misc/nimsrc.nim
template: nimSrcFilename()
compiler support: instantiationInfo()
info about code that invokes it, so it must be a
macro/template
functions: cnfTable() and cnfValue()
extract AVP list from C header config.h
from autoconf environment
all compile time: slurp/staticRead, gorge/staticExec
no OO support
compiler quits if slurp() fails
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
C Bindings
code walk over zlib/zlib.nim
C struct z_stream vs. NIM object TZStream
NIM objects are GC controlled (tuples are not)
compatibility types cstring, cint, cuint, etc.
verify descriptor mapping: zstreamspecs.c
cross compiling i386/x64/Linux/Windows etc.
cstring, cint, probably struct alignments vary
see import/binding in test section: tZstreamSpecs()
doAssert() validity of descriptor mapping
Zlib part is compiled all first
using macros and compile time lists
note the compile time path separator D
rather than / operator or DirSep
when host/target systems differ (eg. Posix/Windows)
A modern tool
for interfacing
and creating bare
bone C
programmes
Jordan Hrycaj
Code Generation
Tools For C
My Problem:
Embedded/Multi-
Platform
Compiler
Stay with C
Choosing NIM
Working with
NIM
The Language
NIM Workflow
Examples
Summary
NIM Summary
Produces code for target system/compiler
C/C++/ObjC, JS
Multi paradigm language
extensible, DSL, accessible AST
Targeting C
Easy to interface C libraries
Supports C cross compiling
GDB aware for debugging
Young language
Small (but not too small) user group
Documentation OK (possibly more examples needed)
No big sponsors

More Related Content

Similar to Working with NIM - By Jordan Hrycaj

Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docMayurWagh46
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...bhargavi804095
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...bhargavi804095
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxNEHARAJPUT239591
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJmeharikiros2
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfAnassElHousni
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programmingRokonuzzaman Rony
 
C Unit 1 notes PREPARED BY MVB REDDY
C Unit 1 notes PREPARED BY MVB REDDYC Unit 1 notes PREPARED BY MVB REDDY
C Unit 1 notes PREPARED BY MVB REDDYRajeshkumar Reddy
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkDr.Neeraj Kumar Pandey
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it worksMark John Lado, MIT
 
C++ language basic
C++ language basicC++ language basic
C++ language basicWaqar Younis
 

Similar to Working with NIM - By Jordan Hrycaj (20)

Programming in c plus plus2
Programming in c plus plus2Programming in c plus plus2
Programming in c plus plus2
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
C_Intro.ppt
C_Intro.pptC_Intro.ppt
C_Intro.ppt
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
C intro
C introC intro
C intro
 
Srgoc dotnet_new
Srgoc dotnet_newSrgoc dotnet_new
Srgoc dotnet_new
 
C Unit 1 notes PREPARED BY MVB REDDY
C Unit 1 notes PREPARED BY MVB REDDYC Unit 1 notes PREPARED BY MVB REDDY
C Unit 1 notes PREPARED BY MVB REDDY
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
C programming first_session
C programming first_sessionC programming first_session
C programming first_session
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
 
C++ language basic
C++ language basicC++ language basic
C++ language basic
 
C programming
C programmingC programming
C programming
 

Recently uploaded

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Working with NIM - By Jordan Hrycaj

  • 1. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj <jordan@mjh-it.co.uk> Camsec - 26/01/2017
  • 2. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Overview Code Generation Tools For C My Problem: Embedded/Multi-Platform Compiler Why I would stay with C Observations after choosing NIM Working with NIM The Language NIM Workflow Examples
  • 3. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Motivation Support for operating systems Windows, Posix (Linux, Darwin), BSD, embedded Support for architectures Intel/AMD, ARM, probably GPU Write code once, use it everywhere JVM not an option on small systems support hardware (mm registers, mmu)
  • 4. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Checking out well known systems Comparing some alternatives to C. C++ The industry standard, has evolved considerably Still not happy with C++ (eg. memory management) Older systems might be unsupported (as of C++11/14) Bloated binaries (think of IoT) Alternative Rust Modern, seems to tick most boxes (ref counts, no GC) Feels a bit like designed-by-committee Older systems are unsupported Alternative GO Many good ideas A bit more low-level (than Rust) GCC/GO not available on Windows More examples ...
  • 5. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Using plain/bare bone C Why using C at all? Coding in C can be a pain Bloated code (but small binaries) ... like crawling when you could use a car Bare bone C++ is possible eg. w/o traps/exceptions highly dependent on target system compiler/linker ... but plain C It is universally supported, profiling, optimisers etc. Fall back strategies (older systems, missing features) Small language (compared to C++), features in libraries ... so generate C code, practised already widely CPP, code generators (bison, flex, re2c etc.) C itself
  • 6. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Solution: Compile to C Compilation: high level language → C → binary Wish list for a compiler: Must Have Functional support, closures Convincing memory management (GC, ref count) Important Complex but clean data structures Generic C support (eg. inline, FFI) Cross-compiling made easy Templates/Macros Optional Multi threading support (actors, pools, etc.) Built-in OO Remark: I found some really interesting stuff but many compilers produce C++ code which is what I wanted to avoid.
  • 7. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Useful tools to generate C code Plain C compilers Chicken Scheme (since 2000) Compiler/interpreter Rich library (items called “eggs”) Small runtime library Vala OO compiler (since 2006) C# like programming language GLib objects (can do without on a subset of features) GLib seems to be portable but is bloated and big NIM (formerly Nimrod, since 2008) Imperative, statically typed, functional Influenced by Ada, C++, Lisp, C#, etc. AST exposed for meta/macro programming Produces C code ready for target system Small runtime library (unless GC is avoided)
  • 8. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary I ended up choosing NIM But Chicken would have been OK as well Advantages Seems to be used in some industry/business applications Expressive syntax feels more like scripting (Perl/Python) Simple but (sometimes too) powerful way of coding Generics (~C++ templates), templates (simple macros) Macros (AST programming, ~Lisp macros) for DSL Can also produce C++, Obj C, and JS (probably others) Caveats Still experimental version Set up by a benevolent dictator + crew of enthusiasts C coding experience needed for NIM to be most useful No backing funds like for Go, Rust Verdict after using it for several months Useful even if support stops Most features I need are available
  • 9. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Hello World NIM is elegant (I am not aware of another elegant language) The programme echo "Hello World" MAIN calling a function proc helloWorld() = ‌‌ echo "Hello World" helloWorld() MAIN calling a function with optional argument proc helloWorld(text = “Hello World”) = echo text “Hello People”.helloWorld Note that the type of text is string – inferred by its default argument. A more complete way of stating the argument would be: text:string=“Hello World” See http://nim-by-example.github.io
  • 10. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Oddities/Niceties Things that are different in NIM Symbol names (1st character case important) theSymbol, the_symbol, theEsymboL are equal TheSymbol and theSymbol are different Closure support often needs annotation For C the pragma {.procvar.} usually works Compiler needs to figure out for potential concurrency Results in plain C (no run time lib needed) Many NIM features are are available at compile time Can process files to create complex static data Functional filters and operators but no OO Sequence functions head() and tail() are missing proc tail*[T](s: openArray[T]): seq[T] {.inline.} = if 0 < s.len: (@s)[1 .. <s.len] else: @[] Only finite sequence types supported No tail recursion for formally unbounded sequences Sequence type ad-hoc extensible
  • 11. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Speed/Time Comparisons http://arthurtw.github.io/2015/01/12/quick-comparison-nim-vs- rust.html Game of Life Rust Nim/boundChecks:on n=30000 with map print 1x 1.75x / 1.87x 1x=3.33s without map print 1x 1.15x / 1.72x 1x=0.78s http://togototo.wordpress.com/2013/08/23/benchmarks-round-two- parallel-go-rust-d-scala-and-nimrod/ Lang Compiler Speed/s %Fastest Res.Mem/KiB D ldc2 0.812 116.38% 26,536 C++ clang++ 0.945 100.00% 25,552 Nimrod clang 0.980 96.43% 25,932 C++ g++ 1.025 92.20% 25,532 Rust rustc 1.109 85.21% 47,708 Go 6g 1.184 79.81% 30,768 C clang 1.199 78.82% 25,796 Scala scala 1.228 76.95% 72,960 Go gccgo 2.710 34.87% 69,120 (excerpt)
  • 12. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Code Generation Compile-and-run a programme with nim c -r helloworld.nim what happens in the background is The compiler builds up an AST several compiler passes imported code libraries are merged into the AST Depending on the target code – assume C for now C code files are generated placed into the ./nimcache directory one C source per imported library The compiler starts a C compiler on the C sources optimised for GCC, Clang, Vcc (fallback: tiny CC) produces binary The binary is started
  • 13. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary NIM Tools Besides Compiler Rudimentary REPL Install: nimble install nrpl NIM embedded debugger endb, outdated GDB Compile NIM with line pragmas enabled Works fine for experienced C coder nim2c Convert C code to NIM code Handy tool, needs manual post-processing
  • 14. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Simple Functional Stuff code walk over misc/nimsrc.nim generic function: tail() generic T: any data type, normally inferred (see tests) openArray: ordered data items of all the same type type inference: .. else @[] pattern matching for C optimisation seq and openArray check generated code function: cnfValue() input cnfTable(): seq[] of string pairs (AVP list) add item that always matches: concat(@[(s,””)]) filter out first match: filterIt(it[0] == s) get first match: head() extract pair from sequence: [0], get value: [1]
  • 15. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary Compile Time Coding code walk over misc/nimsrc.nim template: nimSrcFilename() compiler support: instantiationInfo() info about code that invokes it, so it must be a macro/template functions: cnfTable() and cnfValue() extract AVP list from C header config.h from autoconf environment all compile time: slurp/staticRead, gorge/staticExec no OO support compiler quits if slurp() fails
  • 16. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary C Bindings code walk over zlib/zlib.nim C struct z_stream vs. NIM object TZStream NIM objects are GC controlled (tuples are not) compatibility types cstring, cint, cuint, etc. verify descriptor mapping: zstreamspecs.c cross compiling i386/x64/Linux/Windows etc. cstring, cint, probably struct alignments vary see import/binding in test section: tZstreamSpecs() doAssert() validity of descriptor mapping Zlib part is compiled all first using macros and compile time lists note the compile time path separator D rather than / operator or DirSep when host/target systems differ (eg. Posix/Windows)
  • 17. A modern tool for interfacing and creating bare bone C programmes Jordan Hrycaj Code Generation Tools For C My Problem: Embedded/Multi- Platform Compiler Stay with C Choosing NIM Working with NIM The Language NIM Workflow Examples Summary NIM Summary Produces code for target system/compiler C/C++/ObjC, JS Multi paradigm language extensible, DSL, accessible AST Targeting C Easy to interface C libraries Supports C cross compiling GDB aware for debugging Young language Small (but not too small) user group Documentation OK (possibly more examples needed) No big sponsors