<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rndthts.dev</title>
    <description>The latest articles on DEV Community by rndthts.dev (@rndthts).</description>
    <link>https://dev.to/rndthts</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F506731%2F7dce75a2-4c1f-47b7-baa5-45e6057cd943.png</url>
      <title>DEV Community: rndthts.dev</title>
      <link>https://dev.to/rndthts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rndthts"/>
    <language>en</language>
    <item>
      <title>From 'I Just Want a Piggy Bank' to a CNC-Ready Matrix Generator</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Sun, 08 Mar 2026 13:06:04 +0000</pubDate>
      <link>https://dev.to/rndthts/from-i-just-want-a-piggy-bank-to-a-cnc-ready-matrix-generator-4553</link>
      <guid>https://dev.to/rndthts/from-i-just-want-a-piggy-bank-to-a-cnc-ready-matrix-generator-4553</guid>
      <description>&lt;p&gt;Most of what I write about here is software development. But in my free time, I also like to mess around with a CO2 laser CNC machine.&lt;/p&gt;

&lt;p&gt;Recently I wanted to build a piggy bank (saving box) where you track progress by crossing out a number grid until you hit a target amount.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: Templates Exist Until They Don't
&lt;/h2&gt;

&lt;p&gt;Online templates work only for common totals. As soon as you need custom values, they break down:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You want a custom target (for example: 750, 1350, 4200)&lt;/li&gt;
&lt;li&gt;You want your own grid size (rows and columns based on your physical design)&lt;/li&gt;
&lt;li&gt;You want specific denominations (for example: 1, 2, 5, 10, 20, 50, 100)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As software engineers, we usually do not stop at "close enough." We build what we actually need.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Built: Piggy Bank Matrix Builder
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://rndthts.dev/tools/piggy-bank-matrix/" rel="noopener noreferrer"&gt;&lt;strong&gt;Piggy Bank Matrix Builder&lt;/strong&gt;&lt;/a&gt; generates a CNC-ready SVG matrix using your target sum, chosen numbers, and grid size.&lt;/p&gt;

&lt;h2&gt;
  
  
  What It Does
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Set a target sum&lt;/li&gt;
&lt;li&gt;Provide numbers (comma-separated) as denominations or any custom values&lt;/li&gt;
&lt;li&gt;Choose rows and columns&lt;/li&gt;
&lt;li&gt;Adjust a small-number bias to influence distribution&lt;/li&gt;
&lt;li&gt;Export as:

&lt;ul&gt;
&lt;li&gt;SVG (for laser/CNC)&lt;/li&gt;
&lt;li&gt;CSV (for further edits)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why This Was a Good Fit for Vibe Coding
&lt;/h2&gt;

&lt;p&gt;This was a perfect vibe coding project for me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance is not a hard requirement&lt;/li&gt;
&lt;li&gt;The software is not safety-critical&lt;/li&gt;
&lt;li&gt;I do not usually write in this language stack&lt;/li&gt;
&lt;li&gt;I can verify results quickly by checking totals, previewing SVG, and testing on real material&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That makes AI-assisted iteration fast and practical, with clear validation at each step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Video Version
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=h-mbZj34kWo" rel="noopener noreferrer"&gt;&lt;strong&gt;From Idea to Working Tool With Codex: Target Piggy Banks&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;

&lt;p&gt;If you build something with it (different currencies, weird denominations, alternative layouts), I would love to see it.&lt;/p&gt;

&lt;p&gt;Tool: &lt;a href="https://rndthts.dev/tools/piggy-bank-matrix/" rel="noopener noreferrer"&gt;https://rndthts.dev/tools/piggy-bank-matrix/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>vibecoding</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Just added audio to my YouTube welcome video. 
Bootstrapping my channel with code, step by step.
https://www.youtube.com/watch?v=5uWnukdw1QY</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Fri, 27 Feb 2026 13:13:49 +0000</pubDate>
      <link>https://dev.to/rndthts/just-added-audio-to-my-youtube-welcome-video-bootstrapping-my-channel-with-code-step-by-step-4ef9</link>
      <guid>https://dev.to/rndthts/just-added-audio-to-my-youtube-welcome-video-bootstrapping-my-channel-with-code-step-by-step-4ef9</guid>
      <description>&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body flex items-center justify-between"&gt;
        &lt;a href="https://www.youtube.com/watch?v=5uWnukdw1QY" rel="noopener noreferrer" class="c-link fw-bold flex items-center"&gt;
          &lt;span class="mr-2"&gt;youtube.com&lt;/span&gt;
          

        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Building a YouTube Channel in Public</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Wed, 25 Feb 2026 22:08:01 +0000</pubDate>
      <link>https://dev.to/rndthts/building-a-youtube-channel-in-public-4kn1</link>
      <guid>https://dev.to/rndthts/building-a-youtube-channel-in-public-4kn1</guid>
      <description>&lt;h2&gt;
  
  
  Bootstrapping a YouTube Channel With Code
&lt;/h2&gt;

&lt;p&gt;I decided to start a YouTube channel around one simple idea:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is actually possible with code?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This project is mostly for fun. I want to explore YouTube as a platform and experiment with areas of coding I haven’t touched before. Instead of overthinking it, I’m learning by building in public.&lt;/p&gt;

&lt;p&gt;And the first thing I’m building is the tooling for the channel itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Building the Tools First
&lt;/h2&gt;

&lt;p&gt;The first series is focused entirely on bootstrapping the channel.&lt;/p&gt;

&lt;p&gt;Instead of manually recording and editing everything, I’m creating small tools that help me produce better videos. I build the tools, use them to create videos, then improve them as I go.&lt;/p&gt;

