[HN Gopher] DMD Compiler as a Library: A Call to Arms
       ___________________________________________________________________
        
       DMD Compiler as a Library: A Call to Arms
        
       Author : ingve
       Score  : 59 points
       Date   : 2024-02-22 11:55 UTC (11 hours ago)
        
 (HTM) web link (dlang.org)
 (TXT) w3m dump (dlang.org)
        
       | docandrew wrote:
       | Given how fast the D compiler is, this might be a nice
       | alternative to something like LuaJIT for an embedded scripting
       | language in other apps.
        
         | WalterBright wrote:
         | Many people do use D as a scripting language, replacing their
         | use of bash. D programs can be compiled and run in one step.
         | #!/usr/bin/env rdmd         import std.stdio;         void
         | main()         {             writeln("Hello, world with
         | automated script running!");         }                   >
         | ./myprog.d         Hello, world with automated script running!
         | 
         | https://dlang.org/rdmd.html
        
           | thechao wrote:
           | Classically, you wait until your name is said _three_ times,
           | before appearing before the supplicant!
        
           | tripleo1 wrote:
           | Do you also have:
           | 
           | 1. Statically linked executables (essential but not crucial)
           | 
           | 2. Dependency management (import tripleo.rtfm from
           | "git:...#1234567")
           | 
           | 3. A good AST API (jdt/roaster, javapoet, etc)
           | 
           | 4. Tooling (cf spoon/soot)
           | 
           | (I approve this comment, even given it's limited
           | usefulness/expressivity)
        
             | WalterBright wrote:
             | 1. yes
             | 
             | 2. no
             | 
             | 3. that's what this topic is about
             | 
             | 4. yes
        
             | bachmeier wrote:
             | > 2. Dependency management (import tripleo.rtfm from
             | "git:...#1234567")
             | 
             | Not built into the compiler, but Dub, the official package
             | manager distributed with the compiler, should be what I
             | think your concise statement is referring to.
        
         | p0nce wrote:
         | This is what HipremeEngine does for example.
         | https://github.com/MrcSnm/HipremeEngine
        
       | UncleEntity wrote:
       | Swift does something like this I believe. Not too sure on the
       | details but I remember reading about how you can modify the
       | parser to do...things.
       | 
       | Looked interesting.
        
         | favorited wrote:
         | It's part of the LLVM philosophy that the compiler tools should
         | be usable as a library. There was a not insignificant amount of
         | drama[0] when the folks working on LLD (the then-new LLVM
         | linker) decided they weren't going to follow that pattern. It
         | was another 5ish years before they revisited that decision[1],
         | but it's not a simple task to retrofit a project with proper
         | error handling (exit on error is fine for a binary, but not a
         | library), memory management (leaking is faster than freeing
         | memory for a short lived process, but isn't acceptable in a
         | library), etc.
         | 
         | [0]https://discourse.llvm.org/t/lld-status-update-and-
         | performan...
         | 
         | [1]https://discourse.llvm.org/t/rfc-revisiting-lld-as-a-
         | library...
        
           | mhh__ wrote:
           | I'd go a step further: the compiler should _solely_ be a way
           | to run the library, no extra code.
        
       | berkeleynerd wrote:
       | This looks like more fun than sudoku or crossword puzzles.
        
         | WalterBright wrote:
         | I can vouch for it being a lot more fun! Doing puzzles is a
         | useless waste of time.
        
           | klyrs wrote:
           | Not true! Doing puzzles can help mitigate risk for dementia.
           | Probably not necessary for those of us who do puzzling work
           | day to day, but there is a solid benefit to society if people
           | are in the habit of doing puzzles.
        
             | jraph wrote:
             | > puzzling work
             | 
             | You don't say. Appalling, even!
        
             | WalterBright wrote:
             | Working in programming is often a puzzle, and something
             | useful is accomplished beyond exercising your brain.
             | 
             | For an analogy, if you walk to work every day instead of
             | driving, you are accomplishing something useful beyond just
             | the exercising.
             | 
             | My dad always tried to get a residence that was about a 15
             | min walk to work.
        
       | WalterBright wrote:
       | The author, Razvan Nitu, joined the D community a few years ago
       | and is now a key member of the core Dlang team.
        
       | sixthDot wrote:
       | The backend/frontend separation sounds right but the "sub" split
       | of the frontend sounds odd.
       | 
       | You dont care if in the AST some functions are doing too much. In
       | my opinion the most important is the ability to _drive the
       | compilation_, for example stop after lexical, stop after
       | semantics. Then if too much code got compiled into the *.a that's
       | unfortunate but not dramatic.
       | 
       | BTW there's is also the split between the driver and the
       | frontend, but I think this is done.
        
       | mhh__ wrote:
       | The issue isn't really refactoring.
       | 
       | This is kind of like a blind man clapping to echolocate his way
       | around a maze. It's better than nothing but the issue is that the
       | compiler just isn't amenable to non-batch work (e.g. the semantic
       | analysis is all or nothing, it uses WAY too much memory[0], and
       | it can't serialize it's state to the disk).
       | 
       | You could already use the frontend as a library, almost no one
       | does. It's not because the API.
       | 
       | [0] SDC can pack a lot of types into a few bytes. This is the
       | kind of thing real refactoring allows -- this change (packing
       | types, to be specific) is something I would happily chip in and
       | help out with.
       | 
       | Memory layout and locality is where performance lies in a
       | compiler (that and doing less work).
        
         | moonchild wrote:
         | 'Compilers' should be incremental (and iterative), and the
         | asymptotics are _way_ more important than the constant factors;
         | if the conceptual models are not adequate to effectively
         | express very fine-grain incrementality, no amount of bitpacking
         | is going to save you. https://arxiv.org/pdf/2104.01270.pdf as
         | an example--this is not perfect, but very serviceable.
         | 
         | Being able to serialise is good and valuable and useful, and
         | enables some very interesting things, but you can still do a
         | lot of useful things with just a persistent process working on
         | in-memory structures.
        
       | codr7 wrote:
       | I wanted to like D for a long time, feels like Go without the
       | limitations to me.
       | 
       | But it's taking A LONG TIME to become usable.
        
         | Alifatisk wrote:
         | I found the importC feature interesting, I tried the example
         | code (with running the hello world in C using the importC flag)
         | and the compiler threw lots of warnings.
         | 
         | https://dlang.org/spec/importc.html#examples
        
           | WalterBright wrote:
           | I did not get any warnings running it on Ubuntu.
           | 
           | What warnings are you seeing? What operating system?
        
             | Alifatisk wrote:
             | Thanks for checking up,
             | 
             | I did it on my Winows 10 machine, it's kinda too late to
             | start it up now and run it but I'll do it tomorrow and post
             | both the code and the error message.
             | 
             | Update: I did this quickly on my MBP.
             | 
             | Code: cat test.c 1 | #include <stdio.h> #include <stdlib.h>
             | 
             | int main() { puts("Hello, world!"); return EXIT_SUCCESS; }
             | 
             | Command: dmd test.c
             | 
             | Error: ld: address=0x0 points to section(2) with no content
             | in '/Users/james/.asdf/installs/dmd/2.107.0/dmd2/osx/lib/li
             | bphobos2.a[3233](config_a98_4c3.o)' clang: error: linker
             | command failed with exit code 1 (use -v to see invocation)
             | Error: linker exited with status 1
        
       ___________________________________________________________________
       (page generated 2024-02-22 23:01 UTC)