<?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>Forem: Annurdien Rasyid</title>
    <description>The latest articles on Forem by Annurdien Rasyid (@annurdien).</description>
    <link>https://forem.com/annurdien</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%2F639076%2F0c66be52-93c5-43b1-8bae-831912de4066.png</url>
      <title>Forem: Annurdien Rasyid</title>
      <link>https://forem.com/annurdien</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/annurdien"/>
    <language>en</language>
    <item>
      <title>What makes ARM... ARM?</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Tue, 18 Nov 2025 08:09:44 +0000</pubDate>
      <link>https://forem.com/annurdien/what-makes-arm-arm-om0</link>
      <guid>https://forem.com/annurdien/what-makes-arm-arm-om0</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;You're probably reading this on an ARM chip. No, really. If it's a phone, 100%. A new Mac? Yep. A Windows laptop? Uh oh, look out, Intel. Let's fall down the rabbit hole of what this 'ARM' thing actually is.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's one of those things that’s everywhere, but &lt;strong&gt;no one&lt;/strong&gt; ever &lt;em&gt;really&lt;/em&gt; explains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The First Rabbit Hole: What &lt;em&gt;is&lt;/em&gt; ARM?&lt;/li&gt;
&lt;li&gt;The Big Showdown: RISC vs. CISC&lt;/li&gt;
&lt;li&gt;
The Heart of the Matter: The Instruction Set

&lt;ul&gt;
&lt;li&gt;The Load/Store Philosophy (aka "The No Touchy Rule")&lt;/li&gt;
&lt;li&gt;Conditional Love (and Execution)&lt;/li&gt;
&lt;li&gt;The Squeezy Boy: The Thumb Instruction Set&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The M1-llion Dollar Question: Why Did Apple Switch?&lt;/li&gt;

&lt;li&gt;Lessons from the Rabbit Hole&lt;/li&gt;

&lt;li&gt;Next Steps&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  TL;DR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ARM isn't a chip:&lt;/strong&gt; It's a &lt;em&gt;recipe&lt;/em&gt; (an architecture) that companies like Apple, Samsung, and Qualcomm license to make their &lt;em&gt;own&lt;/em&gt; chips.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;It's built on a philosophy called RISC&lt;/strong&gt; (Reduced Instruction Set Computer). This means it uses a small, simple set of instructions. Think of it as the Marie Kondo of CPUs. ✨&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The alternative is CISC&lt;/strong&gt; (Complex Instruction Set Computer), which is what your typical Intel or AMD (x86) processor is. It's the "throw everything in a drawer" approach. 🌀&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The key difference is the "Load/Store" model.&lt;/strong&gt; ARM instructions can't futz with memory directly. They have to &lt;code&gt;LDR&lt;/code&gt; (Load) data into a register, do the math, and &lt;code&gt;STR&lt;/code&gt; (Store) it back. This seems slow but is &lt;em&gt;insanely&lt;/em&gt; fast in practice.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apple switched for power, efficiency, and... control.&lt;/strong&gt; They wanted to build the whole "System on a Chip" (SoC) their own way, and Intel's x86 recipe was holding them back.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The First Rabbit Hole: What &lt;em&gt;is&lt;/em&gt; ARM?
&lt;/h2&gt;

&lt;p&gt;This was the first thing that psyched me out. I thought ARM was a company that &lt;em&gt;made&lt;/em&gt; chips, like Intel.&lt;/p&gt;