&lt;p&gt;Over time, I plan to make these tools available to others.&lt;/p&gt;

&lt;p&gt;I’m also delegating as much of the coding as possible to AI tools. &lt;br&gt;
Part of this journey is exploring how far AI-assisted development can go, and where it still needs human input.&lt;/p&gt;
&lt;h3&gt;
  
  
  The First Attempt
&lt;/h3&gt;

&lt;p&gt;I wanted to create a welcome video with a clean terminal-style look.&lt;/p&gt;

&lt;p&gt;So I opened &lt;code&gt;iTerm&lt;/code&gt; and recorded myself typing the text.&lt;/p&gt;

&lt;p&gt;It looked… bad.&lt;br&gt;
Everything was off. The typing felt awkward. It wasn’t what I had imagined.&lt;/p&gt;

&lt;p&gt;Here’s that first version:&lt;br&gt;


  &lt;iframe src="https://www.youtube.com/embed/t4PPIEStaNU"&gt;
  &lt;/iframe&gt;


&lt;br&gt;
Instead of trying to fix the recording setup, I decided to build something better.&lt;/p&gt;
&lt;h3&gt;
  
  
  A Simple Terminal Typer App
&lt;/h3&gt;

&lt;p&gt;I wrote a small terminal-typer app that simulates typing and exports directly to video.&lt;/p&gt;

&lt;p&gt;That way I can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Control timing&lt;/li&gt;
&lt;li&gt;Replay scripts consistently&lt;/li&gt;
&lt;li&gt;Generate clean output every time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s how it looks like:&lt;br&gt;


  &lt;iframe src="https://www.youtube.com/embed/A3s_dYqoGqg"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;Using it, I recreated the welcome video, and it turned out much closer to what I originally wanted.&lt;/p&gt;

&lt;p&gt;Here is the current version of the welcome video:&lt;br&gt;


  &lt;iframe src="https://www.youtube.com/embed/vYnN9xU77no"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is just the beginning.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Right now, I’m focused on building the foundation.&lt;/p&gt;

&lt;p&gt;If you’d like to follow the journey, consider &lt;strong&gt;&lt;a href="https://www.youtube.com/@rndthts" rel="noopener noreferrer"&gt;subscribing&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
And if there’s something you’d like to see me build, let me know.&lt;/p&gt;

&lt;p&gt;If you watched the videos, you’ll notice they’re still silent.&lt;br&gt;
Adding audio is one of the next steps in this experiment.&lt;/p&gt;

</description>
      <category>youtube</category>
      <category>programming</category>
      <category>showdev</category>
    </item>
    <item>
      <title>A Quick Thing I Want to Share</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Thu, 04 Dec 2025 08:24:53 +0000</pubDate>
      <link>https://dev.to/rndthts/a-quick-thing-i-want-to-share-3jdc</link>
      <guid>https://dev.to/rndthts/a-quick-thing-i-want-to-share-3jdc</guid>
      <description>&lt;p&gt;Every now and then, I need a fast way to share a file from my computer or access something from another device on my network. &lt;/p&gt;

&lt;p&gt;Instead of setting up anything complicated, I just make use of Python’s &lt;code&gt;http.server&lt;/code&gt; module, which serves the current directory, &lt;br&gt;
or I provide &lt;code&gt;--directory /path/to/dir&lt;/code&gt; if I want to serve something else.&lt;/p&gt;

&lt;p&gt;Command:&lt;br&gt;
&lt;code&gt;python3 -m http.server&lt;/code&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>development</category>
    </item>
    <item>
      <title>Leadership Tip: Delegate Onboarding</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Tue, 11 Nov 2025 09:57:37 +0000</pubDate>
      <link>https://dev.to/rndthts/leadership-tip-delegate-onboarding-2moa</link>
      <guid>https://dev.to/rndthts/leadership-tip-delegate-onboarding-2moa</guid>
      <description>&lt;p&gt;As a team lead, you shouldn’t personally handle the full onboarding of new joiners. Instead, delegate this responsibility to others on your team and let them grow.&lt;/p&gt;

&lt;p&gt;Set up a good onboarding flow, check in now and then, and let the buddy handle the rest. Simple as that.&lt;/p&gt;

&lt;p&gt;Don’t forget to rotate that role so more people get the chance to develop those skills.&lt;/p&gt;

</description>
      <category>leadership</category>
      <category>development</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Tag your `TODO`s</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Mon, 10 Nov 2025 08:27:07 +0000</pubDate>
      <link>https://dev.to/rndthts/tag-your-todos-15e4</link>
      <guid>https://dev.to/rndthts/tag-your-todos-15e4</guid>
      <description>&lt;p&gt;Tip: Tag your &lt;code&gt;TODO&lt;/code&gt;s like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// TODO [PROJ-142]: replace legacy API client&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use a well-defined format so you can easily grep them, track them in your backlog, and even generate CI reports to keep an eye on unresolved &lt;code&gt;TODO&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;Enforce this during reviews, make sure every &lt;code&gt;TODO&lt;/code&gt; has a task id/number.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>development</category>
    </item>
    <item>
      <title>You’re not the real product owner if you can’t say, “F*ck it, let’s kill the product.” Until you have that power, you’re just managing, not owning.</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Thu, 06 Nov 2025 09:10:39 +0000</pubDate>
      <link>https://dev.to/rndthts/youre-not-the-real-product-owner-if-you-cant-say-fck-it-lets-kill-the-product-until-you-4oh9</link>
      <guid>https://dev.to/rndthts/youre-not-the-real-product-owner-if-you-cant-say-fck-it-lets-kill-the-product-until-you-4oh9</guid>
      <description></description>
    </item>
    <item>
      <title>Non-Technical Books For Developers</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Mon, 24 Feb 2025 08:13:58 +0000</pubDate>
      <link>https://dev.to/rndthts/non-technical-books-for-developers-chi</link>
      <guid>https://dev.to/rndthts/non-technical-books-for-developers-chi</guid>
      <description>&lt;p&gt;Mostly, I will write about the technical aspects of software development, but before I continue with that, I want to list some books that, in my opinion , every developer should read. Those books aren’t directly related to technical topics. This list is based on books I have read, and it just so happens that there are 10 of them. &lt;strong&gt;I didn’t chase that number.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since it’s still early in the year, you can still adjust your New Year’s resolutions and maybe even swap out some books on your reading list for some from the list below.&lt;/p&gt;

