aboutsummaryrefslogtreecommitdiff
path: root/src/generator.c
Commit message (Collapse)AuthorAgeFilesLines
* [cup] Self-hosting is now possible! Make some tweaks to match C outputMustafa Quraish2022-02-071-1/+0
| | | | | | | | | | | A bit of a chonky commit, but this ports over the remaining (well, almost) everything from the C implementation to the self-hosted compiler. The only things that really remain right now are (1) defer support and (2) support for constants in local scopes. There were used barely enough so for now their uses have been removed, but I'll implement them back later. Not sure how useful (2) is though.
* Allow empty return statements for void functionsMustafa Quraish2022-02-051-1/+4
|
* Fix label counts during code generation for `||` and `&&`Mustafa Quraish2022-02-051-10/+10
|
* Allow function declarations without a definitionMustafa Quraish2022-02-051-0/+3
|
* Handle command-line arguments properly on linuxMustafa Quraish2022-02-051-4/+13
| | | | | | | Turns out they're supposed to be accessed on the stack there. For macOS, because 16 byte-alignment is "required", we don't know the exact position of the arguments and that they're also passed in through `rdi` and `rsi`
* Add support for some more binary ops: |, &, ^Mustafa Quraish2022-02-051-1/+22
|
* Add ability to initialize global variablesMustafa Quraish2022-02-051-0/+13
| | | | The code to initialize them is put right before calling `main()`
* Allow `builtins.c` to inject constants into program, use for syscallsMustafa Quraish2022-02-051-4/+2
| | | | | | We can now directly expose the `syscallN()` APIs to the program and define the `open()`, `write()` etc syscalls in the stdlib. This simplifies the implementation a decent bunch :^)
* Add support for basic structsMustafa Quraish2022-02-031-1/+9
| | | | | | | | Structs for now (and probably for the near future) are not allowed to be passed by value, and instead you just pass a pointer to it. Nested structs can also be defined, and they can be either anonymous, or named (in which case only the members can access the type).
* Push `argc` and `argv` to the `main()` functionMustafa Quraish2022-02-031-0/+10
|
* Add helper to create builtins for syscalls + implement `read()`Mustafa Quraish2022-02-031-56/+2
| | | | | | | | | | This was possible, but very tedious to do by hand before. Now we automate it based on the number of arguments. Note that currently we can't just add `syscall3()` etc as builtins because the actual numbers for the system calls vary from one system to another, and we want to maintain support for macOS and Linux (at least for now).
* Remove `putc` intrinsic and replace with `write(fd, buf, size)`Mustafa Quraish2022-02-031-6/+14
| | | | | `putc`, `puts`, and `putnum` in `std/common.cup` are now implemented in terms of the `write()` syscall.
* Add support for `char` type + string/char literalsMustafa Quraish2022-02-021-6/+48
| | | | | | | | | | | This commit does a few things in one go: - Add support for a `char` type + some changes to support the new size - Add support for character literals. We need some escaping here to be able to use `\n` and `\0`, etc. - Add support for string literals. These are all stored in the `.data` section. Fortunately NASM already handles the escape characters. - Fix some bugs with code generation, specifically using `movsx` to sign extend the smaller types into 64-bit registers.
* Remove default initialization to 0 for variable declarationsMustafa Quraish2022-02-021-5/+0
| | | | | | This made sense when all variables were of the same size, but now with arrays we don't initialize them by default. Perhaps we should find a way to 0-initialize all the memory?
* Type checking of expressions / functions!Mustafa Quraish2022-02-021-1/+11
| | | | | | | | | | | | | | | | | This is a bit of a chonky commit, but it adds in the basics of checking the types of expressions / function calls / return types. There's still a lot of work to be done, including: (1) Adding new core types, and casting between allowed types automatically (2) Picking the corrent output type based on input types (for instance float+int == float) (3) We need much better error reporting, the error messages are really vague and unhelpful as-is (4) We also need to work to ensure that a function with a return type actually returns (5) Possible re-factoring to make stuff less hacky when we have more types / structs / arrays / etc.
* Add support for pointers! (tests missing)Mustafa Quraish2022-02-021-13/+16
| | | | | | | | | | This commit adds initial support for taking pointers / dereferencing. The type system is still a bit of a hot mess, so all type information is actually not looked at, but the functionality still seems to be there. Still need to add some tests for pointers/dereferencing to ensure that it works in some edge cases as well.
* Refactor variable access+assignment in terms of `generate_lvalue()`Mustafa Quraish2022-02-021-20/+21
| | | | This will make it trivial to compute pointers!
* Modify how types are stored.Mustafa Quraish2022-02-021-2/+2
| | | | | | | We now dynamically allocate the type structure, and recursively store a reference to the original type if it's a pointer. For now it's a little bit of a waste but it helps us future-proof stuff for more complex things to come
* Defer: Pop elements off the defer-stack when returningMustafa Quraish2022-02-011-2/+4
| | | | | | | | | | | | | We restore the count later, but this fix makes it so that the compiler doesn't get stuck in an infinite loop when you try to compile the following code: ``` fn main(): int { defer return 5; return 1; } ```
* Add basic `defer` implementation.Mustafa Quraish2022-02-011-0/+21
| | | | | | We don't have any closures yet, so it's essentially the same as just moving the statement after the `defer` keyword to the end of the block/ right before returning from the function.
* Global variables now supported! + some fixes to OP_ASSIGNMustafa Quraish2022-01-311-27/+29
| | | | | | Previously we weren't creating a new assignment node, and this was causing all sorts of funky errors. This commit also fixes that, and we can now use global variables :^)
* Minor fixes to code generationMustafa Quraish2022-01-311-11/+11
| | | | | | | (1) Prefix function names with `func_` now, to avoid clasing with possible NASM keywords (such as `abs`). (2) Label numbers are now properly handed for conditional expressions.
* Add basic builtin-function supportMustafa Quraish2022-01-311-6/+95
| | | | | | | This isn't really super extendible for now, but it's a start and gives us the `print` builtin which allows us to finally actually print out values to the screen, so we can move away from testing with exit codes eventually.
* Rename `cup` directory to `src`Mustafa Quraish2022-01-301-0/+355