4 ms·
Part 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
by azhenley 6y ago
Part 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 agothere's certainly equivalent assembler, but depending on the architecture you're targeting this can be a pretty monumental task. Even a single function call can be north of a hundred lines or something (i'm making this up :D ) I guess that's why we have things like LLVM that allow you to generate intermediate representations that get converted to a bunch of different instruction sets
- jermaustin1 6y agoFor sure, i'm just telling the GP that their request for something that outputs assembly could be done with minor modifications depending on the assembly language they want to output.
- catwind7 6y agogotcha. I also get the feeling that GP was more interested in being right vs actually knowing how to get the emitter to emit assembly. It felt like the followup was just gonna be ... well WHAT ABOUT BINARY?
- kerkeslager 6y agoNah, assembly -> binary isn't that hard. ;)
- kerkeslager 6y agoWell, what you just posted already highlights one difficulty: when you come to the PRINT statement, you have to emit the string and the instructions to two different places, so we're already talking about having two different emitters, or some other way of handling this. And we need to generate different non-conflicting names/addresses depending on architecture. You said in your other post that this can be done with minor modifications, but I can already foresee a few modifications that would need to be made which aren't minor. And then there's the problem that you may want to target more than one architecture. We can write two completely different code generators, but it would be nice if there were an architecture that could share some of the code.
- catwind7 6y agonand2tetris is a great course that shows you how to design a computer from the chip level, build an assembler, and write a high level language + compiler that eventually produces the assembly code for the machine architecture _you_ built. I highly recommend it.
- kerkeslager 6y agonand2tetris is awesome, and taught me a large portion of the little I know on this subject. The real barriers to me at this point are targeting a pragmatic real-world architecture.