&lt;p&gt;I’ll write something about each book as a description, highlight, or opinion. So let’s start:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Who Moved My Cheese? by Spencer Johnson
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0x4mwdhywor9o7b76bnb.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0x4mwdhywor9o7b76bnb.jpg" alt="Who Moved My Cheese?" width="800" height="1267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An easy read, I listed it first because it’s about change, something especially relevant in today’s AI boom. As developers, we should embrace AI as just another tool and use it to our advantage.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Never Split the Difference by Chris Voss
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5vt8yhwixlrp68h50ko.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh5vt8yhwixlrp68h50ko.jpg" alt="Never Split the Difference" width="661" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From what I’ve seen so far, the majority of developers struggle with negotiations. We’re often afraid to even consider it, whether it’s about salary, perks, project choices, technology, or even getting more CI machines. That’s why this book is such a valuable read.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. How to Win Friends and Influence People by Dale Carnegie
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx89he9cze9xk4glb2yz9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx89he9cze9xk4glb2yz9.jpg" alt="How to Win Friends and Influence People" width="800" height="1242"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Communication is an underrated skill for developers, and this classic teaches how to work effectively with people.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. The Mythical Man-Month by Frederick P. Brooks Jr
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuggi4bqida34alw7db0h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuggi4bqida34alw7db0h.jpg" alt="The Mythical Man-Month" width="800" height="1208"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a goodreads review to explain the book:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I want to print many copies of this book.&lt;br&gt;&lt;br&gt;
I want to print many copies and roll them up.&lt;br&gt;&lt;br&gt;
I want to roll them up and take them to meetings with my clients.&lt;br&gt;&lt;br&gt;
I want to take them to meetings and hit them over the head repeatedly while screaming “more… than… 30… years… and you… still… don’t… understand… anything… stop… making… me… write… bad… software…!"&lt;br&gt;&lt;br&gt;
Seriously.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  5. The Phoenix Project by Gene Kim, Kevin Behr, and George Spafford
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0cblroow0ejctcj5jqmh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0cblroow0ejctcj5jqmh.jpg" alt="The Phoenix Project" width="677" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This book illustrates DevOps principles through storytelling, highlighting collaboration, efficiency, and continuous improvement. While reading, I found myself mapping the characters to real people.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. The 7 Habits of Highly Effective People by Stephen R. Covey
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjykzgfm7j7q1lh5rxfl.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frjykzgfm7j7q1lh5rxfl.jpg" alt="The 7 Habits of Highly Effective People" width="638" height="1000"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’ve ever heard the saying, “Listen to understand, not to reply,” but don’t know where it comes from, read this book. I revisit it every 2–3 years, and each time, I find new insights.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. The Five Dysfunctions of a Team by Patrick Lencioni
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9tsver8m60l8mea6bh8n.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9tsver8m60l8mea6bh8n.jpeg" alt="The Five Dysfunctions of a Team" width="184" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Primarily a leadership book, but if you work in a team, I think it's definitely worth reading.&lt;/p&gt;

&lt;h3&gt;
  
  
  8. Thinking, Fast and Slow by Daniel Kahneman
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkggxnsbkp4xh2qrpeccp.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkggxnsbkp4xh2qrpeccp.jpeg" alt="Thinking, Fast and Slow" width="183" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This book explores how we make decisions and the cognitive biases that shape our thinking.&lt;/p&gt;

&lt;h3&gt;
  
  
  9. Hooked: How to Build Habit-Forming Products by Nir Eyal
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw92lv290uquc0qc8tl50.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw92lv290uquc0qc8tl50.png" alt="Hooked: How to Build Habit-Forming Products" width="183" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I placed this book after Thinking, Fast and Slow because it builds on many of its ideas, applying them to real-world products.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. Animal Farm by George Orwell
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fodz1kpvd5a85omh7u937.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fodz1kpvd5a85omh7u937.jpg" alt="Animal Farm" width="800" height="1304"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All animals are equal, but some animals are more equal than others.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s it for now, until next time, happy reading!!!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>books</category>
      <category>development</category>
    </item>
    <item>
      <title>A Declaration of Intent: Why I'm Writing</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Tue, 18 Feb 2025 11:37:19 +0000</pubDate>
      <link>https://dev.to/rndthts/a-declaration-of-intent-why-im-writing-4c5j</link>
      <guid>https://dev.to/rndthts/a-declaration-of-intent-why-im-writing-4c5j</guid>
      <description>&lt;p&gt;I should probably have started with this post before writing anything else, but who cares? Probably no one will ever read this, and that’s perfectly fine. I’m not writing this for others. This blog is just my personal collection of notes and documentation; something I should have been doing for years but never did.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;p&gt;I wrote my first “Hello, World!” program in 8th grade using QBasic, and I was hooked. After a few small programs, I started experimenting with Visual Basic. When you’re new to programming; or anything, really, you want something visual. Visuals sell the product. There’s that saying: &lt;strong&gt;Rims sell the car.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frndthts.dev%2Fimages%2Frims.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frndthts.dev%2Fimages%2Frims.jpg" alt="Rims" width="800" height="370"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@grebenyev?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Vlad Grebenyev&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/close-up-photography-of-car-wheel-7eCBiZgyr4E?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, my friend and I created our first GUI program in Visual Basic: a periodic table of elements. Visually, it worked. The code? Absolutely terrible. Imagine spaghetti code, multiply it by 100, then multiply that by a &lt;code&gt;rand(5, 10)&lt;/code&gt;. It was that bad. I don’t have the code anymore, and that’s one of my motivations for writing this blog. But I remember it vividly: massive if-else chains for every element. Yes, for every single element, in multiple places.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frndthts.dev%2Fimages%2Fspaghetti.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frndthts.dev%2Fimages%2Fspaghetti.jpg" alt="Spaghetti" width="800" height="533"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@neonbites?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Thomas Wavid Johns&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/a-pile-of-spaghetti-sitting-on-top-of-a-wooden-table-IReDRPiM-KY?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Who would have thought that, years later, someone would actually pay me to write code professionally?&lt;/p&gt;

