6 ms·
Let's make a Teeny Tiny compiler
- deleted 6y ago[deleted]
- deleted 6y ago[deleted]
- kerkeslager 6y agoI'm sorry to be negative here, but this article, like most articles about compilers, is bike shedding. For those not aware of the bike shedding metaphor, it's the assertion that when discussing the design of a nuclear power plant, everyone will want to discuss the color of the shed where the workers store their bikes because they understand it. Meanwhile, nobody will want to discuss the nuclear reactor itself, because that's complicated and they don't really understand what's going on with it. In compilers, parsing is the bike shed, and code emitters are the nuclear reactor. I've read probably hundreds of articles on "compilers" at this point, and they're all actually just about parsers. I can't point to a single one that actually emitted working assembly.
- jermaustin1 6y agoThis tutorial is multipart, the code emitting is in the 3rd part.
- kerkeslager 6y agoI'll believe it when I see it.
- zachrip 6y agoI'll take "what is due diligence" for 300, Alex.
- kerkeslager 6y agoI guess I was technically wrong in that it does have an emitter, but I wasn't wrong in that it still doesn't emit assembly. gcc (or whatever you're using to compile your C) is still doing all the nuclear reactor part.
- azhenley 6y agoVery few "commercial" compilers emit assembly directly. They use LLVM or C or some other intermediate language.
- kerkeslager 6y agoSorry that I'm coming across as critical of your work here--that wasn't my intent but I didn't do enough to avoid it. Your article isn't the problem, it's just proximal to the problem I'm describing. I should have been more clear about that in my post. To address your post: I'm not aware of any widely used compilers that compile from a general-purpose language to C. There are a handful of DSLs that do, and there may be some mainstream general-purpose language compiler that does that I don't know of. Compiling to LLVM or GCC's RTL has a lot more in common with compiling to Assembly than it does with compiling to C. Compiling to LLVM/RTL/assembly is fundamentally different from compiling to another high-level language. When compiling to C for example, you get to compile your for loops into C for loops--it's a fairly easy one-to-one conversion. Compiling to conditional jumps is a much more complicated endeavor, requiring more architecture.
- jermaustin1 6y agoI believe what you are meaning by assembly is native code or the executable bytes that a compiler/linker/assembler output? Assembly is just another language (or more precisely: family of languages).
- kerkeslager 6y agoI wasn't confused about what assembly was. Compiling a for loop to a slightly different syntax of for loop is a much different problem from compiling a for loop to conditional jumps.
- azhenley 6y agoThe emitter was released 11 days ago. The links to part 2 and 3 are in the first line of this blog post.
- jermaustin1 6y agohttps://web.eecs.utk.edu/~azh/blog/teenytinycompiler3.html https://web.eecs.utk.edu/~azh/blog/teenytinycompiler3.html
- dang 6y agoPlease don't be a jerk on HN. I'm sure you don't mean to be, but comments like this can really come across the wrong way. That's one reason the site guidelines include: "Please don't post shallow dismissals, especially of other people's work. A good critical comment teaches us something." https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- kerkeslager 6y agoI don't personally think the fact that there are hundreds of other articles that already cover the same topic in greater depth is a shallow dismissal, and I think that could teach people something. But I'm open to the possibility that I maybe could have communicated this in a kinder way. Do you have any suggestions on how?
- sitzkrieg 6y agoi have to agree, its like people get excited about getting something going and immediately write about the parser level and thats all you see
- azhenley 6y agoPart 3 is the emitter that produces C: https://web.eecs.utk.edu/~azh/blog/teenytinycompiler3.html https://web.eecs.utk.edu/~azh/blog/teenytinycompiler3.html
- deleted 6y ago[deleted]
- kerkeslager 6y agoAh, okay. It looks like you're taking a fairly C-like language and transforming it to C--I'd call this a transpiler rather than a compiler. You're not wrong to call it a compiler, but it's a pretty noncentral example of a compiler. Congrats on at least having an emitter, but I'm still searching for an article that shows how to emit assembly of any kind.
- dlo 6y agoI used to work on a static analyzer that did taint analysis, model checking, buffer bounds checking, and so on -- a bit like a compiler backend on steroids. If there's a specific topic you'd like an explanation on, I could be convinced to write something up. My favorite was always context-sensitive, interprocedural points-to analysis. And dataflow analysis in the presence of higher-order controlflow constructs.
- kerkeslager 6y agoHonestly, I'd just like to see anything that outputs assembly I can run through an assembler to create an executable. I've fumblingly written a bit of this code myself, but always felt hampered by a lack of knowledge.
- jermaustin1 6y agoInstead of outputting c, could you not just output the equivalent assembler? so instead of self.emitter.emitLine("printf(\"" + self.curToken.text + "\\n\");") you do something like self.emitter.emitLine("STRING DB " + self.curToken.text + "', '$'") ... self.emitter.emitLine("LEA DX,STRING") self.emitter.emitLine("MOV AH,09H") self.emitter.emitLine("INT 21H")
- catwind7 6y agoso what if it is? i know what the bike shedding metaphor is and frankly you're stretching it a bit because all this person is trying to do is educate and they're not even saying this is the only way to do it. seems odd to pull out the bike shed metaphor for every case there's an abundance of technical articles on a subject matter. There's lots of tutorials on for loops in X language. Do you consider that bike shedding?
- kerkeslager 6y agoI'm not gonna argue with you over whether the metaphor I used was applicable or not--the point of the metaphor was to communicate and it's clear I succeeded in communicating. It's clear you understood what I meant, because you provided another example. Yes, whether or not you agree the bike shedding metaphor applies, you have to agree that having the 100th tutorial on how to do something that's in the docs that ships with the language is equally pointless.
- catwind7 6y agoi think you succeeded in communicating your thoughts and feelings in spite of the metaphor. there are plenty of documentation in the form of tutorials on for nearly every aspect of every programming language I can think of. I think it's important to keep in mind that we all learn differently and sometimes one explanation can make no sense while another makes a lot of sense. That said, I think I get your frustration in that sometimes the process of _finding_ the explanation that clicks for whatever your question is (how to emit assembler?) can be really painful because all the explanations are shallow. However, I don't think it's fair to take that frustration out on the writers (I got the impression you were, but maybe you weren't and just felt like venting). I for one encourage engineers to write if no for no other reason than to better cement their own understanding. I do wonder, though, if maybe there's some improvements we can make to how we filter / search for long-form technical articles outside of google so that the content is more relevant
- kerkeslager 6y ago> That said, I think I get your frustration in that sometimes the process of _finding_ the explanation that clicks for whatever your question is (how to emit assembler?) can be really painful because all the explanations are shallow. While I agree with your overall point about different learning styles, that's not really the problem I'm describing. Introductory material on emitting assembly or similar, doesn't exist for any learning style as far as I know. The best that I know of are some dead-tree books, and their emitters target dead or obscure architectures. None of the code examples are ones I could run on my machine. > However, I don't think it's fair to take that frustration out on the writers (I got the impression you were, but maybe you weren't and just felt like venting). That's a fair criticism: I apologized to the writer in a different post.
- dang 6y agohttps://news.ycombinator.com/item?id=23441767 https://news.ycombinator.com/item?id=23441767
- azhenley 6y agoThat was before it was completed :(
- dang 6y agoThat's one problem with these multipart blog posts. It becomes ambiguous whether the submission is "part 1" or the head of the list ("part 1" "part 2" "part 3"). If you want to make a single long version of the page that includes all three parts, I'd be happy to arrange a repost. It would be best to email hn@ycombinator.com about it.