{"id":26027570,"url":"https://github.com/teodutu/compiler-workshop","last_synced_at":"2026-04-20T15:02:49.954Z","repository":{"id":280166955,"uuid":"941160443","full_name":"teodutu/compiler-workshop","owner":"teodutu","description":"Workshop on compilers featuring the DMD as frontend and LLVM as backend","archived":false,"fork":false,"pushed_at":"2025-03-01T17:09:14.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T18:22:49.968Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/teodutu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-01T16:27:45.000Z","updated_at":"2025-03-01T17:09:18.000Z","dependencies_parsed_at":"2025-03-01T18:22:53.361Z","dependency_job_id":"1d34d23c-eded-4e54-99cd-b3dde2fb075b","html_url":"https://github.com/teodutu/compiler-workshop","commit_stats":null,"previous_names":["teodutu/compiler-workshop"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodutu%2Fcompiler-workshop","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodutu%2Fcompiler-workshop/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodutu%2Fcompiler-workshop/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/teodutu%2Fcompiler-workshop/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/teodutu","download_url":"https://codeload.github.com/teodutu/compiler-workshop/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242249710,"owners_count":20096887,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-06T16:42:15.841Z","updated_at":"2026-04-20T15:02:44.907Z","avatar_url":"https://github.com/teodutu.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Compiler-Workshop\n\nWorkshop on compilers featuring the DMD as frontend and LLVM as backend\n\n## DLang\n\n[D](https://dlang.org/) is a general-purpose systems-level programming language that excels at metaprogramming and design-by-introspection (basically querying the compiler at compile-time and generating different code based on these queries).\nOn top of these, D also provides a memory-safety mechanism which is less compiler-heavy but also less rigorous than that of Rust.\n\nTo install it, run this command (don't forget to run the `source` command printed by the install script to activate the compiler):\n\n```bash\ncurl -fsS https://dlang.org/install.sh | bash -s dmd\n```\n\nWe'll display some of D's features highlighted above.\nEnter the `d-features` folder.\n\n### [`ctfe.d`](d-features/ctfe.d)\n\nThis file shows that D is capable of running functions at compile-time provided their inputs are known to the compiler.\nThis feature is called **Compile-Time Function Execution (CTFE)**.\n\nRun `make ctfe`.\nNotice that simply compiling the code prints the strings in the `main()` function.\nThis is possible because both `jsonConfig` and `parsedConfig` are `immutable` which makes them known at compile time because their value cannot change.\n\n### `__traits()`\n\nThis is in my opinion the bread and butter of D's strong metaprogramming.\n`__traits()` is a construction that interrogates the compiler at compile-time.\nIt asks the compiler for various information that can then be used to customise the generated code.\n\nThis feature works well in conjunction with `static if` statements which use the syntax of regular `if`s but are evaluated at **compile-time**.\nSimilarly, `staic foreach` statements are `foreach` statements that unrolled and their code is generated rather than run at compile-time.\nBy far one of the coolest `__traits()` is `__traits(compiles, /* Some D code */)` which is `true` if the D code compiles and is often used like this:\n\n```D\nstatic if (__traits(compiles, /* Some D code */))\n    /* The same D code */\n```\n\nI.e. if some code compiles, use it.\n\nFollow the code in [`d-features/traits_compiles.d`](d-features/traits_compiles.d).\nRun it and figure out how it works.\n\n### `mixin`\n\nYou can also encounter `__traits()` in [`d-features/auto_string.d`](d-features/auto_string.d).\nIn this snippet we use design-by-introspection to automatically generate the `toString()` method for any `struct` / `class`.\nTo generate and insert code at compile-time we use the `mixin` keyword.\nGo through the code mentioned above and notice how `mixin` works with the template `AutoToString` which in turn uses `__traits(getMember)` to access the members of a generic `struct` or `class`.\n\n### [`safe.d`](d-features/safe.d)\n\nSafety is enforeced in D mostly at the function level by using the `@safe` keyword.\nThis is a [rather complex mechanism](https://dlang.org/spec/memory-safe-d.html). that works mostly by forbidding certain memory-related actions such as pointer arithmetic.\n\nInspect the code in `d-features/safe.d`.\nCompile it with `make safe` and run it with `./safe`.\nNotice that it incorrectly prints the 11th element of the array.\n\n**Task:** Change `unsafe_func()` from `@trusted` to `@safe` and make the code compile without changing its functionality (it must still print the 11th element of the array).\nWe dare you, we double dare you!\nYou have a partial solution in the `safe_func()` function, but that function produces an error at runtime.\nWhy?\n\n## LLVM\n\n[LLVM](https://llvm.org/) is one of the most popular compiler backends and the one used by the [`ldc` D compiler](https://github.com/ldc-developers/ldc).\nIts most powerful feature and what makes LLVM so customisable is its **intermediate representation** called [LLVM IR](https://mcyoung.xyz/2023/08/01/llvm-ir/).\nTo install it, follow the instructions [here](https://llvm.org/docs/GettingStarted.html#getting-the-source-code-and-building-llvm).\nThis may take a while and quite a bit of storage space, but it's well worth it.\n\nThis IR is a mid-point between the high-level AST created by the frontend and the low-level and machine-dependent assembly output by the compiler.\nIts assembly-like structure allows for easy optimisations via **LLVM passes**.\nThese passes are functions that are compiled into libraries that can be loaded with the `opt` tool to modify existing LLVM IR.\n\nFollow the code in the `llvm/` directory.\nWe'll work on [`sample.c`](llvm/sample.c).\nThe LLVM pass is [`FuncionPass.cpp`](llvm/FunctionPass.cpp).\nIt just prints the functions and instructions found and modifies all `add` instructions into `sub`.\n\n1. Run `make build-pass` to build `build/libFunctionPass.so`.\nThis library contains the code in `FunctionPass.cpp`.\n\n1. Run `make generate-llvmir` to generate the LLVM IR code corresponding to `sample.c`.\nInspect the code in `sample.ll`.\n\n1. Run `make run-pass` to run the LLVM pass.\nNotice the output then inspect the resulting file `sample_edited.ll`.\n\n1. Run `make llvm-interpret` to interpret `sample_edited.ll` using LLVM's interpreter `lli`.\n\n1. Run `make link-llvm` to generate the `sample_edited` executable from `sample_edited.ll`.\nThis uses `llc` to convert the LLVM IR code to Assembly and `clang` to assemeble and link it into an executable.\n\n1. Run `make clean` to delete all resulting files and `make celean-pass` to remove the `build/` folder.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteodutu%2Fcompiler-workshop","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fteodutu%2Fcompiler-workshop","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fteodutu%2Fcompiler-workshop/lists"}