&lt;p&gt;Fast forward 20 years. I’ve been writing code professionally for 12 or 13 years now. Hundreds of small projects, prototypes, interesting applications of design patterns, and problem-solving insights - &lt;strong&gt;gone&lt;/strong&gt;. Never documented. Lost to time.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fun fact: almost all the software I’ve written professionally is terminal-based, often running in the background without user interaction. Turns out, if you go fast enough, no one even notices the rims.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What Will Be Covered
&lt;/h2&gt;

&lt;p&gt;From now on, I’m going to try to document as much as possible. I’ll cover topics I find interesting, reimplement old projects, explore tools, and maybe even suggest potential projects.&lt;/p&gt;

&lt;p&gt;Here are some of the things I want to write about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Compilation steps&lt;/strong&gt; – understanding what happens when you compile code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dear ImGui&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ugly and spaghetti code&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;File formats&lt;/strong&gt; – exploring Mach-O and ELF&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linkers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debuggers&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Books&lt;/strong&gt; – reviews, short or long, depending on the book&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Package managers&lt;/strong&gt; – how they work and why they matter&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Meson build system&lt;/strong&gt; – why you should care&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Projects that are good for beginners&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test-Driven Development (TDD)&lt;/strong&gt; – is it worth the hype?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production-ready Hello World&lt;/strong&gt; – making even the simplest programs production-grade&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LLVM passes and codegen&lt;/strong&gt; – digging into compiler internals&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Assembly and syscalls&lt;/strong&gt; – low-level programming insights&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OpenCV experiments&lt;/strong&gt; – exploring computer vision with POCs &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;OpenGL experiments&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exploring C++ projects&lt;/strong&gt; – possibly a series, maybe weekly :) &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exception handling&lt;/strong&gt; – what happens under the hood&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Random industry thoughts&lt;/strong&gt; – because this field is wild&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Agile, Scrum, and Leadership&lt;/strong&gt; – reflections on the way software teams work&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Different personas in software development and management teams&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Other rndthts&lt;/strong&gt; …&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This list will evolve over time, but the goal remains the same: document things I find useful, interesting, or just worth preserving. Also, the order of topics is completely random, and it’s far from complete.&lt;/p&gt;

&lt;p&gt;If, by any chance, someone else stumbles upon this blog and actually finds it useful, well, that’s a happy accident! But really, this is just me making up for years of not writing things down. Let’s see where this random ride takes us.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>writing</category>
    </item>
    <item>
      <title>Compilation Process - Compilation</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Sun, 09 Feb 2025 12:01:44 +0000</pubDate>
      <link>https://dev.to/rndthts/compilation-process-compilation-dgh</link>
      <guid>https://dev.to/rndthts/compilation-process-compilation-dgh</guid>
      <description>&lt;p&gt;Cover Photo by &lt;a href="https://unsplash.com/@bernardhermant?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Bernard Hermant&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/person-hands-with-green-paint-bbuTpY447gA?utm_content=creditCopyText&amp;amp;utm_medium=referral&amp;amp;utm_source=unsplash" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the last blog post, we covered the &lt;a href="https://rndthts.dev/posts/c_cpp_compilation_process/" rel="noopener noreferrer"&gt;Preprocessing stage&lt;/a&gt;. Now, it's time to dive into the Compilation stage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy27enz0tx7s1i1rbj02h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy27enz0tx7s1i1rbj02h.png" alt="Compilation" width="800" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Compilation?
&lt;/h2&gt;

&lt;p&gt;Compilation is the process of converting preprocessed source code (in our case, C or C++ code) into assembly code. This phase is handled by the compiler, which performs various checks and optimizations before generating low-level code.&lt;/p&gt;

&lt;p&gt;Unlike the preprocessing phase, which merely modifies the code textually, compilation involves deeper analysis and transformation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; To stop the compilation process after the compilation phase, use the -S flag:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc/clang &lt;span class="nt"&gt;-S&lt;/span&gt; main.c &lt;span class="nt"&gt;-o&lt;/span&gt; main.s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's explore this with an example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;foo.h&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;main.c&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key Stages of Compilation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Lexical Analysis (Tokenization)
&lt;/h3&gt;