&lt;p&gt;Nope!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    /\_____/\
   /  o   o  \
  ( ==  ^  == )
   )         (
  (           )
 ( (  )   (  ) )
(__(__)___(__)__)

(Baaa!)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ARM (the company) is more like an architectural firm. They design blueprints. The big one is the &lt;strong&gt;ARM Instruction Set Architecture (ISA)&lt;/strong&gt;. This is, quite literally, the &lt;em&gt;language&lt;/em&gt; that the processor speaks.&lt;/p&gt;

&lt;p&gt;They license these blueprints out.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Apple&lt;/strong&gt; 🍎 takes the blueprint and &lt;em&gt;wildly&lt;/em&gt; modifies it to create their M-series monsters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Qualcomm&lt;/strong&gt; licenses it to build their Snapdragon chips for your Android phone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Samsung&lt;/strong&gt; licenses it for their Exynos chips.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Raspberry Pi&lt;/strong&gt; uses a chip from Broadcom, who... you guessed it... licensed the ARM design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is &lt;em&gt;completely&lt;/em&gt; different from the x86 world. Intel and AMD own their x86 designs. They design 'em, they build 'em, they sell 'em. You can't license the "Core i9" blueprint and make your own. This licensing model is ARM's first superpower.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Big Showdown: RISC vs. CISC
&lt;/h2&gt;

&lt;p&gt;Okay, so ARM's blueprint is for a &lt;strong&gt;RISC&lt;/strong&gt; chip. What the heck does that mean?&lt;/p&gt;

&lt;p&gt;It stands for &lt;strong&gt;Reduced Instruction Set Computer&lt;/strong&gt;. Its arch-nemesis is &lt;strong&gt;CISC&lt;/strong&gt;, the &lt;strong&gt;Complex Instruction Set Computer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is the core, &lt;strong&gt;A Number 1&lt;/strong&gt;, most important difference.&lt;/p&gt;

&lt;p&gt;Your &lt;strong&gt;CISC (Intel x86)&lt;/strong&gt; processor is a jack of all trades. 🌀 (O_o) It has &lt;em&gt;tons&lt;/em&gt; of complex, specialized instructions. Want to add two numbers &lt;em&gt;from memory&lt;/em&gt; and store the result &lt;em&gt;back in memory&lt;/em&gt; all in one go? It’s got an instruction for that! &lt;code&gt;ADD [memory_A], [memory_B]&lt;/code&gt; (I'm simplifying, but you get it).&lt;/p&gt;

&lt;p&gt;The philosophy is: "Let's make the &lt;em&gt;hardware&lt;/em&gt; (the chip) smart, so the &lt;em&gt;software&lt;/em&gt; (your code) can be dumber."&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          CISC (x86)
+------------------------------------+
|  ONE BIG, COMPLEX INSTRUCTION      |
|  (e.g., "Add mem A to mem B")      |
+------------------------------------+
| Takes multiple  clock cycles, but  |
| looks like one step to the coder.  |
+------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;RISC (ARM)&lt;/strong&gt; processor is a minimalist. ✨ (^-^) It has a tiny, fixed set of &lt;em&gt;very simple&lt;/em&gt; instructions. All instructions are the same length (e.g., 32 bits). All of them (basically) take &lt;em&gt;one&lt;/em&gt; clock cycle to run.&lt;/p&gt;

&lt;p&gt;Want to add two numbers from memory? Too bad. The RISC philosophy says, "Hey, only 'Load' and 'Store' instructions get to touch memory. That's it."&lt;/p&gt;

&lt;p&gt;So you have to do this instead:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;code&gt;LDR R1, [memory_A]&lt;/code&gt; (Load the value from memory A into a temporary bucket called Register 1)&lt;/li&gt;
&lt;li&gt; &lt;code&gt;LDR R2, [memory_B]&lt;/code&gt; (Load the value from memory B into Register 2)&lt;/li&gt;
&lt;li&gt; &lt;code&gt;ADD R3, R1, R2&lt;/code&gt; (Add the numbers in R1 and R2, put the result in R3)&lt;/li&gt;
&lt;li&gt; &lt;code&gt;STR R3, [memory_C]&lt;/code&gt; (Store the result from R3 into memory C)
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                   RISC (ARM)
+--------+ +--------+ +--------+ +---------+
|  LDR   | |  LDR   | |  ADD   | |  STR    |
+--------+ +--------+ +--------+ +---------+
| Each step is tiny, simple, and takes     |
| one clock cycle. More steps, but faster  |
| and way more predictable.                |
+------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;em&gt;looks&lt;/em&gt; less efficient, right? Four instructions vs. one! But here's the magic: because every instruction is so simple and all the same size, the CPU can &lt;em&gt;chew through them&lt;/em&gt; at a &lt;em&gt;ludicrous&lt;/em&gt; speed. It can pipeline them, run them out of order, and predict what's coming next with spooky accuracy.&lt;/p&gt;

&lt;p&gt;The CISC chip, with its janky, variable-length instructions, is like a Rube Goldberg machine. It's &lt;em&gt;constantly&lt;/em&gt; trying to figure out "Wait, is this instruction 1 byte? Or 5 bytes? Or 15?"&lt;/p&gt;

&lt;p&gt;David Patterson, one of the godfathers of RISC, pointed out that the x86 manual is over &lt;strong&gt;3,600 pages long&lt;/strong&gt;. The ARM manual is... also a beast (5,400 pages for ARMv8), but the &lt;em&gt;core&lt;/em&gt; RISC philosophy is all about that simple, clean foundation.&lt;/p&gt;




&lt;h2&gt;
  
  
  The Heart of the Matter: The Instruction Set
&lt;/h2&gt;

&lt;p&gt;Let's dive a little deeper into that RISC philosophy.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Load or Store Philosophy (aka "The No Touchy Rule")
&lt;/h3&gt;

&lt;p&gt;This is what we just talked about, but it's worth its own section. It's &lt;em&gt;the&lt;/em&gt; defining feature.&lt;/p&gt;

&lt;p&gt;On an ARM chip, math operations (like &lt;code&gt;ADD&lt;/code&gt;, &lt;code&gt;SUB&lt;/code&gt;, &lt;code&gt;MUL&lt;/code&gt;) can &lt;strong&gt;only&lt;/strong&gt; operate on registers. Registers are those tiny, super-fast "bucket" storage spaces &lt;em&gt;right on the CPU&lt;/em&gt;. An x86 chip might have 16 of them. An ARM chip has 32 (or more).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[R1] [R2] [R3] ... [R32]
 (tiny, happy, super-fast buckets!)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a &lt;em&gt;brilliant&lt;/em&gt; design. Why? Because accessing main RAM is &lt;em&gt;slow&lt;/em&gt;. Glacially slow. The CPU is a Formula 1 car, and RAM is a horse drawn buggy. (CPU 🏎️) ... (RAM 🐴)&lt;/p&gt;

&lt;p&gt;The CISC (x86) approach lets you build an F1 car that can &lt;em&gt;also&lt;/em&gt; be pulled by a horse (&lt;code&gt;ADD [memory]...&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The RISC (ARM) approach says: "That's dumb. Let's build &lt;em&gt;a tiny stable&lt;/em&gt; (the registers) right next to the F1 car. We'll use two &lt;em&gt;really fast&lt;/em&gt; couriers (&lt;code&gt;LDR&lt;/code&gt; and &lt;code&gt;STR&lt;/code&gt;) to move hay &lt;em&gt;into&lt;/em&gt; the stable. But the F1 car &lt;em&gt;only&lt;/em&gt; ever deals with the stable."&lt;/p&gt;

&lt;p&gt;This separation makes it &lt;em&gt;way&lt;/em&gt; easier to optimize and run things in parallel.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The RISC philosophy concentrates on reducing the complexity of instructions performed by the hardware. The RISC philosophy provides greater flexibility and intelligence in software rather than hardware."&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.acsce.edu.in/acsce/wp-content/uploads/2020/03/MES-Module-1.pdf" rel="noopener noreferrer"&gt;Source: ACS College of Engineering&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Conditional Love (and Execution)
&lt;/h3&gt;

&lt;p&gt;This is a classic ARM feature that's just &lt;em&gt;so cool&lt;/em&gt;. &lt;code&gt;(^_−)☆&lt;/code&gt; On older ARM architectures (and it's still around in a different form), you could make &lt;em&gt;almost any instruction&lt;/em&gt; conditional.&lt;/p&gt;

&lt;p&gt;In normal code, you'd say:&lt;br&gt;
&lt;code&gt;IF (R1 == 5) THEN { ADD R2, R2, 1 }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This &lt;code&gt;IF&lt;/code&gt; causes a "branch," a jump in the code that can mess up the CPU's perfect pipeline.&lt;/p&gt;

&lt;p&gt;ARM let you do this:&lt;br&gt;
&lt;code&gt;ADD**EQ** R2, R2, 1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This one instruction &lt;em&gt;means&lt;/em&gt;: "Add 1 to R2, &lt;em&gt;but only if&lt;/em&gt; the 'equal' flag is set." The CPU just runs the instruction, checks the flag, and if it's not set, does... nothing. It just skips it in one cycle. No jump. No pipeline flush. It's... &lt;em&gt;elegant&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  The Squeezy Boy: The Thumb Instruction Set
&lt;/h3&gt;

&lt;p&gt;Another classic ARM trick! Those 32-bit instructions were great for performance, but they were &lt;em&gt;chunky&lt;/em&gt;. In the '90s, memory was precious. So, ARM introduced "Thumb"—a &lt;em&gt;second&lt;/em&gt; instruction set, living right alongside the first, that used 16-bit instructions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+---------------+   squeeeeeze!   +-------+
|  32-bit Instr |     (&amp;gt;_&amp;lt;)       | 16-bit|
+---------------+                 +-------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It was a compressed version. The CPU could switch into "Thumb mode," run a bunch of code with double the density, and then switch back to 32-bit mode for the heavy lifting. This was a &lt;em&gt;killer feature&lt;/em&gt; for early mobile phones and the Game Boy Advance.&lt;/p&gt;




&lt;h2&gt;
  
  
  The M1-llion Dollar Question: Why Did Apple Switch?
&lt;/h2&gt;

&lt;p&gt;This is the story, right? Apple, who had been with Intel's x86 for over a decade, just... left. And their new M1 chips &lt;em&gt;destroyed&lt;/em&gt; the Intel competition.&lt;/p&gt;

&lt;p&gt;It wasn't &lt;em&gt;just&lt;/em&gt; RISC vs. CISC. It was &lt;em&gt;control&lt;/em&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance-per-Watt:&lt;/strong&gt; The Intel story for years was "more power, more power, more power." This also meant "more heat, more heat, more heat." Their 12" MacBook was a disaster—it was so hot it had to throttle itself constantly. Intel: (🔥_🔥) (too hot!) Meanwhile, Apple's &lt;em&gt;iPads&lt;/em&gt; (running ARM chips) were getting scarily fast... and had no fans. ARM: (•‿•) (so cool) The writing was on the wall. ARM's simple RISC design is just &lt;em&gt;way&lt;/em&gt; more power-efficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;The SoC (System on a Chip):&lt;/strong&gt; Apple didn't just want to swap a CPU. They wanted to build a single, monolithic &lt;em&gt;beast&lt;/em&gt; of a chip.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+------------------------------------------+
|           Apple 🍎 M1 (SoC)              |
|                                          |
| +-------+ +-------+ +----------+ +-----+ |
| |  CPU  | |  GPU  | | Neural   | | RAM | |
| | (ARM) | |       | | Engine   | |     | |
| +-------+ +-------+ +----------+ +-----+ |
|                                          |
+------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;They put the CPU, the GPU, the AI cores, and &lt;em&gt;even the RAM&lt;/em&gt; on the &lt;em&gt;same piece of silicon&lt;/em&gt;. This is called a "System on a Chip," and it makes data transfer &lt;em&gt;insanely&lt;/em&gt; fast. Intel's model was to sell you a CPU. You'd get your RAM from one company, your GPU from another... Apple wanted to control the whole widget. The ARM &lt;em&gt;license&lt;/em&gt; let them do that. Intel's x86 &lt;em&gt;business model&lt;/em&gt; did not.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;The Gory Details:&lt;/strong&gt; That Hacker News comment was right. The M1's decoders are &lt;em&gt;monstrous&lt;/em&gt;. Because ARM's instructions are all the same simple 32-bit length, Apple could just build a &lt;em&gt;super-wide&lt;/em&gt; decoder that shovels 8 instructions per clock cycle into the chip. Intel's best chips were struggling to do 4, because they're constantly parsing that messy, variable-length x86 code.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Lessons from the Rabbit Hole
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Simplicity is a Superpower:&lt;/strong&gt; 💡 RISC's "dumb" instructions are its greatest strength. It moves complexity from the silicon to the software (the compiler), and it turns out, software is &lt;em&gt;way&lt;/em&gt; easier to change than a factory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Model Matters:&lt;/strong&gt; ARM's &lt;em&gt;business model&lt;/em&gt; (licensing) is just as important as its &lt;em&gt;architecture&lt;/em&gt;. It lets companies like Apple innovate in ways that the "buy it off the shelf" x86 world just can't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Computers Are Still (Beautifully) Cursed:&lt;/strong&gt; At the end of the day, it's still just a fetch-execute cycle. But &lt;em&gt;how&lt;/em&gt; you fetch, and &lt;em&gt;what&lt;/em&gt; you execute... well, that's where all the magic is.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;This just scratches the surface. We didn't even &lt;em&gt;really&lt;/em&gt; get into 64-bit (AArch64), privilege levels (EL0-EL3), or the new kid on the block, &lt;strong&gt;RISC-V&lt;/strong&gt; (which is open source!).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; (\_/)
 (O.o)
(")_(")
(Down the *next* rabbit hole...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But for now, I want to leave you with the &lt;a href="https://developer.arm.com/documentation/" rel="noopener noreferrer"&gt;official Arm developer documentation&lt;/a&gt;. It's... a lot. But now you know the philosophy. It's not a 3,600-page manual of "what if"s; it's a testament to the power of "Load, Store, and get out of the way."&lt;/p&gt;

&lt;p&gt;What's your favorite "simple-is-actually-genius" tech? Let me know!&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>cpu</category>
      <category>arm</category>
    </item>
    <item>
      <title>From Swift to Machine Code</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Tue, 18 Nov 2025 08:08:29 +0000</pubDate>
      <link>https://forem.com/annurdien/from-swift-to-machine-code-1nmp</link>
      <guid>https://forem.com/annurdien/from-swift-to-machine-code-1nmp</guid>
      <description>&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;What is a Compiler?&lt;/li&gt;
&lt;li&gt;The Swift Compiler Architecture&lt;/li&gt;
&lt;li&gt;
Frontend: Source Code to SIL

&lt;ul&gt;
&lt;li&gt;Parsing &amp;amp; Lexing&lt;/li&gt;
&lt;li&gt;Want to See the AST for Yourself?&lt;/li&gt;
&lt;li&gt;Example: An Abstract Syntax Tree (AST)&lt;/li&gt;
&lt;li&gt;Semantic Analysis&lt;/li&gt;
&lt;li&gt;Clang Importer&lt;/li&gt;
&lt;li&gt;SIL Generation&lt;/li&gt;
&lt;li&gt;Example: What SIL Actually Looks Like&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Mid-End: SIL Optimizations

&lt;ul&gt;
&lt;li&gt;SIL Guaranteed Transformations&lt;/li&gt;
&lt;li&gt;SIL Optimizations&lt;/li&gt;
&lt;li&gt;IR Generation&lt;/li&gt;
&lt;li&gt;Example: What LLVM IR Looks Like&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
Back-End: LLVM Code Generation

&lt;ul&gt;
&lt;li&gt;Example: The Final Assembly Output&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Linker: Creating the Final Executable&lt;/li&gt;
&lt;li&gt;Code Examples from the Swift Compiler&lt;/li&gt;
&lt;li&gt;Key Takeaways&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Here's a fun thought: every time you write some Swift code and slam that "Run" button in Xcode, you're kicking off one of the most ridiculously sophisticated magic tricks in modern software. You're triggering this massive, complex piece of engineering. The Swift compiler grabs your (hopefully) beautiful, human-readable code and transforms it into super-efficient, safe, optimized &lt;em&gt;machine code&lt;/em&gt; that your phone or Mac just... runs.&lt;/p&gt;

&lt;p&gt;(This was one of those psyching-myself-out moments for me—seriously, it feels like it should be way more complicated than it is! It's like giving a master chef a recipe for a 5-course meal written in crayon. The chef (our &lt;strong&gt;compiler&lt;/strong&gt;) has to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Figure out what you &lt;em&gt;meant&lt;/em&gt; to write (Parsing).&lt;/li&gt;
&lt;li&gt; Make sure it's a valid, safe recipe that won't burn the kitchen down (Semantic Analysis).&lt;/li&gt;
&lt;li&gt; Make it &lt;em&gt;way more efficient&lt;/em&gt; than your crayon scrawl (Optimization).&lt;/li&gt;
&lt;li&gt; Finally, translate it into the &lt;em&gt;actual physical movements&lt;/em&gt; of their hands and tools (Machine Code).)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Swift first popped into existence back in 2010, thanks to Chris Lattner and his crew at Apple. Their grand plan? To build a shiny new language to replace Objective-C. They wanted something that could do everything Objective-C did, but with way, way better safety, performance, and a developer experience that didn't make you want to tear your hair out. Because who doesn't love a good "out with the old, in with the new" story, right?&lt;/p&gt;

&lt;p&gt;So how does this wild transformation &lt;em&gt;actually&lt;/em&gt; work? What sorcery happens between you typing &lt;code&gt;print("Hello, World!")&lt;/code&gt; and seeing those glorious words appear on your screen? I'm going to walk you through this whole pipeline, step by step. Let's follow the rabbit down the hole.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      (\_/)
      (o.o)
     (")(")o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What is a Compiler?
&lt;/h2&gt;

&lt;p&gt;Before we dive into Swift's peculiarities, let's get on the same page about what this compiler beast even &lt;em&gt;is&lt;/em&gt;. If you ask a computer science professor, they'll clear their throat and give you a dusty definition like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A compiler is software that translates computer code written in one programming language into another language.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sounds... kinda simple. And boring. And it &lt;em&gt;massively&lt;/em&gt; undersells the drama! A compiler isn't just a basic Google Translate for code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    +-----------------+      +-----------------+
    | "Hello" (Swift) | ===&amp;gt; | "Hola" (Machine)|  &amp;lt;- This is WRONG
    +-----------------+      +-----------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's not just translating 'Hello' to 'Hola.' It's more like translating, "I'd, y'know, maybe like to get some food... if that's cool, I guess?" into "GO TO RESTAURANT. ORDER TACOS. PAY. EAT." It takes your sometimes-vague &lt;em&gt;intent&lt;/em&gt; and turns it into a set of brutally fast, efficient, and unambiguous instructions.&lt;/p&gt;

&lt;p&gt;The Swift compiler shoves your code through four main phases, or zones: &lt;strong&gt;Frontend&lt;/strong&gt;, &lt;strong&gt;Mid-End&lt;/strong&gt;, &lt;strong&gt;Back-End&lt;/strong&gt;, and &lt;strong&gt;Linker&lt;/strong&gt;. Each phase grabs the code, does its &lt;em&gt;one job&lt;/em&gt;, and passes it along to the next.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Swift Compiler Architecture
&lt;/h2&gt;

&lt;p&gt;The Swift compiler follows the traditional playbook, but it adds some of its own &lt;em&gt;special sauce&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Why all the stages? You don't try to bake a cake, frost it, and eat it all in one motion. You &lt;em&gt;parse&lt;/em&gt; the ingredients (Frontend), &lt;em&gt;mix and bake&lt;/em&gt; (Mid-End), &lt;em&gt;decorate&lt;/em&gt; (Back-End), and &lt;em&gt;put it on a plate&lt;/em&gt; (Linker). Each step builds on the last.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;waves hands&lt;/em&gt; ~&lt;em&gt;~&lt;/em&gt;~ compiler magic ~&lt;em&gt;~&lt;/em&gt;~&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+-------------------+
|    Frontend       |
| (The Grammar Cop) |
+-------------------+
         |
         v
+---------------------+
|   Mid-End (SIL)     |
| (The Super-Trainer) |
+---------------------+
         |
         v
+---------------------+
|  Back-End (LLVM).   |
| (The Factory Floor) |
+---------------------+
         |
         v
+--------------------------+
|         Linker           |
| (The General Contractor) |
+--------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What makes Swift so special is its secret weapon: &lt;strong&gt;Swift Intermediate Language (SIL)&lt;/strong&gt;. Think of it as a high-level, private language the compiler uses to talk to itself. This SIL thingy allows Swift to perform all kinds of galaxy-brain optimizations that other compilers just, like, &lt;em&gt;can't&lt;/em&gt; achieve. (Seriously, what are those other compilers even doing? Twiddling their thumbs?)&lt;/p&gt;

&lt;h2&gt;
  
  
  Frontend: Source Code to SIL
&lt;/h2&gt;

&lt;p&gt;The frontend is the bouncer and grammar cop at the club. Its job is to take your messy, all-too-human &lt;code&gt;.swift&lt;/code&gt; files, make sure they're following the rules, and prepare them for the optimization party. It transforms that text into the fancy SIL we just talked about.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       .--.
      |o_o |
      |:_/ |
     //   \ \
    (|     | )
    /'\_   _/`\
    \___)=(___/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Parsing &amp;amp; Lexing
&lt;/h2&gt;

&lt;p&gt;First, the compiler just... reads your code. A "lexer" scans your file and breaks it into "tokens" (like &lt;code&gt;func&lt;/code&gt;, &lt;code&gt;myFunctionName&lt;/code&gt;, &lt;code&gt;(&lt;/code&gt;, &lt;code&gt;)&lt;/code&gt;, &lt;code&gt;{&lt;/code&gt;, &lt;code&gt;}&lt;/code&gt;). Then a "parser" takes that stream of tokens and tries to build a giant tree structure called an &lt;strong&gt;Abstract Syntax Tree (AST)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is &lt;em&gt;literally&lt;/em&gt; a grammar check. Like your 8th-grade English teacher. It sees the &lt;code&gt;func&lt;/code&gt; keyword and thinks, "OK, my rulebook says that after &lt;code&gt;func&lt;/code&gt; comes an &lt;em&gt;identifier&lt;/em&gt; (the name), then &lt;code&gt;(&lt;/code&gt;, then some arguments, then &lt;code&gt;)&lt;/code&gt;, then maybe &lt;code&gt;-&amp;gt;&lt;/code&gt; and a return type, and &lt;em&gt;definitely&lt;/em&gt; &lt;code&gt;{&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;." If you miss a curly brace, the parser throws up its hands and gives you that error message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Want to See the AST for Yourself?
&lt;/h2&gt;

&lt;p&gt;This AST thing isn't some mythical, abstract concept. It's &lt;em&gt;real&lt;/em&gt; data. And you can actually go look at it!&lt;/p&gt;

&lt;p&gt;A fantastic tool called the &lt;a href="https://swift-ast-explorer.com/" rel="noopener noreferrer"&gt;Swift AST Explorer&lt;/a&gt; lets you do just this. You paste your Swift code on the left, and on the right, it instantly shows you the exact, glorious, complicated &lt;strong&gt;Abstract Syntax Tree&lt;/strong&gt; that the compiler builds from it. It's the perfect way to see this first step of the compiler in action. Go paste in a &lt;code&gt;struct&lt;/code&gt; or a &lt;code&gt;func&lt;/code&gt; and watch the tree get built. It's so cool!&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: An Abstract Syntax Tree (AST)
&lt;/h3&gt;

&lt;p&gt;Let's say you write this &lt;em&gt;super&lt;/em&gt; simple line of Swift:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;x&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="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The parser chews on this and spits out a data structure (the AST) that, if we drew it as text (like you'd see in the explorer tool!), would look 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;(top_level_code
  (variable_declaration 'x'
    (binary_expression '+'
      (integer_literal 1)
      (integer_literal 2)
    )
  )
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See? It's a tree! &lt;code&gt;top_level_code&lt;/code&gt; is the root. It has one branch, a &lt;code&gt;variable_declaration&lt;/code&gt;. That declaration has a &lt;em&gt;name&lt;/em&gt; (&lt;code&gt;x&lt;/code&gt;) and an &lt;em&gt;initial value&lt;/em&gt;. The value is a &lt;code&gt;binary_expression&lt;/code&gt; (the &lt;code&gt;+&lt;/code&gt; operation). And &lt;em&gt;that&lt;/em&gt; expression has two branches of its own: the &lt;code&gt;integer_literal&lt;/code&gt; &lt;code&gt;1&lt;/code&gt; and the &lt;code&gt;integer_literal&lt;/code&gt; &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The compiler now &lt;em&gt;understands&lt;/em&gt; your code's &lt;em&gt;structure and intent&lt;/em&gt;, not just as a pile of letters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Semantic Analysis
&lt;/h2&gt;

&lt;p&gt;This is the &lt;em&gt;logic&lt;/em&gt; check. Your grammar might be perfect, but you wrote, "The green idea slept furiously." Your teacher is now &lt;em&gt;very&lt;/em&gt; confused.&lt;/p&gt;

&lt;p&gt;This is the compiler stage that says, "Whoa, hoss. You're trying to add a &lt;code&gt;String&lt;/code&gt; to an &lt;code&gt;Int&lt;/code&gt; (&lt;code&gt;"hello" + 5&lt;/code&gt;). That's &lt;em&gt;semantic nonsense&lt;/em&gt;! What does that even &lt;em&gt;mean&lt;/em&gt;? I can't find a &lt;code&gt;+&lt;/code&gt; function that takes a String and an Int. &lt;em&gt;ERROR!&lt;/em&gt;" This is where &lt;strong&gt;type checking&lt;/strong&gt; lives, making sure all your types line up and your code actually &lt;em&gt;makes sense&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clang Importer
&lt;/h2&gt;

&lt;p&gt;This part is the universal translator on your Star Trek team. It's the bridge that connects Swift to all that "legacy" (read: ancient but necessary) C and Objective-C code out there. It peeks into C/Obj-C "header files" and magically makes them available in Swift as if they were native Swift APIs. It handles all the weird cultural (syntactical) differences, like mapping &lt;code&gt;NSString&lt;/code&gt; to &lt;code&gt;String&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  SIL Generation
&lt;/h2&gt;

&lt;p&gt;And &lt;em&gt;poof!&lt;/em&gt; The grand finale for the frontend. This stage takes that big, beautiful, type-checked AST (which is now guaranteed to be grammatically and logically correct) and converts it into &lt;strong&gt;raw Swift Intermediate Language (SIL)&lt;/strong&gt;. This is our first real look at the compiler's internal secret language. It's the super-detailed, high-level &lt;em&gt;set of instructions&lt;/em&gt; for the &lt;em&gt;next&lt;/em&gt; department (the Mid-End).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Swift Source (.swift) → AST → Type-checked AST → Raw SIL
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example: What SIL Actually Looks Like
&lt;/h3&gt;

&lt;p&gt;Let's use our &lt;code&gt;var x = 1 + 2&lt;/code&gt; example again. The &lt;strong&gt;Raw SIL&lt;/strong&gt; (before any optimization) is super verbose, but it looks something like this. &lt;em&gt;Don't panic&lt;/em&gt;, I'll explain it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is our main function&lt;/span&gt;
&lt;span class="n"&gt;sil&lt;/span&gt; &lt;span class="kd"&gt;@main&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="kd"&gt;@convention&lt;/span&gt;&lt;span class="p"&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;Int32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;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;Int32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nf"&gt;bb0&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="n"&gt;$Int32&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="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;$UnsafeMutablePointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Optional&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;UnsafeMutablePointer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int8&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

  &lt;span class="c1"&gt;// 1. Make some space on the stack for our variable 'x'&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;alloc_stack&lt;/span&gt; &lt;span class="n"&gt;$Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="s"&gt;"x"&lt;/span&gt;

  &lt;span class="c1"&gt;// 2. Load the *type* Int&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;metatype&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="kd"&gt;@thin&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;Type&lt;/span&gt;

  &lt;span class="c1"&gt;// 3. Load the *literal value* 1&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;integer_literal&lt;/span&gt; &lt;span class="n"&gt;$Builtin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Int64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&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="kd"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;$Int&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="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;$Builtin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Int64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// 4. Load the *literal value* 2&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="n"&gt;integer_literal&lt;/span&gt; &lt;span class="n"&gt;$Builtin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Int64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&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="kd"&gt;struct&lt;/span&gt; &lt;span class="n"&gt;$Int&lt;/span&gt; &lt;span class="p"&gt;(&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="n"&gt;$Builtin&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Int64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;// 5. Find the '+' function for Ints&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;function_ref&lt;/span&gt; &lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;$sSi1poiyS2i_SitFZ&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="kd"&gt;@convention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;@thin&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;Type&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;Int&lt;/span&gt;

  &lt;span class="c1"&gt;// 6. CALL the '+' function with 1 and 2&lt;/span&gt;
  &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;apply&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="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="p"&gt;,&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="p"&gt;:&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="kd"&gt;@convention&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&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="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;@thin&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;Type&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;Int&lt;/span&gt;

  &lt;span class="c1"&gt;// 7. STORE the result (which is in %9) into our variable 'x' (which is at %2)&lt;/span&gt;
  &lt;span class="n"&gt;store&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="n"&gt;to&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="err"&gt;$&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;

  &lt;span class="c1"&gt;// ... (lots of cleanup code)...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See? It's not &lt;em&gt;magic&lt;/em&gt;, it's just a &lt;em&gt;lot&lt;/em&gt; of very specific, tiny steps! It has to "allocate" memory for &lt;code&gt;x&lt;/code&gt;, load the numbers &lt;code&gt;1&lt;/code&gt; and &lt;code&gt;2&lt;/code&gt;, &lt;em&gt;find the &lt;code&gt;+&lt;/code&gt; function&lt;/em&gt;, &lt;em&gt;call&lt;/em&gt; the &lt;code&gt;+&lt;/code&gt; function, and finally &lt;em&gt;store&lt;/em&gt; the result. This verbosity is &lt;em&gt;good&lt;/em&gt;—it gives the optimizer tons of tiny pieces to move around and improve.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mid-End: SIL Optimizations
&lt;/h2&gt;

&lt;p&gt;Okay, we have Raw SIL. Now the &lt;strong&gt;Mid-End&lt;/strong&gt; steps in. This is the personal trainer. Its job is to take that "raw" SIL and make it &lt;em&gt;better, faster, stronger&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is where Swift gets its superpowers. Because SIL still "understands" high-level Swift concepts (like &lt;code&gt;Array&lt;/code&gt; or &lt;code&gt;String&lt;/code&gt;, not just raw memory addresses), it can make &lt;em&gt;smarter&lt;/em&gt; changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       .---.
      /  .-.  \
     |  |   |  |
      \  `-'  /
       `-----'
       /     \
      |       |
      |       |
       \     /
        `---'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  SIL Guaranteed Transformations
&lt;/h2&gt;

&lt;p&gt;First, some mandatory checkups. This is the "eat your vegetables" phase. It's the compiler being your spotter at the gym. "Whoa there, you're about to use that &lt;code&gt;myVariable&lt;/code&gt; before you put any value in it! That's a crash waiting to happen. Let me stop you right there." This is what &lt;em&gt;guarantees&lt;/em&gt; Swift's memory safety and catches things like uninitialized variables. This produces "canonical" SIL.&lt;/p&gt;

&lt;h2&gt;
  
  
  SIL Optimizations
&lt;/h2&gt;

&lt;p&gt;Now the &lt;em&gt;real&lt;/em&gt; fun begins. After we have "canonical" SIL, the compiler runs a ton of optimization passes. These are high-level, Swift-specific transformations. Stuff like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ARC Optimization&lt;/strong&gt;: This is the compiler cleaning up after you. It sees where you create an object and figures out &lt;em&gt;exactly&lt;/em&gt; where you're done with it, inserting the 'throw this in the trash' (release) code automatically. Then it &lt;em&gt;optimizes&lt;/em&gt; that, like "Hmm, you're releasing it on line 10 and retaining it again on line 11? Let's just... &lt;em&gt;not&lt;/em&gt; do either of those. Saved two instructions."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Generic Specialization&lt;/strong&gt;: This is a custom-tailoring shop. It sees your generic, one-size-fits-all &lt;code&gt;Array&amp;lt;T&amp;gt;&lt;/code&gt; is being used in one place as &lt;code&gt;Array&amp;lt;Int&amp;gt;&lt;/code&gt; and another as &lt;code&gt;Array&amp;lt;String&amp;gt;&lt;/code&gt;. So it builds &lt;em&gt;two brand-new, custom-fit&lt;/em&gt; versions: one that &lt;em&gt;only&lt;/em&gt; works on &lt;code&gt;Int&lt;/code&gt;s and one that &lt;em&gt;only&lt;/em&gt; works on &lt;code&gt;String&lt;/code&gt;s. These bespoke versions are way faster.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Devirtualization&lt;/strong&gt;: Replaces a fancy, dynamic "hey, &lt;em&gt;somebody&lt;/em&gt; needs to run this function" call (dynamic dispatch) with a simple, direct "call &lt;em&gt;this specific function right here&lt;/em&gt;" (static dispatch). Much, much faster.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dead Code Elimination&lt;/strong&gt;: Finds code that never, ever gets run (maybe it's inside an &lt;code&gt;if false&lt;/code&gt;) and just yeets it into the sun.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Inlining&lt;/strong&gt;: The compiler sees your code is about to make a "phone call" (function call) to a tiny function that just does &lt;code&gt;a + b&lt;/code&gt;. It says, "This is dumb. Why go through all the overhead of a call? I'll just &lt;em&gt;paste the &lt;code&gt;a + b&lt;/code&gt; code right here.&lt;/em&gt;" It hangs up the phone and just does the work.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  IR Generation
&lt;/h2&gt;

&lt;p&gt;Once the SIL is as lean and mean as it can possibly be, it's time to translate it &lt;em&gt;again&lt;/em&gt;. This step (&lt;strong&gt;lib/IRGen&lt;/strong&gt;) lowers the "Optimized SIL" into &lt;strong&gt;LLVM Intermediate Representation (IR)&lt;/strong&gt;. We're now officially leaving the cozy, Swift-specific world and entering the big, scary, platform-agnostic land of LLVM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Raw SIL → Canonical SIL → Optimized SIL → LLVM IR
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example: What LLVM IR Looks Like
&lt;/h3&gt;

&lt;p&gt;So, our optimizer looked at that bloated SIL and said, "Are you kidding me? You're allocating a variable just to store the result of &lt;code&gt;1 + 2&lt;/code&gt;? I &lt;em&gt;know&lt;/em&gt; what &lt;code&gt;1 + 2&lt;/code&gt; is! It's &lt;code&gt;3&lt;/code&gt;! And you're not even &lt;em&gt;using&lt;/em&gt; &lt;code&gt;x&lt;/code&gt;! I'm throwing all this junk out."&lt;/p&gt;

&lt;p&gt;In a &lt;em&gt;real&lt;/em&gt; (but still simple) optimized scenario where you &lt;code&gt;print(1 + 2)&lt;/code&gt;, the &lt;strong&gt;LLVM IR&lt;/strong&gt; would look totally different. The optimizer would pre-calculate &lt;code&gt;1 + 2&lt;/code&gt; (a process called "constant folding"). The IR would be translated from SIL and look more like this generic, low-level assembly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight llvm"&gt;&lt;code&gt;&lt;span class="c1"&gt;; Define a constant string for "3\n"&lt;/span&gt;
&lt;span class="vg"&gt;@.str&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;unnamed_addr&lt;/span&gt; &lt;span class="k"&gt;constant&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="s"&gt;c"3\n\00"&lt;/span&gt;

&lt;span class="c1"&gt;; The main function&lt;/span&gt;
&lt;span class="k"&gt;define&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="vg"&gt;@main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="nv"&gt;%0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;**&lt;/span&gt; &lt;span class="nv"&gt;%1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;; 1. Get a pointer to our constant string "3\n"&lt;/span&gt;
  &lt;span class="nv"&gt;%3&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;getelementptr&lt;/span&gt; &lt;span class="k"&gt;inbounds&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt; &lt;span class="p"&gt;x&lt;/span&gt; &lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;]*&lt;/span&gt; &lt;span class="vg"&gt;@.str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;i64&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;i64&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;

  &lt;span class="c1"&gt;; 2. Call the 'puts' function (a C function to print a string)&lt;/span&gt;
  &lt;span class="nv"&gt;%4&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;call&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="vg"&gt;@puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;%3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="c1"&gt;; 3. Return 0&lt;/span&gt;
  &lt;span class="k"&gt;ret&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;; Declaration for the external 'puts' function&lt;/span&gt;
&lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="kt"&gt;i32&lt;/span&gt; &lt;span class="vg"&gt;@puts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;i8&lt;/span&gt;&lt;span class="p"&gt;*)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait, what? Where did our &lt;code&gt;+&lt;/code&gt; go? &lt;strong&gt;The optimizer killed it!&lt;/strong&gt; It saw &lt;code&gt;1 + 2&lt;/code&gt;, did the math &lt;em&gt;at compile time&lt;/em&gt;, and just replaced the whole operation with the &lt;em&gt;result&lt;/em&gt;, &lt;code&gt;3&lt;/code&gt;. Then it just calls a basic &lt;code&gt;puts&lt;/code&gt; function (or a Swift equivalent) to print the string "3".&lt;/p&gt;

&lt;p&gt;&lt;em&gt;That's&lt;/em&gt; optimization. All that complicated SIL &lt;em&gt;vanished&lt;/em&gt; because it wasn't needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Back-End: LLVM Code Generation
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Back-End&lt;/strong&gt; is the factory floor. This is where we forget all about "Swift". The code is now in LLVM IR, a &lt;em&gt;generic&lt;/em&gt; compiler language. LLVM is a masterpiece of engineering used by many languages (like Rust, Clang, C++, etc.). Swift is now just &lt;em&gt;one more customer&lt;/em&gt; of the giant LLVM factory.&lt;/p&gt;

&lt;p&gt;It takes that generic LLVM IR and does the &lt;em&gt;final&lt;/em&gt; translation into actual, real-deal &lt;strong&gt;machine code&lt;/strong&gt; for your target architecture (e.g., the ARM chip in your iPhone or the M-series chip in your Mac).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        ._________.
        | .-[ ]-. |
        | |  '  | |
        | |_____| |
        |_________|
       /           \
      /             \
     |_______________|
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example: The Final Assembly Output
&lt;/h3&gt;

&lt;p&gt;This is it! The moment of truth! The LLVM Back-End takes that generic LLVM IR (the &lt;code&gt;puts("3\n")&lt;/code&gt; one) and translates it into the &lt;em&gt;specific&lt;/em&gt; assembly language for your CPU. If you're compiling for an Apple Silicon Mac (ARM64 architecture), it will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight armasm"&gt;&lt;code&gt;&lt;span class="c"&gt;; This is ARM64 assembly (for an Apple M-series chip)&lt;/span&gt;
&lt;span class="nl"&gt;_main&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
    &lt;span class="nb"&gt;stp&lt;/span&gt;     &lt;span class="nv"&gt;x29&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;x30&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;sp&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="o"&gt;]!&lt;/span&gt; &lt;span class="c"&gt;; Set up the stack (prologue)&lt;/span&gt;
    &lt;span class="nb"&gt;mov&lt;/span&gt;     &lt;span class="nv"&gt;x29&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;sp&lt;/span&gt;

    &lt;span class="c"&gt;; 1. Get the address of our string "3\n"&lt;/span&gt;
    &lt;span class="nb"&gt;adrp&lt;/span&gt;    &lt;span class="nv"&gt;x0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;_main&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;str&lt;/span&gt;&lt;span class="no"&gt;@&lt;/span&gt;&lt;span class="nv"&gt;PAGE&lt;/span&gt;
    &lt;span class="nb"&gt;add&lt;/span&gt;     &lt;span class="nv"&gt;x0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;x0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;_main&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nv"&gt;str&lt;/span&gt;&lt;span class="no"&gt;@&lt;/span&gt;&lt;span class="nv"&gt;PAGEOFF&lt;/span&gt;

    &lt;span class="c"&gt;; 2. Call the 'puts' function (address is in register x0)&lt;/span&gt;
    &lt;span class="nb"&gt;bl&lt;/span&gt;      &lt;span class="nv"&gt;_puts&lt;/span&gt;

    &lt;span class="c"&gt;; 3. Set the return value to 0&lt;/span&gt;
    &lt;span class="nb"&gt;mov&lt;/span&gt;     &lt;span class="nv"&gt;w0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;#&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="nb"&gt;ldp&lt;/span&gt;     &lt;span class="nv"&gt;x29&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;x30&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nv"&gt;sp&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="c"&gt;; Clean up the stack (epilogue)&lt;/span&gt;
    &lt;span class="nb"&gt;ret&lt;/span&gt;                        &lt;span class="c"&gt;; Return&lt;/span&gt;

&lt;span class="c"&gt;; The data section, where our string lives&lt;/span&gt;
&lt;span class="nl"&gt;_main&lt;/span&gt;&lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nl"&gt;str&lt;/span&gt;&lt;span class="err"&gt;:&lt;/span&gt;
    &lt;span class="err"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;asciz&lt;/span&gt;  &lt;span class="s2"&gt;"3\n"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the &lt;em&gt;real deal&lt;/em&gt;! This is the human-readable version of the binary machine code. No more high-level concepts like 'string' or 'variable.' This is all just: 'load this memory address into CPU register &lt;code&gt;x0&lt;/code&gt;,' 'jump to the &lt;code&gt;_puts&lt;/code&gt; function,' and 'put a &lt;code&gt;0&lt;/code&gt; in the return register.'&lt;/p&gt;

&lt;p&gt;The CPU just chugs through these instructions one by one. And &lt;em&gt;poof!&lt;/em&gt; '3' appears on your screen.&lt;/p&gt;

&lt;p&gt;But wait, there's more! The LLVM back-end does &lt;em&gt;another&lt;/em&gt; whole round of optimizations &lt;em&gt;on the assembly itself&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Instruction Selection&lt;/strong&gt;: Picks the &lt;em&gt;best&lt;/em&gt; and most efficient machine instructions for the job.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Register Allocation&lt;/strong&gt;: This is a &lt;em&gt;huge&lt;/em&gt; one. Your CPU has a &lt;em&gt;tiny&lt;/em&gt; number of super-fast storage buckets (like 32 registers). This stage is a master-level game of Sudoku, figuring out how to juggle all your hundreds of variables to use &lt;em&gt;only&lt;/em&gt; those few registers for maximum speed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Instruction Scheduling&lt;/strong&gt;: Re-orders the machine instructions to make sure the CPU's internal pipelines are always full and chugging along efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Peephole Optimizations&lt;/strong&gt;: Takes one last look and makes tiny, local improvements (e.g., "you're storing a value just to load it right back? I'll just keep it in the register.").&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, the code generator spits out &lt;strong&gt;object files&lt;/strong&gt; (&lt;code&gt;.o&lt;/code&gt;). This is it! This is machine code! ...Almost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linker: Creating the Final Executable
&lt;/h2&gt;

&lt;p&gt;You thought we were done? Ha! Psych!&lt;/p&gt;

&lt;p&gt;You've got your &lt;code&gt;.o&lt;/code&gt; file (or maybe a bunch of them), but it's just one piece of the puzzle. It's like one part of an IKEA furniture set. Your code (&lt;code&gt;.o&lt;/code&gt; file) is the &lt;code&gt;BILLY&lt;/code&gt; bookcase. But the instructions say you need a 'hex key' (the &lt;code&gt;print&lt;/code&gt; function) and 'screws' (system libraries). Your &lt;code&gt;.o&lt;/code&gt; file just has a bunch of "IOUs" for code it needs.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Linker&lt;/strong&gt; is the final boss. It's the general contractor who assembles the final, runnable program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      /----\     /----\
     /      \   /      \
    |        |-|        |
     \      /   \      /
      \----/     \----/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The linker does a few crucial things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Symbol Resolution&lt;/strong&gt;: It goes through all your &lt;code&gt;.o&lt;/code&gt; files. It sees your code "calling" &lt;code&gt;_puts&lt;/code&gt; (a symbol) and finds the &lt;em&gt;actual code&lt;/em&gt; for &lt;code&gt;_puts&lt;/code&gt; in the operating system's C library. It connects the "call" to the "destination."&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Library Linking&lt;/strong&gt;: It grabs all the system libraries (like &lt;code&gt;libc&lt;/code&gt;, &lt;code&gt;Foundation&lt;/code&gt;, &lt;code&gt;SwiftCore&lt;/code&gt;) and user libraries you said you needed and pulls in just the pieces you &lt;em&gt;actually&lt;/em&gt; use.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Relocation&lt;/strong&gt;: It adjusts all the memory addresses in the code so it can be loaded and run properly by the operating system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dead Code Stripping&lt;/strong&gt;: After pulling in those big libraries, it finds all the code you &lt;em&gt;didn't&lt;/em&gt; end up using and tosses it out to make your final executable smaller.
  
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ╱|、
  (˚ˎ 。7  
   |、˜〵          
  じしˍ,)ノ

Take a short break here.....
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;h2&gt;
  
  
  Code Examples from the Swift Compiler
&lt;/h2&gt;

&lt;p&gt;Don't just take my word for it. This stuff is all open source! Let's peek under the hood at the &lt;em&gt;actual source code&lt;/em&gt; from the Swift compiler repository. This is the belly of the beast.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      .--.
     /    \
    |      | \
     \    /  /
      '--'  /
           /
          /
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Parsing: Building the AST
&lt;/h2&gt;

&lt;p&gt;This is the code that reads your text and builds the grammar tree.&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;// From lib/Parse/Parser.h in swiftlang/swift&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Parser&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="n"&gt;ParserResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Expr&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parseExpr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Diag&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;ParserResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Stmt&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parseStmt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="n"&gt;ParserResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Decl&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parseDecl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ParseDeclOptions&lt;/span&gt; &lt;span class="n"&gt;Flags&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;IsAtStartOfLineOrPreviousHadSemi&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Parse a function declaration&lt;/span&gt;
  &lt;span class="n"&gt;ParserResult&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;FuncDecl&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;parseFuncDecl&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;strong&gt;What this &lt;em&gt;really&lt;/em&gt; means:&lt;/strong&gt;&lt;br&gt;
Look at those function names! &lt;code&gt;parseExpr&lt;/code&gt; is the code that gets called when the parser expects an &lt;em&gt;expression&lt;/em&gt; (like &lt;code&gt;1 + 1&lt;/code&gt; or &lt;code&gt;foo()&lt;/code&gt;). &lt;code&gt;parseStmt&lt;/code&gt; handles a &lt;em&gt;statement&lt;/em&gt; (like &lt;code&gt;var x = 5&lt;/code&gt;). &lt;code&gt;parseDecl&lt;/code&gt; handles a &lt;em&gt;declaration&lt;/em&gt; (like &lt;code&gt;class MyClass {}&lt;/code&gt; or &lt;code&gt;func myFunc()&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Real-life example:&lt;/strong&gt; When you type &lt;code&gt;func&lt;/code&gt;, the main parser loop says "Aha! The &lt;code&gt;func&lt;/code&gt; keyword! I'll call &lt;code&gt;parseFuncDecl()&lt;/code&gt;." That function then takes over, saying "OK, I just saw &lt;code&gt;func&lt;/code&gt;. According to my rulebook, the &lt;em&gt;next&lt;/em&gt; thing &lt;em&gt;must&lt;/em&gt; be an identifier (the function name). Let me call my &lt;code&gt;parseIdentifier&lt;/code&gt; helper..." If it finds a &lt;code&gt;5&lt;/code&gt; instead, it throws a "parser error"—your familiar "Expected an identifier" message.&lt;/p&gt;
&lt;h2&gt;
  
  
  Type Checking: Semantic Analysis
&lt;/h2&gt;

&lt;p&gt;This is the code that &lt;em&gt;solves the puzzle&lt;/em&gt; of your types.&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;// From lib/Sema/TypeCheckConstraints.cpp in swiftlang/swift&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConstraintSystem&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="n"&gt;Solution&lt;/span&gt; &lt;span class="n"&gt;applySolution&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;Solution&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;solution&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// Type checking for function calls&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;solveForFunctionCall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ApplyExpr&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;apply&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;strong&gt;What this &lt;em&gt;really&lt;/em&gt; means:&lt;/strong&gt;&lt;br&gt;
This is &lt;em&gt;so cool&lt;/em&gt;. When you write &lt;code&gt;let x = "hello" + 5&lt;/code&gt;, the compiler creates a &lt;code&gt;ConstraintSystem&lt;/code&gt;—a big logic puzzle. It says, "I have an &lt;code&gt;ApplyExpr&lt;/code&gt; (a function call, because &lt;code&gt;+&lt;/code&gt; is just a function!). The left side is &lt;code&gt;String&lt;/code&gt;. The right side is &lt;code&gt;Int&lt;/code&gt;. I need to &lt;code&gt;solve()&lt;/code&gt; for a function &lt;code&gt;+&lt;/code&gt; that takes &lt;code&gt;(String, Int)&lt;/code&gt;. It looks through all its 'constraint' rules, finds &lt;em&gt;no such function&lt;/em&gt;, and &lt;code&gt;solve()&lt;/code&gt; fails. &lt;code&gt;solveForFunctionCall&lt;/code&gt; is the detective trying to find a suspect (&lt;code&gt;+&lt;/code&gt; function) that matches the evidence (&lt;code&gt;String&lt;/code&gt; and &lt;code&gt;Int&lt;/code&gt;).&lt;/p&gt;
&lt;h2&gt;
  
  
  SIL Generation: High-Level IR
&lt;/h2&gt;

&lt;p&gt;This is the scribe that translates the AST (the &lt;em&gt;idea&lt;/em&gt;) into SIL (the &lt;em&gt;recipe&lt;/em&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="c1"&gt;// From lib/SILGen/SILGen.cpp in swiftlang/swift&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SILGenModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="n"&gt;SILFunction&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;getOrCreateFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SILLocation&lt;/span&gt; &lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SILDeclRef&lt;/span&gt; &lt;span class="n"&gt;constant&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                   &lt;span class="n"&gt;ForDefinition_t&lt;/span&gt; &lt;span class="n"&gt;forDefinition&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Generate SIL for expressions&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;emitExprInto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RValue&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;rv&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SILLocation&lt;/span&gt; &lt;span class="n"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SGFContext&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What this &lt;em&gt;really&lt;/em&gt; means:&lt;/strong&gt;&lt;br&gt;
This is the translator. &lt;code&gt;SILGenModule&lt;/code&gt; is in charge of converting the entire AST into SIL. When it sees your &lt;code&gt;myFunc&lt;/code&gt; in the AST, it calls &lt;code&gt;getOrCreateFunction&lt;/code&gt; to build the new, empty SIL function. Then, it walks all the code &lt;em&gt;inside&lt;/em&gt; your function. When it sees the expression &lt;code&gt;1 + 1&lt;/code&gt;, it calls &lt;code&gt;emitExprInto&lt;/code&gt; to spit out the &lt;em&gt;actual SIL instructions&lt;/em&gt; for loading &lt;code&gt;1&lt;/code&gt;, loading another &lt;code&gt;1&lt;/code&gt;, and calling the &lt;code&gt;add&lt;/code&gt; function for integers.&lt;/p&gt;
&lt;h2&gt;
  
  
  SIL Optimizations: Mid-Level Transformations
&lt;/h2&gt;

&lt;p&gt;This is the clean-up crew, the personal trainer, the efficiency expert.&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;// From lib/SILOptimizer/SILCombiner/SILCombiner.h in swiftlang/swift&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SILCombiner&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;SILInstructionVisitor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;SILCombiner&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;visitApplyInst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ApplyInst&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;AI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;visitAllocStackInst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AllocStackInst&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;ASI&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Combine redundant instructions&lt;/span&gt;
  &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;tryCombine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;InstructionBase&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What this &lt;em&gt;really&lt;/em&gt; means:&lt;/strong&gt;&lt;br&gt;
This is the &lt;em&gt;clean-up crew&lt;/em&gt;. It's a &lt;code&gt;Visitor&lt;/code&gt; that 'visits' every single instruction in the SIL. Look at &lt;code&gt;visitApplyInst&lt;/code&gt;—this code runs for &lt;em&gt;every function call&lt;/em&gt; (&lt;code&gt;ApplyInst&lt;/code&gt;) in your program. Inside this function is a &lt;em&gt;giant&lt;/em&gt; list of patterns. It might say, "Are you calling a function that just returns a value... and then immediately calling another function with that value? I can &lt;em&gt;combine&lt;/em&gt; these into one operation!" &lt;code&gt;tryCombine&lt;/code&gt; is its master function, full of tricks to make the code faster.&lt;/p&gt;
&lt;h2&gt;
  
  
  LLVM IR Generation: Lowering to LLVM
&lt;/h2&gt;

&lt;p&gt;This is the final translation from the Swift world to the generic factory floor.&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;// From lib/IRGen/IRGen.cpp in swiftlang/swift&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IRGenerator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&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;Module&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;getModule&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;const&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;Module&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Generate LLVM IR for SIL functions&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;emitSILFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SILFunction&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What this &lt;em&gt;really&lt;/em&gt; means:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;IRGenerator&lt;/code&gt; is the final translator. Its big job is &lt;code&gt;emitSILFunction&lt;/code&gt;. This function takes a whole function written in &lt;strong&gt;SIL&lt;/strong&gt; (Swift-specific) and painstakingly converts it, instruction by instruction, into &lt;strong&gt;LLVM IR&lt;/strong&gt; (generic). It's the bridge out of the Swift-specific world. &lt;code&gt;getModule()&lt;/code&gt; is just a helper to get the 'file' (&lt;code&gt;llvm::Module&lt;/code&gt;) that it's writing all this new LLVM IR into.&lt;/p&gt;
&lt;h2&gt;
  
  
  Linking: Final Assembly
&lt;/h2&gt;

&lt;p&gt;This isn't the &lt;em&gt;final&lt;/em&gt; OS linker, but a &lt;em&gt;compiler&lt;/em&gt; linker that helps with optimization!&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;// From lib/SILOptimizer/UtilityPasses/Link.cpp in swiftlang/swift&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SILLinker&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;SILModuleTransform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nl"&gt;public:&lt;/span&gt;
  &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;SILModule&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;getModule&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;auto&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Fn&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;M&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;M&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linkFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LinkMode&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="n"&gt;invalidateAnalysis&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Fn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SILAnalysis&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;InvalidationKind&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Everything&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;strong&gt;What this &lt;em&gt;really&lt;/em&gt; means:&lt;/strong&gt;&lt;br&gt;
This is a super-clever part of the &lt;em&gt;optimization&lt;/em&gt; phase. A &lt;code&gt;SILModule&lt;/code&gt; is your &lt;em&gt;entire app's&lt;/em&gt; worth of SIL. This pass, &lt;code&gt;SILLinker&lt;/code&gt;, runs through it and finds all the functions (&lt;code&gt;Fn&lt;/code&gt;). &lt;code&gt;M.linkFunction&lt;/code&gt; is the key: it's the code that says, "Oh, you're &lt;em&gt;calling&lt;/em&gt; &lt;code&gt;myOtherFunction&lt;/code&gt;? Let me go &lt;em&gt;find&lt;/em&gt; &lt;code&gt;myOtherFunction&lt;/code&gt;'s SIL code (maybe from another file) and &lt;em&gt;pull it in&lt;/em&gt; so the optimizer can see &lt;em&gt;both&lt;/em&gt; functions at the same time." This is what allows &lt;strong&gt;Whole Module Optimization&lt;/strong&gt; and lets the compiler do crazy-smart things like inlining functions &lt;em&gt;across files&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Key Takeaways
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     .--.
    /  () \
    \    /
     `--'
       |
       |
      / \
     |   |
     `---'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;It's not a single magic box. It's an assembly line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Multi-phase Pipeline&lt;/strong&gt;: A &lt;em&gt;grammar teacher&lt;/em&gt; (Frontend), a &lt;em&gt;hyper-obsessed personal trainer&lt;/em&gt; (Mid-End/SIL), a &lt;em&gt;generic factory floor&lt;/em&gt; (Back-End/LLVM), and a &lt;em&gt;general contractor&lt;/em&gt; (Linker).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SIL is Unique&lt;/strong&gt;: That &lt;strong&gt;Swift Intermediate Language (SIL)&lt;/strong&gt; is the &lt;em&gt;real&lt;/em&gt; secret weapon. It lets Swift do all kinds of high-level, safety-conscious optimizations (like for generics and ARC) that other languages just can't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open Source&lt;/strong&gt;: This is all just... out there. You can go read the entire &lt;a href="https://github.com/swiftlang/swift" rel="noopener noreferrer"&gt;swiftlang/swift&lt;/a&gt; repository right now. Go on. Psych yourself out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization Focus&lt;/strong&gt;: They are &lt;em&gt;obsessed&lt;/em&gt; with optimization. Every single stage, from SIL to LLVM, is designed to clean, tweak, polish, and speed up your code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safety First&lt;/strong&gt;: The compiler is your overprotective parent. It uses semantic analysis (&lt;code&gt;TypeCheckConstraints&lt;/code&gt;) and guaranteed SIL transformations to enforce Swift's safety rules so you don't (metaphorically) run with scissors.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So, yeah. The Swift compiler is one hell of a piece of software. It takes your perfectly reasonable human-written code and chews it up, transforms it, optimizes it, and spits it out the other end as blazing-fast, executable machine code. It does all this through four key phases, each ensuring your final program is safe, correct, and as fast as it can be.&lt;/p&gt;

&lt;p&gt;Get your head around this pipeline, and you'll not only write better Swift code, but you'll be a wizard at debugging when things go weird. The secret, as we've seen, is that &lt;strong&gt;SIL&lt;/strong&gt; thing. It's the compiler's private notebook, letting it perform high-level optimizations other compilers can only dream of.&lt;/p&gt;

&lt;p&gt;And it's all open source! You can go wade into the &lt;a href="https://github.com/swiftlang/swift" rel="noopener noreferrer"&gt;swiftlang/swift&lt;/a&gt; repository if you're feeling brave. For a &lt;em&gt;slightly&lt;/em&gt; less terrifying deep dive, check the official &lt;a href="https://www.swift.org/documentation/swift-compiler/" rel="noopener noreferrer"&gt;Swift compiler documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;From your brain to a running binary, the compiler is the unsung hero doing all the heavy lifting. Next time you hit "Run" in Xcode and wait for your print statement to appear, just take a second to appreciate the journey your code is on. It's doing all this work while you just sit there. Computers are so cool!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;( •_•)
( •_•)&amp;gt;⌐■-■
(⌐■_■)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>swift</category>
      <category>compiling</category>
      <category>code</category>
      <category>ios</category>
    </item>
    <item>
      <title>Swift Actor, what is that?</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Tue, 18 Nov 2025 08:04:33 +0000</pubDate>
      <link>https://forem.com/annurdien/swift-actor-what-is-that-p0a</link>
      <guid>https://forem.com/annurdien/swift-actor-what-is-that-p0a</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;You've probably written a race condition. It's okay. We all have. We deploy it, and we just kinda... hope. We're all that dog in the "This is fine" meme, surrounded by the fire of multiple threads trying to write to &lt;code&gt;var totalClicks = 0&lt;/code&gt; at the same time.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For decades, our solution was... "don't do that." We used locks, mutexes, semaphores, or the grand-daddy of all, &lt;code&gt;DispatchQueue.sync&lt;/code&gt;. These are all just fancy ways of putting a velvet rope in front of our data, and they are &lt;em&gt;so easy&lt;/em&gt; to get wrong.&lt;/p&gt;

&lt;p&gt;Then Swift 5.5 gave us &lt;strong&gt;Actors&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  TLDR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An &lt;strong&gt;Actor&lt;/strong&gt; is a special kind of &lt;code&gt;class&lt;/code&gt; that &lt;strong&gt;protects its own data&lt;/strong&gt;. It's an "island of isolation."&lt;/li&gt;
&lt;li&gt;It prevents data races by forcing &lt;em&gt;all&lt;/em&gt; access to its &lt;code&gt;var&lt;/code&gt; properties from the &lt;em&gt;outside&lt;/em&gt; to be &lt;strong&gt;asynchronous&lt;/strong&gt; (using &lt;code&gt;await&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;It processes these &lt;code&gt;await&lt;/code&gt; calls &lt;strong&gt;one at a time&lt;/strong&gt;, in the order they arrive (mostly). It has a "mailbox," or a queue, for incoming jobs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crucially, an actor is NOT a thread.&lt;/strong&gt; It's a lightweight &lt;em&gt;task&lt;/em&gt; that runs on a cooperative &lt;strong&gt;thread pool&lt;/strong&gt; managed by the Swift runtime.&lt;/li&gt;
&lt;li&gt;The "magic" isn't in the CPU; it's in the &lt;strong&gt;compiler&lt;/strong&gt; (which enforces the &lt;code&gt;await&lt;/code&gt;) and the &lt;strong&gt;Swift Runtime&lt;/strong&gt; (which manages the mailbox and thread pool).&lt;/li&gt;
&lt;li&gt;Actors are &lt;strong&gt;re-entrant&lt;/strong&gt;, which is a fancy way of saying they can &lt;em&gt;pause&lt;/em&gt; their current job (at an &lt;code&gt;await&lt;/code&gt;) to run a &lt;em&gt;new&lt;/em&gt; job, then come back later. This is weird, but it prevents deadlocks.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  The Wild West of Dispatch Queue
&lt;/h2&gt;

&lt;p&gt;Before actors, if you wanted to protect a variable, you did this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ClickCounter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;queue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;DispatchQueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"com.my-app.click-counter-queue"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;queue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// This block is "safe"&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&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="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getCount&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;Int&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;queue&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sync&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// This block is also "safe"&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&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... works. But it's cursed.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;It's manual:&lt;/strong&gt; You have to &lt;em&gt;remember&lt;/em&gt; to wrap &lt;em&gt;every&lt;/em&gt; access in &lt;code&gt;queue.sync&lt;/code&gt;. If you forget just one, &lt;em&gt;poof&lt;/em&gt;, data race.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;It's blocking:&lt;/strong&gt; &lt;code&gt;queue.sync&lt;/code&gt; &lt;em&gt;blocks&lt;/em&gt; the calling thread until the work is done. If you call this from the main thread, your UI freezes.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;It's easy to deadlock:&lt;/strong&gt; What if &lt;code&gt;queue.sync&lt;/code&gt; calls another function that &lt;em&gt;also&lt;/em&gt; tries to &lt;code&gt;queue.sync&lt;/code&gt; on the &lt;em&gt;same queue&lt;/em&gt;? You've just deadlocked. Your app is frozen forever. Congrats.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Thread 1 (Main)                            Thread 2 (Background)
      |                                             |
      |--- accesses var C ---X--- accesses var C ---|
      |                                             | (kaboom)
      | (UI frozen)                                 | (Wrong value)
      v                                             v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We needed something better. We needed the &lt;em&gt;compiler&lt;/em&gt; to protect us from ourselves.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Even Is an Actor?
&lt;/h2&gt;

&lt;p&gt;This is what an actor looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;actor&lt;/span&gt; &lt;span class="kt"&gt;ClickCounter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Look ma, no queue!&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&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="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;getCount&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;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Also no queue!&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&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... this is just a &lt;code&gt;class&lt;/code&gt;, right? It looks so simple. This was one of those "psyching myself out" moments. I expected more.&lt;/p&gt;

&lt;p&gt;But the magic isn't in what &lt;em&gt;you&lt;/em&gt; write. It's in what the &lt;em&gt;compiler&lt;/em&gt; makes &lt;em&gt;everyone else&lt;/em&gt; write.&lt;/p&gt;

&lt;p&gt;If you have an instance of it: &lt;code&gt;let counter = ClickCounter()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;counter.increment()&lt;/code&gt; -&amp;gt; &lt;strong&gt;COMPILE ERROR.&lt;/strong&gt; &lt;code&gt;(Expression is 'async' but is not marked with 'await')&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let current = counter.count&lt;/code&gt; -&amp;gt; &lt;strong&gt;COMPILE ERROR.&lt;/strong&gt; &lt;code&gt;(Actor-isolated property 'count' can only be accessed 'async')&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To use it, you &lt;em&gt;must&lt;/em&gt; &lt;code&gt;await&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doAThing&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&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 &lt;code&gt;actor&lt;/code&gt; keyword told the compiler: "Hey. See this &lt;code&gt;count&lt;/code&gt; variable? And &lt;code&gt;increment()&lt;/code&gt;? They are &lt;strong&gt;isolated&lt;/strong&gt;. Anyone who wants to touch them from the outside? Make them get in line. Make them &lt;code&gt;await&lt;/code&gt;."&lt;/p&gt;

&lt;h3&gt;
  
  
  It's Just a Class (With a Bouncer)
&lt;/h3&gt;

&lt;p&gt;This is the best analogy. An actor is a nightclub.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its properties (&lt;code&gt;var count&lt;/code&gt;) and methods (&lt;code&gt;increment()&lt;/code&gt;) are &lt;em&gt;inside&lt;/em&gt; the club.&lt;/li&gt;
&lt;li&gt;The actor itself is the &lt;strong&gt;only thread&lt;/strong&gt; (metaphorically) allowed inside at a time.&lt;/li&gt;
&lt;li&gt;All those &lt;code&gt;await&lt;/code&gt; calls from other tasks are people lining up outside.&lt;/li&gt;
&lt;li&gt;The actor has a &lt;strong&gt;Mailbox&lt;/strong&gt; (a queue) which is the line.&lt;/li&gt;
&lt;li&gt;The Swift Runtime is the &lt;strong&gt;Bouncer&lt;/strong&gt; &lt;code&gt;(⌐■_■)&lt;/code&gt;. It picks the next person from the line, lets them in, waits for them to finish, and kicks them out before letting the next person in.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;          (The "Mailbox" Queue)
[Task 3] [Task 2] [Task 1] &amp;lt;--- Bouncer (Swift Runtime)
                             |
                             v
                       +--------------+
                       | Actor        |
                       | (Nightclub)  |
                       |              |
                       | var count = 0|  &amp;lt;-- "Safe" State
                       |              |
                       +--------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;Task 1&lt;/code&gt; (&lt;code&gt;await counter.increment()&lt;/code&gt;) runs, the bouncer lets it in. It has exclusive access. It messes with &lt;code&gt;count&lt;/code&gt;, finishes, and leaves. &lt;em&gt;Then&lt;/em&gt; the bouncer lets &lt;code&gt;Task 2&lt;/code&gt; in.&lt;/p&gt;

&lt;p&gt;No two tasks are ever inside at the same time. No data races. Beautiful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory &amp;amp; CPU: The Great Actor Misdirection
&lt;/h3&gt;

&lt;p&gt;This is the &lt;em&gt;next&lt;/em&gt; psych-out moment.&lt;br&gt;
"Okay, so an actor is a thread, right? It's got its own serial queue." &lt;strong&gt;NOPE.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the big lie. An actor is &lt;em&gt;not&lt;/em&gt; a thread. It does &lt;em&gt;not&lt;/em&gt; have its own &lt;code&gt;DispatchQueue&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where does an actor &lt;em&gt;live&lt;/em&gt;?&lt;/strong&gt;&lt;br&gt;
In memory, it's just a &lt;code&gt;class&lt;/code&gt;. It's a reference type. It lives on the &lt;strong&gt;heap&lt;/strong&gt;. It's a chunk of memory that holds its properties (like &lt;code&gt;count&lt;/code&gt;) and some internal bookkeeping stuff (like a lock and its mailbox).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where does an actor &lt;em&gt;run&lt;/em&gt;?&lt;/strong&gt;&lt;br&gt;
This is the crazy part. It &lt;em&gt;doesn't&lt;/em&gt; "run." Its &lt;em&gt;jobs&lt;/em&gt; run.&lt;/p&gt;

&lt;p&gt;Swift has a &lt;strong&gt;Cooperative Thread Pool&lt;/strong&gt;. When your app launches, the Swift Runtime creates a handful of threads, maybe as many as you have CPU cores. Let's say 8.&lt;/p&gt;

&lt;p&gt;Your app might have &lt;em&gt;thousands&lt;/em&gt; of &lt;code&gt;async&lt;/code&gt; tasks (including all your actor calls). The Swift Runtime (our Bouncer, also called the &lt;strong&gt;Executor&lt;/strong&gt;) is the big cheese. It &lt;em&gt;juggles&lt;/em&gt; all these thousands of tasks onto those 8 available threads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     Your Tasks (1000s of them)
[T1] [T2] [T3] [T4] ... [T999]
   \   |   /    /
    \  |  /    /
 (Swift Runtime Executor)
     |  |  |  |
     v  v  v  v
  +--+--+--+--+--+--+--+--+
  | T1| T4|   |T99|   |T23| ... (CPU Cores / Thread Pool)
  +--+--+--+--+--+--+--+--+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you &lt;code&gt;await counter.increment()&lt;/code&gt;, you aren't "blocking a thread." You're just creating a little job, handing it to the actor's mailbox, and your &lt;em&gt;current&lt;/em&gt; task &lt;em&gt;suspends&lt;/em&gt;. It says to the Runtime, "Hey, wake me up when this is done," and &lt;em&gt;gets off the thread&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The Runtime is now free to run a &lt;em&gt;different&lt;/em&gt; task on that thread. Maybe it's UI work. Maybe it's another actor.&lt;/p&gt;

&lt;p&gt;Eventually, the &lt;code&gt;increment()&lt;/code&gt; job gets to the front of the line. The Runtime says, "Okay, you're up," and slaps that job onto &lt;em&gt;any available thread&lt;/em&gt; from the pool. It runs, finishes, and the original task gets woken up.&lt;/p&gt;

&lt;p&gt;This is &lt;em&gt;so much more efficient&lt;/em&gt; than &lt;code&gt;DispatchQueue.sync&lt;/code&gt;, which just sits there, holding a whole thread hostage, doing nothing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Down to the Metal: What the Assembly &lt;em&gt;Actually&lt;/em&gt; Says
&lt;/h3&gt;

&lt;p&gt;So what does this look like in assembly? This is where it gets &lt;em&gt;really&lt;/em&gt; cursed.&lt;/p&gt;

&lt;p&gt;Let's look at three cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 1: &lt;code&gt;nonisolated&lt;/code&gt; (The Simple One)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have &lt;code&gt;nonisolated let id = "counter"&lt;/code&gt;, it's immutable. It's safe. The compiler knows this. Accessing it is just a normal, direct memory read.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; Just reading a 'let' property.
; rdi holds 'self' (the actor instance)
mov rax, [rdi + 0x18] ; Move value at offset 0x18 (the 'id' property) into rax
; ... (no locks, no queues, no magic)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's &lt;em&gt;fast&lt;/em&gt;. It's just a regular &lt;code&gt;class&lt;/code&gt; property read.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 2: &lt;em&gt;Internal&lt;/em&gt; Function (Already Inside the Club)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;What about when &lt;code&gt;increment()&lt;/code&gt; (which is &lt;code&gt;private&lt;/code&gt;) accesses &lt;code&gt;self.count&lt;/code&gt;? It's &lt;em&gt;already inside&lt;/em&gt; the club. It doesn't need to get in line. It's already been "de-queued" by the bouncer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; Inside 'increment()', accessing 'self.count'
; rdi holds 'self'
mov rbx, [rdi + 0x10] ; Move 'count' (at offset 0x10) into rbx
add rbx, 1            ; Add 1 to it
mov [rdi + 0x10], rbx ; Move it back
; ... (still no magic, just raw, fast access)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the "psych out" again. &lt;strong&gt;Inside the actor, all access is synchronous and fast.&lt;/strong&gt; It's just simple assembly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case 3: &lt;em&gt;External&lt;/em&gt; &lt;code&gt;await&lt;/code&gt; Call (Getting in Line)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the one. What does &lt;code&gt;await counter.increment()&lt;/code&gt; &lt;em&gt;actually&lt;/em&gt; compile to?&lt;/p&gt;

&lt;p&gt;You might expect a &lt;code&gt;JMP&lt;/code&gt; or &lt;code&gt;CALL&lt;/code&gt; to the &lt;code&gt;increment&lt;/code&gt; function. You would be wrong.&lt;/p&gt;

&lt;p&gt;You don't call &lt;code&gt;increment&lt;/code&gt; at all. The compiler &lt;em&gt;rewrites your code&lt;/em&gt;. It breaks your function into pieces (a "continuation") and tells the Swift Runtime to enqueue the &lt;em&gt;piece of code&lt;/em&gt; that calls &lt;code&gt;increment&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The assembly for &lt;code&gt;await counter.increment()&lt;/code&gt; looks less like &lt;code&gt;CALL&lt;/code&gt; and more like this (conceptually):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;; This is a WILD simplification
; rdi = the actor instance ('counter')
; rsi = the job to run (a function pointer to 'increment')
; ... (stuff to package up 'self')
CALL swift_actor_enqueue ; &amp;lt;-- THE MAGIC BOUNCER
; ...
CALL swift_task_suspend ; &amp;lt;-- "I'm going to sleep now, wake me later"
; ...
; (code resumes here, hours later, maybe on a different thread)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;That's it.&lt;/strong&gt; That's the whole trick.&lt;/p&gt;

&lt;p&gt;It's not "assembly" in the way we think of it. It's the compiler generating calls into the pre-built, C++-based &lt;strong&gt;Swift Runtime&lt;/strong&gt;. The &lt;code&gt;swift_actor_enqueue&lt;/code&gt; function is the bouncer. It takes the job, adds it to the actor's internal mailbox (with a lock, of course), and returns. Then &lt;code&gt;swift_task_suspend&lt;/code&gt; parks your current task.&lt;/p&gt;

&lt;p&gt;The complexity isn't in &lt;em&gt;your&lt;/em&gt; code's assembly. The complexity is hidden inside the runtime. It's an abstraction, and a damn good one.&lt;/p&gt;

&lt;h3&gt;
  
  
  Re-entrancy: The Part That Melts Your Brain
&lt;/h3&gt;

&lt;p&gt;Here's the last rabbit hole. And it's a doozy.&lt;br&gt;
What happens if an actor &lt;code&gt;await&lt;/code&gt;s &lt;em&gt;another&lt;/em&gt; &lt;code&gt;async&lt;/code&gt; function?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;actor&lt;/span&gt; &lt;span class="kt"&gt;BankAccount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;withdraw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&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="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Check 1&lt;/span&gt;

        &lt;span class="c1"&gt;// Uh oh...&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;permission&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;fraudCheckService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requestPermission&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;// We're back! ...or are we?&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;permission&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Check 2&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Wait, what? I already checked this!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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;When the code hits &lt;code&gt;await fraudCheckService...&lt;/code&gt;, it &lt;strong&gt;suspends&lt;/strong&gt;.&lt;br&gt;
But what does that &lt;em&gt;mean&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;It means the actor &lt;strong&gt;gives up its lock&lt;/strong&gt;. The bouncer says "Okay, you're waiting for the network, get out." And the bouncer immediately goes back to the mailbox and says, &lt;strong&gt;"NEXT!"&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(Inside withdraw())
  |
  v
await fraudCheck...
  |
  +-----&amp;gt; (Task suspends, leaves the actor)
  |
(Mailbox) [Task 2: another call to withdraw(75)]
  |
  v
(Bouncer lets Task 2 in, while Task 1 is suspended)
  |
  v
(Task 2 runs. balance is 100. amount is 75. Checks pass.
 balance becomes 25. Task 2 finishes.)
  |
  v
(Task 1's network call *finally* returns.
 It gets back in line at the mailbox.)
  |
  v
(Bouncer lets Task 1 back in. It resumes.)
  |
  v
if balance &amp;lt; amount... (balance is 25, amount is 100)
  |
  v
(Check 2 fails. Thank god.)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;re-entrancy&lt;/strong&gt;. The actor "re-enters" itself to run other jobs while one is paused. This is &lt;em&gt;wildly&lt;/em&gt; different from a &lt;code&gt;DispatchQueue.sync&lt;/code&gt; block, which would &lt;em&gt;never&lt;/em&gt; let go.&lt;/p&gt;

&lt;p&gt;This is why we had to check the balance &lt;em&gt;twice&lt;/em&gt;. Between the &lt;code&gt;await&lt;/code&gt;, our actor's state could have been completely changed by other tasks.&lt;/p&gt;

&lt;p&gt;This is the "beautifully cursed" part. It prevents deadlocks (because no one ever "holds" the lock while waiting), but it forces you to be &lt;em&gt;very&lt;/em&gt; careful about your state before and after every single &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lessons Learned
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Actors are abstractions, not threads.&lt;/strong&gt; The "magic" is 90% the Swift compiler and 10% the Swift Runtime.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The compiler is your friend.&lt;/strong&gt; It's what inserts the &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt; barrier, which is the &lt;em&gt;real&lt;/em&gt; protection.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access &lt;em&gt;inside&lt;/em&gt; an actor is fast.&lt;/strong&gt; It's just raw, synchronous property access. No locks, no queues.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access &lt;em&gt;outside&lt;/em&gt; an actor is a runtime call.&lt;/strong&gt; It's &lt;code&gt;swift_actor_enqueue&lt;/code&gt; and &lt;code&gt;swift_task_suspend&lt;/code&gt;. It's a state machine, not a simple &lt;code&gt;CALL&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;await&lt;/code&gt; means "pause and let someone else cut in."&lt;/strong&gt; This is re-entrancy. Always re-check your state after an &lt;code&gt;await&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Next Steps
&lt;/h2&gt;

&lt;p&gt;This is just the start. We didn't even talk about &lt;code&gt;MainActor&lt;/code&gt; (a special, global actor that &lt;em&gt;is&lt;/em&gt; tied to the main thread) or &lt;code&gt;Sendable&lt;/code&gt; (a protocol that tells the compiler which types are &lt;em&gt;safe&lt;/em&gt; to throw over the wall into an actor).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  (\_/)
  (O.o)
 (")_(")
(Down the next rabbit hole...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But now, when you type &lt;code&gt;actor&lt;/code&gt;, you know what you're &lt;em&gt;really&lt;/em&gt; doing. You're not just making a class. You're building a tiny, isolated island, hiring a bouncer, and telling the compiler to manage the line-up. And that's pretty cool.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>code</category>
    </item>
    <item>
      <title>I Wrote HTTP with Rust (From Scratch)</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Tue, 18 Nov 2025 07:14:54 +0000</pubDate>
      <link>https://forem.com/annurdien/i-wrote-http-with-rust-from-scratch-3486</link>
      <guid>https://forem.com/annurdien/i-wrote-http-with-rust-from-scratch-3486</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;The best way to understand something is tried to implement it by yourself! I saw a guy on YouTube implement HTTP in C from scratch (including the TCP), I'm curious, can I do it in Rust, well I just did, (with unsafe yet not optimized code)! Now, before we dive into this rabbit hole, first we need to understand what is HTTP, but to understand HTTP, we need to know what is TCP/IP model and how it works.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;WARNING: this is just a crazy implementation, this is not safe code, this is actually not great implementation and most importantly don't think this code is ready for production. Also We're assuming in your system, there's already TCP implementation, so we don't write TCP here.&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The TCP/IP Model
&lt;/h3&gt;

&lt;p&gt;So the TCP/IP model, also known as the Internet Protocol Suite, is a conceptual framework for how data is transmitted over a network. It is divided into four layers, each with specific functions. These layers are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Application Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Transport Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Internet Layer&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Network Interface Layer&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's look 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;+--------------------+
| Application Layer  |
|  - HTTP            |
|  - FTP             |
|  - SMTP            |
|  - DNS             |
+--------------------+
| Transport Layer    |
|  - TCP             |
|  - UDP             |
+--------------------+
| Internet Layer     |
|  - IP              |
|  - ICMP            |
|  - ARP             |
+--------------------+
| Network Interface  |
|  - Ethernet        |
|  - Wi-Fi           |
|  - ARP             |
+--------------------+
| Physical Layer     |
|  - Physical Media  |
+--------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Layer Functions:
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Application Layer&lt;/strong&gt;: It provides network services to the applications of the user. This is where high-level protocols like HTTP, FTP, SMTP, and DNS resides.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transport Layer&lt;/strong&gt;: Ensures reliable data transfer between hosts. It is responsible for error detection and correction, data flow control, and segmentation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Internet Layer&lt;/strong&gt;: Determines the best path through the network for data to travel. It handles packet routing, addressing, and fragmentation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Network Interface Layer&lt;/strong&gt;: Manages the hardware connections and data transfer between adjacent network nodes. It's'used for framing, physical addressing, and error detection on the physical link.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is TCP?
&lt;/h3&gt;

&lt;p&gt;The Transmission Control Protocol (TCP) is a core protocol of the Internet Protocol Suite. It provides reliable, ordered, and error-checked delivery of data between applications running on hosts communicating via an IP network. TCP is connection-oriented, meaning a connection is established and maintained until the applications at each end have finished exchanging messages.&lt;/p&gt;

&lt;h4&gt;
  
  
  TCP Packet Structure
&lt;/h4&gt;

&lt;p&gt;A TCP packet (or segment) structure is divided into several fields, each serving a specific purpose for establishing and maintaining a reliable connection. Below is a simplified diagram of a TCP packet:&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%2Fuj5qwu6g8n89e8nawpex.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%2Fuj5qwu6g8n89e8nawpex.jpg" alt="Image" width="600" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You don't really need to understand what are those data are used for, but basically, it enables TCP to reliably send data trough network.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is HTTP?
&lt;/h3&gt;

&lt;p&gt;The Hypertext Transfer Protocol (HTTP) is an application layer protocol used for transmitting hypermedia documents, such as HTML. It's a long story, here are two articles explain how HTTP actually works!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview" rel="noopener noreferrer"&gt;Noob friendly article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.rfc-editor.org/rfc/rfc2616" rel="noopener noreferrer"&gt;Not so noob friendly article&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  HTTP Request Header Structure
&lt;/h4&gt;

&lt;p&gt;An HTTP request header is the format in which the client sends data to the server. It consists of a request line, header fields, and an optional message body. Here is a simplified diagram:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+-----------------------------------------+
| Request Line                            |
| GET /index.html HTTP/1.1                |
+-----------------------------------------+
| Header Fields                           |
| Host: www.example.com                   |
| User-Agent: Mozilla/5.0                 |
| Accept: text/html                       |
| ...                                     |
+-----------------------------------------+
|                                         |
| Optional Message Body (for POST, etc.)  |
| ...                                     |
+-----------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Components&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Request Line&lt;/strong&gt;: Contains the HTTP method, the resource path, and the HTTP version.

&lt;ul&gt;
&lt;li&gt;Example: &lt;code&gt;GET /index.html HTTP/1.1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Header Fields&lt;/strong&gt;: Key-value pairs providing metadata about the request.

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Host&lt;/strong&gt;: The domain name of the server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;User-Agent&lt;/strong&gt;: Information about the client software.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accept&lt;/strong&gt;: Media types the client is willing to receive.&lt;/li&gt;
&lt;li&gt;Other headers may include &lt;code&gt;Content-Type&lt;/code&gt;, &lt;code&gt;Content-Length&lt;/code&gt;, &lt;code&gt;Connection&lt;/code&gt;, etc.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Optional Message Body&lt;/strong&gt;: Present in requests like POST, PUT, etc., and contains the data to be sent to the server.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;In my implementation, I ignore most of the request header, because I don't need most of them in this implementation. Only GET request that are handled in this project for the sake of simplicity, the point is I understand how HTTP works (just an excuse because I'm lazy)&lt;/p&gt;

&lt;h3&gt;
  
  
  How TCP and HTTP Work Together
&lt;/h3&gt;

&lt;p&gt;When you visit a website, your browser (the client) initiates a TCP connection to the server hosting the website. Once the connection is established, the browser sends an HTTP request over the TCP connection to retrieve the web page. The server processes the request and sends back an HTTP response with the requested resource. This process involves the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TCP Handshake&lt;/strong&gt;: Establishes a connection between client and server.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                           Server
|-------[SYN]--------------------&amp;gt;|
|&amp;lt;------[SYN, ACK]----------------|
|-------[ACK]--------------------&amp;gt;|
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Sending HTTP Request&lt;/strong&gt;: Once the TCP connection is established, the client sends an HTTP request.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                           Server
|-------[GET /index.html HTTP/1.1\r\n]--&amp;gt;|
|-------[Host: www.example.com\r\n]------|
|-------[User-Agent: Mozilla/5.0\r\n]----|
|-------[\r\n]--------------------------&amp;gt;|
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Server Response&lt;/strong&gt;: The server processes the request and sends back an HTTP response.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                           Server
|&amp;lt;------[HTTP/1.1 200 OK\r\n]-------------|
|&amp;lt;------[Content-Type: text/html\r\n]-----|
|&amp;lt;------[Content-Length: 137\r\n]---------|
|&amp;lt;------[\r\n]----------------------------|
|&amp;lt;------[&amp;lt;html&amp;gt;...content...&amp;lt;/html&amp;gt;]------|
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;TCP Termination&lt;/strong&gt;: After the data transfer, the TCP connection is closed.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                           Server
|-------[FIN]--------------------&amp;gt;|
|&amp;lt;------[FIN, ACK]----------------|
|-------[ACK]--------------------&amp;gt;|
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Building the HTTP
&lt;/h3&gt;

&lt;p&gt;Here's the code for this super simple HTTP implementation that I wrote from scratch, I use tokio for the TCP. Because I don't want to write&lt;br&gt;
TCP by myself (for now)! &lt;/p&gt;

&lt;h4&gt;
  
  
  The Code
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;net&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;TcpListener&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TcpStream&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::{&lt;/span&gt;&lt;span class="n"&gt;AsyncReadExt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;AsyncWriteExt&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;path&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;mime_guess&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;from_path&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;RwLock&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nn"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Arc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;HEADER_PACKET_LENGTH&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;AllowedFileTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Arc&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;RwLock&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;#[tokio::main]&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&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;let&lt;/span&gt; &lt;span class="n"&gt;allowed_file_table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Arc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;RwLock&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;create_allowed_file_table&lt;/span&gt;&lt;span class="p"&gt;()));&lt;/span&gt;

    &lt;span class="c1"&gt;// Bind a TCP listener to the specified address and port.&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;TcpListener&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"127.0.0.1:7878"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Server listening on port 7878"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;loop&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&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;listener&lt;/span&gt;&lt;span class="nf"&gt;.accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;allowed_file_table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Arc&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;clone&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;allowed_file_table&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nn"&gt;tokio&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;spawn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;move&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;handle_client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allowed_file_table&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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;span class="cd"&gt;/// Handles the client connection.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// Reads the request from the client, checks if the requested file is allowed,&lt;/span&gt;
&lt;span class="cd"&gt;/// and sends the appropriate response.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// # Arguments&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// * `socket` - The TCP stream representing the client connection.&lt;/span&gt;
&lt;span class="cd"&gt;/// * `allowed_file_table` - The allowed file table to check for file access.&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;handle_client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;TcpStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allowed_file_table&lt;/span&gt;

&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;AllowedFileTable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&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;HEADER_PACKET_LENGTH&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="nf"&gt;.read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;n&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="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_utf8_lossy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="nf"&gt;.lines&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;first_line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lines&lt;/span&gt;&lt;span class="nf"&gt;.next&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_line&lt;/span&gt;&lt;span class="nf"&gt;.split_whitespace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&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;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&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="s"&gt;"GET"&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Extract the requested path.&lt;/span&gt;
                &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="nf"&gt;.trim_start_matches&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sc"&gt;'/'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="c1"&gt;// Check if the requested file is allowed.&lt;/span&gt;
                &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allowed_file_table&lt;/span&gt;&lt;span class="nf"&gt;.read&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                    &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="nf"&gt;.iter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.find&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="nf"&gt;.ends_with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="nf"&gt;.cloned&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="p"&gt;};&lt;/span&gt;

                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;full_path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;allowed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="c1"&gt;// Send the content if the file is allowed.&lt;/span&gt;
                    &lt;span class="nf"&gt;send_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;full_path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="c1"&gt;// Send a 403 Forbidden response if the file is not allowed.&lt;/span&gt;
                    &lt;span class="nf"&gt;send_forbidden_packet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Send a 400 Bad Request response if the request is not a valid GET request.&lt;/span&gt;
                &lt;span class="nf"&gt;send_bad_request_packet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/// Sends a 403 Forbidden response to the client.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// # Arguments&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// * `socket` - The TCP stream representing the client connection.&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;send_forbidden_packet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;TcpStream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"HTTP/1.1 403 Forbidden&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/html&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 0&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Forbidden request received"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/// Sends a 400 Bad Request response to the client.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// # Arguments&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// * `socket` - The TCP stream representing the client connection.&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;send_bad_request_packet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;TcpStream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"HTTP/1.1 400 Bad Request&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/html&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 0&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bad request received"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/// Sends the content of the requested file to the client.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// Reads the file, determines its MIME type, and sends it along with the HTTP response headers.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// # Arguments&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// * `path` - The path of the file to send.&lt;/span&gt;
&lt;span class="cd"&gt;/// * `socket` - The TCP stream representing the client connection.&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;send_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;TcpStream&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Determine the MIME type based on the file extension.&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;content_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;from_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.first_or_octet_stream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;format!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s"&gt;"HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Type: {}&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Length: {}&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Access-Control-Allow-Origin: *&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\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;content_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="nf"&gt;.len&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="nf"&gt;.as_bytes&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="nf"&gt;.write_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Send a 403 Forbidden response if the file could not be read.&lt;/span&gt;
        &lt;span class="nf"&gt;send_forbidden_packet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="k"&gt;.await&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="cd"&gt;/// Creates and initializes the allowed file table.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// Scans the predefined list of file paths and adds existing files to the allowed file table.&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// # Returns&lt;/span&gt;
&lt;span class="cd"&gt;///&lt;/span&gt;
&lt;span class="cd"&gt;/// A vector of strings representing the allowed file paths.&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;create_allowed_file_table&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;paths&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"./public/index.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"./public/style.css"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Vec&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;paths&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nn"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.exists&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;table&lt;/span&gt;&lt;span class="nf"&gt;.push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="nf"&gt;.to_string&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="n"&gt;table&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Initialization&lt;/strong&gt;: The main function initializes the allowed file table and starts the TCP listener on port 7878.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Accepting Connections&lt;/strong&gt;: The server listens for incoming connections in an infinite loop. For each new connection, it spawns a new task to handle the client.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Handling Requests&lt;/strong&gt;: The &lt;code&gt;handle_client&lt;/code&gt; function reads the request from the client. It checks if the request is a valid HTTP GET request and whether the requested file is allowed. If the file is allowed, it serves the content; otherwise, it sends a 403 Forbidden response.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sending Responses&lt;/strong&gt;: There are helper functions to send different types of responses (&lt;code&gt;send_forbidden_packet&lt;/code&gt;, &lt;code&gt;send_bad_request_packet&lt;/code&gt;, and &lt;code&gt;send_content&lt;/code&gt;). The &lt;code&gt;send_content&lt;/code&gt; function reads the requested file, determines its MIME type, and sends the file content along with appropriate HTTP headers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Limitations
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Concurrency and Performance&lt;/strong&gt;: Simple implementations may struggle with handling a high number of concurrent connections and large data volumes efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security&lt;/strong&gt;: Basic servers do not provide robust security mechanisms like SSL/TLS, making data susceptible to interception and tampering.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Without features like load balancing, session management, and advanced routing, scaling out to handle increased traffic can be challenging.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functionality&lt;/strong&gt;: Limited to basic GET requests and static file serving. More advanced HTTP methods and dynamic content generation are not supported.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Here's the result
&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%2Fcdn.alja.bar%2FScreenshot%25202024-07-12%2520at%252001.50.34.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%2Fcdn.alja.bar%2FScreenshot%25202024-07-12%2520at%252001.50.34.png" alt="GET" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What Next
&lt;/h3&gt;

&lt;p&gt;If you read this, I think your interested in programming, if you don't understand what I said here, it's totally fine, I don't really understand it either, especially how the TCP stream works, but damn, being stupid is fun right, you can always learning new things everyday, it's like endless journey!. I'm having so much fun writing this article. For next I think I will try to write WebSocket from scratch.&lt;/p&gt;

&lt;p&gt;BTW the here's the github &lt;a href="https://github.com/annurdien/http-rust.git" rel="noopener noreferrer"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Original Post: &lt;a href="https://rasyid.codes/blog/rust-http/" rel="noopener noreferrer"&gt;https://rasyid.codes/blog/rust-http/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>rust</category>
      <category>http</category>
    </item>
    <item>
      <title>Enhance Debugging in Swift with #file, #line, and #function</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Wed, 04 Dec 2024 03:40:23 +0000</pubDate>
      <link>https://forem.com/annurdien/enhance-debugging-in-swift-with-file-line-and-function-4a5f</link>
      <guid>https://forem.com/annurdien/enhance-debugging-in-swift-with-file-line-and-function-4a5f</guid>
      <description>&lt;p&gt;Swift provides three special literals: &lt;code&gt;#file&lt;/code&gt;, &lt;code&gt;#line&lt;/code&gt;, and &lt;code&gt;#function&lt;/code&gt;. These are automatically set to the current file name, line number, and function name, respectively. They are especially handy when creating custom logging functions or test predicates.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;Foundation&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;#file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;line&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;#line&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;function&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;#function&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"[&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;line&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;] &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt; - &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="se"&gt;)&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="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello world!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// [TryDebug.playground:8] foo() - Hello world!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>swift</category>
      <category>iosdev</category>
      <category>ios</category>
    </item>
    <item>
      <title>Directly store value using "if expression" - Swift</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Wed, 04 Dec 2024 03:22:02 +0000</pubDate>
      <link>https://forem.com/annurdien/directly-store-value-using-if-expression-18de</link>
      <guid>https://forem.com/annurdien/directly-store-value-using-if-expression-18de</guid>
      <description>&lt;p&gt;When we want to set a variable for a specific condition, this what we normally do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;temperatureInCelsius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;weatherAdvice&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temperatureInCelsius&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&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;weatherAdvice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"It's very cold. Consider wearing a scarf."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temperatureInCelsius&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;weatherAdvice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"It's really warm. Don't forget to wear sunscreen."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;weatherAdvice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"It's not that cold. Wear a T-shirt."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weatherAdvice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Prints "It's not that cold. Wear a T-shirt."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can make it more cleaner using &lt;code&gt;if expression&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;temperatureInCelsius&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;weatherAdvice&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temperatureInCelsius&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"It's very cold. Consider wearing a scarf."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;temperatureInCelsius&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"It's really warm. Don't forget to wear sunscreen."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"It's not that cold. Wear a T-shirt."&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weatherAdvice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Prints "It's not that cold. Wear a T-shirt."&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Learn more: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow#Conditional-Statements" rel="noopener noreferrer"&gt;https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow#Conditional-Statements&lt;/a&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
    </item>
    <item>
      <title>Developing CROML (Crochet Obvious Minimal Language)</title>
      <dc:creator>Annurdien Rasyid</dc:creator>
      <pubDate>Thu, 19 Sep 2024 16:07:33 +0000</pubDate>
      <link>https://forem.com/annurdien/developing-croml-crochet-obvious-minimal-language-fl0</link>
      <guid>https://forem.com/annurdien/developing-croml-crochet-obvious-minimal-language-fl0</guid>
      <description>&lt;h2&gt;
  
  
  Back Story
&lt;/h2&gt;

&lt;p&gt;During my time at Apple Academy, my colleagues and I developed an app about crocheting. The app is designed to help crocheters track their stitch and row. In the app, we have a feature that allows users to take pictures of an Amigurumi pattern and then break it down into individual stitches. The goal is to enable users to know what stitch they need to do at that exact moment.&lt;br&gt;
 &lt;/p&gt;
&lt;h2&gt;
  
  
  Crochet
&lt;/h2&gt;

&lt;p&gt;For you non-crocheters, let me give you a context. A pattern in crocheting is a "rule" that you need to follow to create a certain shape; it consists of what type of stitch, how many times the stitch should be done, and in which row it should be done. Amigurumi is a type of crochet. It's basically a doll.&lt;/p&gt;

&lt;p&gt;Let me give you an example of an Amigurumi pattern:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5jj8jlbkonip0eokf5h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5jj8jlbkonip0eokf5h.png" alt="Pattern for Pikachu" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's another one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe2z3s0a6igh6h7scy6pn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe2z3s0a6igh6h7scy6pn.png" alt="Pattern for bear" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You see, the pattern can be very different from one another; it has multiple abbreviations, and sometimes it includes natural language. &lt;/p&gt;
&lt;h2&gt;
  
  
  Challange
&lt;/h2&gt;

&lt;p&gt;As I mentioned before, one of the features in our app is allowing the user to take a picture or upload a screenshot of this pattern, and then from that picture, the app should break it down into individual stitches that the user can follow each stitch. &lt;/p&gt;

&lt;p&gt;There are multiple problems I face while developing this feature:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to extract the crochet pattern string from an image.&lt;/li&gt;
&lt;li&gt;How to convert that extracted string, which is a semi-structured language and sometimes has natural language, to data that a computer can understand.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the first problem, which is to extract the string from the image, it's pretty straightforward. I use &lt;strong&gt;Apple Vision Framework&lt;/strong&gt; to detect text in an image; it's easy!. For the second problem though, it's a bit tricky. I try using &lt;strong&gt;Regex&lt;/strong&gt; and &lt;strong&gt;Named Entity Recognition&lt;/strong&gt;, and it's just not working because the nature of the patterns is very different from one to the other. Also,  sometimes the Vision Framework can output incorrect data; sometimes it detects "S" as "5".&lt;/p&gt;

&lt;p&gt;Based on that reason, the obvious solution that came to my head is using &lt;strong&gt;Large Language Model&lt;/strong&gt; to analyze the pattern and convert it to more structured data.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why not using JSON?
&lt;/h2&gt;

&lt;p&gt;The first thing that I did was create a prompt that tells the LLM to convert the crochet pattern to a JSON format. It's pretty straightforward, right? No need to process any further, just straight JSON data. Well, maybe it's true, but there are some problems with that. &lt;/p&gt;

&lt;p&gt;Here's the first 20 lines from 456 of the crochet pattern in JSON format, pretty long, right?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "pattern": {
    "parts": [
      {
        "name": "Head",
        "layers": [
          {
            "id": 1,
            "sequences": [
              {
                "stiches": [
                  {
                    "name": "SC",
                    "times": 6
                  }
                ],
                "repeat": 1
              }
            ]
          }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problems with generating JSON directly with LLM are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;JSON&lt;/code&gt; is error-prone if it's generated by LLM; it's possible, and I encounter that many times the &lt;code&gt;JSON&lt;/code&gt; data that the LLM output is not valid, sometimes it mises the (&lt;code&gt;}&lt;/code&gt;semicolon) at the end.&lt;/li&gt;
&lt;li&gt;Returning pure &lt;code&gt;JSON&lt;/code&gt; data is very token expensive because crochet patterns can have many rows and stitches, and it also takes a very long time to generate the output.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The other solution is not to use &lt;code&gt;JSON&lt;/code&gt;. I've tried using minimal language like &lt;code&gt;TOML&lt;/code&gt; or &lt;code&gt;YAML&lt;/code&gt;; it just doesn't feel right; they have too much indentation and are just not that readable for the context of a crochet pattern.&lt;/p&gt;

&lt;h2&gt;
  
  
  CROML
&lt;/h2&gt;

&lt;p&gt;Because of that reason, I decided to create my own data representation that I can easily parse locally. I came up with &lt;code&gt;CROML&lt;/code&gt; (Crochet Obvious Markup Language). Why, you ask? Here's a quote from Kent Beck.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make it work, make it right, and make it fast. – Kent Beck&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's how CROML looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Head:
 1:SCx6
 2:INCx6
 3:(SCx1,INCx1)[r=6]
 4:(SCx2,INCx1)[r=6]
 5:(SCx3,INCx1)[r=6]
 6-8:SCx30
 9:SCx6,INCx3,(SCx1,INCx1,SCx1)[r=4],INCx3,SCx6

Ears:
 1:SCx6
 2:(SCx1,INCx1)[r=3]
 3:SCx9
 4:(SCx1,INCx1,SCx1)[r=3]
 5-8:SCx12
 9:(SCx2,DECx1)[r=3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CROML Rules 
&lt;/h2&gt;

&lt;h4&gt;
  
  
  1. Parts and Sections
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Each part (e.g., &lt;strong&gt;Body&lt;/strong&gt;, &lt;strong&gt;Head&lt;/strong&gt;, &lt;strong&gt;Paws&lt;/strong&gt;, etc.) is introduced by its name (only a-zA-Z are allowed), followed by a colon (&lt;code&gt;:&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Each part contains layers. These layers are numbered sequentially and correspond to the crochet steps.&lt;/li&gt;
&lt;li&gt;If a series of layers have the same sequence, they can be represented as a range (e.g., &lt;code&gt;6-10&lt;/code&gt; for five layers with identical stitches).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Stitch Notation
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Each stitch type is represented by its abbreviation (e.g., &lt;code&gt;SC&lt;/code&gt; for single crochet, &lt;code&gt;INC&lt;/code&gt; for increase, &lt;code&gt;DEC&lt;/code&gt; for decrease).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The number of times a stitch is repeated in a layer is denoted by the format &lt;code&gt;StitchName x Count&lt;/code&gt;, where &lt;code&gt;StitchName&lt;/code&gt; is the abbreviation, and &lt;code&gt;Count&lt;/code&gt; is the number of times the stitch is performed (e.g., &lt;code&gt;SCx6&lt;/code&gt; for six single crochets).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Multiple Stitches in a Sequence
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When multiple stitches occur in the same sequence, group them with parentheses &lt;code&gt;( )&lt;/code&gt; to show they occur together, separated by commas.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;(SCx1, INCx1)&lt;/code&gt; means one single crochet followed by one increase.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Repeating Sequences
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;If a sequence (group of stitches) is repeated for a number of rounds, append &lt;code&gt;[r=x]&lt;/code&gt;, where &lt;code&gt;x&lt;/code&gt; is the number of repetitions.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;(SCx1, INCx1) [r=6]&lt;/code&gt; means to repeat “single crochet 1 stitch, increase 1 stitch” six times in a row.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  5. Single Repetitions
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;If the sequence is only done once, no &lt;code&gt;[r=x]&lt;/code&gt; is necessary. By default, no repeat means it occurs only once.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;SCx6&lt;/code&gt; assumes a default repeat of 1 unless otherwise specified.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  6. Layer Ranges
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;If multiple layers have the same stitch pattern, use a hyphen &lt;code&gt;(-)&lt;/code&gt; to represent the range.&lt;/li&gt;
&lt;li&gt;Example: &lt;code&gt;6-10: SCx20&lt;/code&gt; means layers 6 through 10 are all single crocheted 20 times.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  7. Order of Stitch Types
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Within parentheses, list stitches in the order they occur.&lt;/li&gt;
&lt;li&gt;When no parentheses are used, the notation assumes a single stitch pattern for that layer.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How About The Parser?
&lt;/h2&gt;

&lt;p&gt;It's pretty straightforward; &lt;strong&gt;CROML&lt;/strong&gt; is agnostic to indentation; I just use simple regex and then convert it to JSON locally; it's much faster than having to wait for LLM to complete generating JSON data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion 
&lt;/h2&gt;

&lt;p&gt;Telling LLM to generate &lt;strong&gt;CROML&lt;/strong&gt; is significantly reducing token usage and also speeding up the generation time. While maybe still not error proof, it's better than having a parse error because of just 1 character missing if I use &lt;strong&gt;JSON&lt;/strong&gt;. &lt;/p&gt;

</description>
      <category>llm</category>
      <category>machinelearning</category>
      <category>ios</category>
      <category>softwaredevelopment</category>
    </item>
  </channel>
</rss>