&lt;p&gt;The preprocessed code is broken down into tokens. For example, the line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int a = 5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will be tokenized into: &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This stage eliminates whitespace and detects unknown symbols. For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int    a    = 5  ;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;will still end up with the same tokens &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;=&lt;/code&gt;, &lt;code&gt;5&lt;/code&gt; and &lt;code&gt;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;However, this will cause an error:&lt;br&gt;
&lt;code&gt;int a = 5 @ 3;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="nl"&gt;error:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="sc"&gt;';'&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;declaration&lt;/span&gt;
    &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;            &lt;span class="o"&gt;^&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Syntax Analysis (Parsing)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Tokens are grouped based on grammar rules to form an Abstract Syntax Tree (AST).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The AST represents the hierarchical syntactic structure of the code, abstracting unnecessary details like semicolons or parentheses.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This phase ensures that the code structure follows language syntax.&lt;br&gt;
Example: &lt;br&gt;
&lt;code&gt;int a = 5 // Missing the ;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="nl"&gt;error:&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="sc"&gt;';'&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt; &lt;span class="n"&gt;end&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;declaration&lt;/span&gt;
    &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;// Missing the ;&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;            &lt;span class="o"&gt;^&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Semantic Analysis
&lt;/h3&gt;

&lt;p&gt;This phase checks for logical correctness:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ensures variables are declared before use.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// int a = 5;&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt; &lt;span class="nl"&gt;error:&lt;/span&gt; &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;undeclared&lt;/span&gt; &lt;span class="n"&gt;identifier&lt;/span&gt; &lt;span class="sc"&gt;'a'&lt;/span&gt;
    &lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;                     &lt;span class="o"&gt;^&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Ensures type compatibility (e.g., no assigning a string to an integer).
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int a = "hello";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="nl"&gt;error:&lt;/span&gt; &lt;span class="n"&gt;incompatible&lt;/span&gt; &lt;span class="n"&gt;pointer&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;integer&lt;/span&gt; &lt;span class="n"&gt;conversion&lt;/span&gt; &lt;span class="n"&gt;initializing&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;an&lt;/span&gt; &lt;span class="n"&gt;expression&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;Wint&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;conversion&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;   &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="o"&gt;|&lt;/span&gt;       &lt;span class="o"&gt;^&lt;/span&gt;   &lt;span class="o"&gt;~~~~~~~&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. IR - Intermediate Representation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The compiler converts the AST into an Intermediate Representation (IR) that is platform-independent.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;IR serves as a bridge between high-level source code and low-level machine code, enabling optimizations.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In future posts, we'll cover this topic in depth, along with various optimizations. For now, let's see how to instruct the compiler to emit this form.&lt;/p&gt;

&lt;p&gt;To generate LLVM IR using clang:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;clang main.c &lt;span class="nt"&gt;-S&lt;/span&gt; &lt;span class="nt"&gt;-emit-llvm&lt;/span&gt; &lt;span class="nt"&gt;-o&lt;/span&gt; main.ll&lt;span class="sb"&gt;`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unoptimized Version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight lua"&gt;&lt;code&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ModuleID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'main.c'&lt;/span&gt;
&lt;span class="n"&gt;source_filename&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"main.c"&lt;/span&gt;
&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="n"&gt;datalayout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"e-m:o-i64:64-i128:128-n32:64-S128-Fn32"&lt;/span&gt;
&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="n"&gt;triple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"arm64-apple-macosx14.0.0"&lt;/span&gt;

&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;Function&lt;/span&gt; &lt;span class="n"&gt;Attrs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;noinline&lt;/span&gt; &lt;span class="n"&gt;nounwind&lt;/span&gt; &lt;span class="n"&gt;optnone&lt;/span&gt; &lt;span class="n"&gt;ssp&lt;/span&gt; &lt;span class="n"&gt;uwtable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;define&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="n"&gt;noundef&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alloca&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mul&lt;/span&gt; &lt;span class="n"&gt;nsw&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;Function&lt;/span&gt; &lt;span class="n"&gt;Attrs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;noinline&lt;/span&gt; &lt;span class="n"&gt;nounwind&lt;/span&gt; &lt;span class="n"&gt;optnone&lt;/span&gt; &lt;span class="n"&gt;ssp&lt;/span&gt; &lt;span class="n"&gt;uwtable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;define&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alloca&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alloca&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alloca&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;load&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ptr&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;align&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="n"&gt;noundef&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;nsw&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;
  &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;declare&lt;/span&gt; &lt;span class="n"&gt;i32&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;attributes&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Compiler Optimizations
&lt;/h3&gt;

&lt;p&gt;The compiler optimizes the IR before producing assembly code. Common optimizations include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function Inlining&lt;/li&gt;
&lt;li&gt;Dead Code Elimination&lt;/li&gt;
&lt;li&gt;Loop Unrolling&lt;/li&gt;
&lt;li&gt;Loop Invariant Code Motion&lt;/li&gt;
&lt;li&gt;Merge Functions&lt;/li&gt;
&lt;li&gt;etc ...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We'll explore these in future posts.&lt;/p&gt;

&lt;p&gt;Final result, arm64 assembly&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;linux&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .text
    .file   "main.c"
    .globl  square                          // -- Begin function square
    .p2align    2
    .type   square,@function
square:                                 // @square
    .cfi_startproc
// %bb.0:
    sub sp, sp, #16
    .cfi_def_cfa_offset 16
    str w0, [sp, #12]
    ldr w8, [sp, #12]
    ldr w9, [sp, #12]
    mul w0, w8, w9
    add sp, sp, #16
    .cfi_def_cfa_offset 0
    ret
.Lfunc_end0:
    .size   square, .Lfunc_end0-square
    .cfi_endproc
                                        // -- End function
    .globl  main                            // -- Begin function main
    .p2align    2
    .type   main,@function
main:                                   // @main
    .cfi_startproc
// %bb.0:
    sub sp, sp, #32
    .cfi_def_cfa_offset 32
    stp x29, x30, [sp, #16]             // 16-byte Folded Spill
    add x29, sp, #16
    .cfi_def_cfa w29, 16
    .cfi_offset w30, -8
    .cfi_offset w29, -16
    stur    wzr, [x29, #-4]
    mov w8, #5                          // =0x5
    str w8, [sp, #8]
    bl  foo
    str w0, [sp, #4]
    ldr w8, [sp, #4]
    str w8, [sp]                        // 4-byte Folded Spill
    ldr w0, [sp, #8]
    bl  square
    ldr w8, [sp]                        // 4-byte Folded Reload
    add w0, w8, w0
    .cfi_def_cfa wsp, 32
    ldp x29, x30, [sp, #16]             // 16-byte Folded Reload
    add sp, sp, #32
    .cfi_def_cfa_offset 0
    .cfi_restore w30
    .cfi_restore w29
    ret
.Lfunc_end1:
    .size   main, .Lfunc_end1-main
    .cfi_endproc
                                        // -- End function
    .ident  "Ubuntu clang version 18.1.3 (1ubuntu1)"
    .section    ".note.GNU-stack","",@progbits
    .addrsig
    .addrsig_sym square
    .addrsig_sym foo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;macos:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    .section    __TEXT,__text,regular,pure_instructions
    .build_version macos, 14, 0 sdk_version 14, 4
    .globl  _square                         ; -- Begin function square
    .p2align    2
_square:                                ; @square
    .cfi_startproc
; %bb.0:
    sub sp, sp, #16
    .cfi_def_cfa_offset 16
    str w0, [sp, #12]
    ldr w8, [sp, #12]
    ldr w9, [sp, #12]
    mul w0, w8, w9
    add sp, sp, #16
    ret
    .cfi_endproc
                                        ; -- End function
    .globl  _main                           ; -- Begin function main
    .p2align    2
_main:                                  ; @main
    .cfi_startproc
; %bb.0:
    sub sp, sp, #32
    stp x29, x30, [sp, #16]             ; 16-byte Folded Spill
    add x29, sp, #16
    .cfi_def_cfa w29, 16
    .cfi_offset w30, -8
    .cfi_offset w29, -16
    stur    wzr, [x29, #-4]
    mov w8, #5                          ; =0x5
    str w8, [sp, #8]
    bl  _foo
    str w0, [sp, #4]
    ldr w8, [sp, #4]
    str w8, [sp]                        ; 4-byte Folded Spill
    ldr w0, [sp, #8]
    bl  _square
    ldr w8, [sp]                        ; 4-byte Folded Reload
    add w0, w8, w0
    ldp x29, x30, [sp, #16]             ; 16-byte Folded Reload
    add sp, sp, #32
    ret
    .cfi_endproc
                                        ; -- End function
.subsections_via_symbols

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this post! In the next one, we'll cover the &lt;strong&gt;Assembler&lt;/strong&gt; phase.&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Compilation Process - Preprocessing</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Wed, 05 Feb 2025 20:13:59 +0000</pubDate>
      <link>https://dev.to/rndthts/compilation-process-preprocessing-3ekk</link>
      <guid>https://dev.to/rndthts/compilation-process-preprocessing-3ekk</guid>
      <description>&lt;p&gt;The compilation process consists of four key steps, as shown in the image below. In this series of blog posts, we’ll explore each of those steps, highlighting the most important aspects and sharing interesting insights along the way. 😊&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmtwudcu4q0g8798sqm3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjmtwudcu4q0g8798sqm3.png" alt="Compilation Steps" width="800" height="187"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Preprocessing (c/cpp -&amp;gt; .i)
&lt;/h2&gt;

&lt;p&gt;The first phase is preprocessing. The input is a .c or .cpp file, and the output is a .i file. Let’s dive into what happens during this stage. &lt;br&gt;
To illustrate, we’ll use the following example:&lt;/p&gt;
&lt;h3&gt;
  
  
  Example 1
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;foo.h&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is a comment in header file&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;main.c&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="c1"&gt;// We have a macro&lt;/span&gt;
&lt;span class="cp"&gt;#define SQUARE(x) (x * x)
&lt;/span&gt;
&lt;span class="cp"&gt;#define PI 3.14
&lt;/span&gt;
&lt;span class="cm"&gt;/* Our main function
 * with multi-line comment
 */&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;SQUARE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PI&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The compilation process can be stopped at any step. In this case, we want to stop it after preprocessing, and to do that, we use the &lt;code&gt;-E&lt;/code&gt; flag.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;gcc &lt;span class="nt"&gt;-E&lt;/span&gt; main.c &lt;span class="nt"&gt;-o&lt;/span&gt; main.i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;main.i&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;# 1 "main.c"
# 1 "&amp;lt;built-in&amp;gt;" 1
# 1 "&amp;lt;built-in&amp;gt;" 3
# 418 "&amp;lt;built-in&amp;gt;" 3
# 1 "&amp;lt;command line&amp;gt;" 1
# 1 "&amp;lt;built-in&amp;gt;" 2
# 1 "main.c" 2
# 1 "./foo.h" 1
&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;# 2 "main.c" 2
# 11 "main.c"
&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;area&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's observe the result:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Header File Inclusion - The preprocessor replaces &lt;code&gt;#include&lt;/code&gt; directives with the actual contents of the specified header files.&lt;/li&gt;
&lt;li&gt;Line Control Statements (# Directives)&lt;/li&gt;
&lt;li&gt;Comments are remove ( single and multi line)&lt;/li&gt;
&lt;li&gt;Macro Expansion (#define) - The preprocessor replaces macros with their defined values. 

&lt;ul&gt;
&lt;li&gt;  &lt;code&gt;int result = foo() + SQUARE(3);&lt;/code&gt; -&amp;gt; &lt;code&gt;int result = foo() + (3 * 3);&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt; &lt;code&gt;float area = PI * 5 * 5;&lt;/code&gt; -&amp;gt; &lt;code&gt;float area = 3.14 * 5 * 5;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example 2
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifdef DEBUG
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#endif
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cp"&gt;#ifdef DEBUG
&lt;/span&gt;    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Debug mode is enabled.&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="cp"&gt;#endif
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Conditional Compilation (&lt;code&gt;#ifdef&lt;/code&gt;, &lt;code&gt;#ifndef&lt;/code&gt;, &lt;code&gt;#endif&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since &lt;code&gt;DEBUG&lt;/code&gt; was not defined, the output after preprocessing looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A well-known application of conditional compilation is &lt;strong&gt;include guards&lt;/strong&gt;. Consider this simplified example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without include guards, the output after preprocessing might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;# 2 "main_multi.c" 2
# 1 "./foo.h" 1
&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;# 3 "main_multi.c" 2
# 1 "./foo.h" 1
&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;# 4 "main_multi.c" 2
# 1 "./foo.h" 1
&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="cp"&gt;# 5 "main_multi.c" 2
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, this is a silly example, but in larger projects, this issue can happen unintentionally. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"foo.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"bar.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"tar.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If both &lt;code&gt;bar.h&lt;/code&gt; and &lt;code&gt;tar.h&lt;/code&gt; also include &lt;code&gt;foo.h&lt;/code&gt;, the compiler processes &lt;code&gt;foo.h&lt;/code&gt; multiple times, which can lead to redefinition errors.&lt;/p&gt;

&lt;p&gt;To prevent multiple inclusions, we use &lt;strong&gt;include guards&lt;/strong&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#ifndef FOO_H
#define FOO_H
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;void&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cp"&gt;#endif // FOO_H
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 3
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="cp"&gt;#define CONCAT(a, b) a##b
&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;CONCAT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Expands to int myVar = 10;&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File: %s, Line: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;__LINE__&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Ignored the stdio.h&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;myVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"File: %s, Line: %d&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"main.c"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Token Pasting (##) and Stringizing (#)&lt;/li&gt;
&lt;li&gt;Including Built-in Macros in our example &lt;code&gt;__FILE__&lt;/code&gt; and &lt;code&gt;__LINE__&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The next step in the process is &lt;strong&gt;compilation&lt;/strong&gt;. Stay tuned for the next post!&lt;/p&gt;

</description>
      <category>c</category>
      <category>cpp</category>
      <category>beginners</category>
      <category>compiling</category>
    </item>
    <item>
      <title>LLVM - Writing your first pass</title>
      <dc:creator>rndthts.dev</dc:creator>
      <pubDate>Thu, 03 Aug 2023 23:14:41 +0000</pubDate>
      <link>https://dev.to/rndthts/llvm-writing-your-first-pass-59ao</link>
      <guid>https://dev.to/rndthts/llvm-writing-your-first-pass-59ao</guid>
      <description>&lt;h2&gt;
  
  
  Writing your first pass with LLVM
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is a pass?&lt;/strong&gt; Simply put, it is a thing that analyzes or transforms LLVM IR inputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What are we going to use?&lt;/strong&gt; We will use &lt;code&gt;opt&lt;/code&gt;. What is &lt;code&gt;opt&lt;/code&gt;?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The opt command is the modular LLVM optimizer and analyzer. It takes LLVM source files as input, runs the specified optimizations or analyses on it, and then outputs the optimized file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Felvisoric.com%2Fimages%2Fopt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Felvisoric.com%2Fimages%2Fopt.png" alt="opt" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is so-called &lt;code&gt;middle end&lt;/code&gt;. The input to the opt is &lt;code&gt;LLVM IR&lt;/code&gt; and the output is also&lt;code&gt;LLVM IR&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Before we start writing our first pass, we need llvm. We can install it or build it ourselves. I used LLVM 16.&lt;/p&gt;

&lt;p&gt;We can install llvm using:&lt;code&gt;brew install llvm&lt;/code&gt; on MacOS or &lt;code&gt;apt install llvm&lt;/code&gt; on Ubuntu. If you decide to build llvm, you can follow the official instructions here:&lt;a href="https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm" rel="noopener noreferrer"&gt;https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From this point I will refere to llvm installation directory as &lt;code&gt;LLVM_PATH&lt;/code&gt;. To easier follow the rest of the stuff, you can just export &lt;code&gt;LLVM_PATH&lt;/code&gt;In my case it was &lt;code&gt;export LLVM_PATH=/opt/homebrew/opt/llvm@16/&lt;/code&gt;To test this, we can invoke:&lt;code&gt;$LLVM_PATH/bin/opt --version&lt;/code&gt; and this should return something similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Homebrew LLVM version 16.0.6
  Optimized build.
  Default target: arm64-apple-darwin22.5.0
  Host CPU: apple-m1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s start from the point of how are we going to invoke &lt;code&gt;opt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$LLVM_PATH/bin/opt -load-pass-plugin hello.dylib -passes=first -disable-output path/to/input.ll
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will have to provide our plugin, where our pass (or multiple passes) is defined, and a list of the passes. The plugin is a shared library. &lt;code&gt;opt&lt;/code&gt; will load our plugin using &lt;code&gt;dlopen&lt;/code&gt; and it will try to find a predefined function name in that plugin to set up the plugin. In this case, it will look for &lt;code&gt;llvmGetPassPluginInfo&lt;/code&gt; function to initialize the plugin.&lt;/p&gt;

&lt;p&gt;From the official documentation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/// The public entry point for a pass plugin.
///
/// When a plugin is loaded by the driver, it will call this entry point to
/// obtain information about this plugin and about how to register its passes.
/// This function needs to be implemented by the plugin, see the example below:
///
/// 
/// extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
/// llvmGetPassPluginInfo() {
/// return {
/// LLVM_PLUGIN_API_VERSION, "MyPlugin", "v0.1", [](PassBuilder &amp;amp;PB) { ... }
/// };
/// }
/// 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means that our plugin should implement this function. Let’s quickly check the return type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;PassPluginLibraryInfo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;uint32_t&lt;/span&gt; &lt;span class="n"&gt;APIVersion&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PluginName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PluginVersion&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;/// The callback for registering plugin passes with a \c PassBuilder&lt;/span&gt;
  &lt;span class="c1"&gt;/// instance&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;RegisterPassBuilderCallbacks&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;PassBuilder&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the minimal version of our plugin looks like this: first.cpp:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"llvm/Passes/PassBuilder.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"llvm/Passes/PassPlugin.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"llvm/Support/raw_ostream.h"&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="n"&gt;LLVM_ATTRIBUTE_WEAK&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassPluginLibraryInfo&lt;/span&gt;
&lt;span class="nf"&gt;llvmGetPassPluginInfo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;LLVM_PLUGIN_API_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"First"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassBuilder&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;PB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Register pass builder callback :)&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
          &lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can build it from command line using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$LLVM_PATH/bin/clang++ -std=c++17 first.cpp -shared -fno-rtti -fno-exceptions `$LLVM_PATH/bin/llvm-config --cppflags --ldflags --system-libs --libs core` -o first
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before invoking &lt;code&gt;opt&lt;/code&gt;, we need the input &lt;code&gt;LLVM IR&lt;/code&gt; file. In our case, We will create a simple c source file and compile it to &lt;code&gt;LLVM IR&lt;/code&gt;.&lt;code&gt;foo.c&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compile it using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$LLVM_PATH/bin/clang -c -S -emit-llvm foo.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run opt with this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$LLVM_PATH/bin/opt -load-pass-plugin first -passes=hello -disable-output foo.ll
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, we should get something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Register pass builder callback :)
/opt/homebrew/opt/llvm@16/bin/opt: unknown pass name 'hello'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can update the implementation of &lt;code&gt;llvmGetPassPluginInfo&lt;/code&gt; to register our pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;extern&lt;/span&gt; &lt;span class="s"&gt;"C"&lt;/span&gt; &lt;span class="n"&gt;LLVM_ATTRIBUTE_WEAK&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassPluginLibraryInfo&lt;/span&gt;
&lt;span class="nf"&gt;llvmGetPassPluginInfo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;LLVM_PLUGIN_API_VERSION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"First"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"0.1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassBuilder&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;PB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;PB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;registerPipelineParsingCallback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="p"&gt;[](&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;StringRef&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionPassManager&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;FPM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                   &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ArrayRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassBuilder&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PipelineElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;FPM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addPass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HelloPass&lt;/span&gt;&lt;span class="p"&gt;{});&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                  &lt;span class="p"&gt;}&lt;/span&gt;
                  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;});&lt;/span&gt;
          &lt;span class="p"&gt;}};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;registerPipelineParsingCallback&lt;/code&gt; has several overloads, but for now, we are interested in function pass, as you can see from the signature of provided lambda.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;StringRef&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionPassManager&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;FPM&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;                &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;ArrayRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassBuilder&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PipelineElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the provided pass name is &lt;code&gt;hello&lt;/code&gt;, we instantiate &lt;code&gt;HelloPass&lt;/code&gt;. So, let’s see the definition of the pass.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;HelloPass&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PassInfoMixin&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;HelloPass&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PreservedAnalyses&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Function&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                              &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;FunctionAnalysisManager&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Function name: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;PreservedAnalyses&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="nf"&gt;isRequired&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pass is simple, it will run on functions and print function names. We added our pass to the &lt;code&gt;llvm::FunctionPassManager&lt;/code&gt; so that the manager will invoke &lt;code&gt;run&lt;/code&gt; on our pass, providing us &lt;code&gt;llvm::Function&lt;/code&gt; and &lt;code&gt;llvm::FunctionAnalysisManager&lt;/code&gt; In our case we do not even use &lt;code&gt;llvm::FunctionAnalysisManager&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We do not change anything when our pass is invoked, so we return &lt;code&gt;llvm::PreservedAnalyses::all()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we now compile again our plugin&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$LLVM_PATH/bin/clang++ -std=c++17 first.cpp -shared -fno-rtti -fno-exceptions `$LLVM_PATH/bin/llvm-config --cppflags --ldflags --system-libs --libs core` -o first
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;run &lt;code&gt;opt&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$LLVM_PATH/bin/opt -load-pass-plugin first -passes=hello -disable-output foo.ll
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function name: foo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let’s update our pass to print the number of arguments as well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;llvm&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;errs&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="s"&gt;"Arg size: "&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;F&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;arg_size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;also, let’s add more functions to our &lt;code&gt;foo.c&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;short&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;tar&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compile our plugin! Compile &lt;code&gt;foo.c&lt;/code&gt; to &lt;code&gt;LLVM IR&lt;/code&gt;. Run &lt;code&gt;opt&lt;/code&gt;, we will get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function name: foo
Function Arg size: 2
Function name: bar
Function Arg size: 3
Function name: tar
Function Arg size: 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cpp</category>
      <category>llvm</category>
      <category>compiler</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
