aboutsummaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
...
* Improve the message on bad file includesMiquel Sabaté Solà2025-01-103-17/+9
| | | | | | Also remove some pending TODOs. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* tests: Bundle end-to-end tests into a tarballMiquel Sabaté Solà2025-01-1012-1367/+19
| | | | | | | | This way I can simply compress all the examples to traverse with a single file, and thus no longer polluting this repository from code from other repositories. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Properly fill the context stack when forcing itMiquel Sabaté Solà2025-01-102-16/+67
| | | | | | | | | | | | | When calling `force_context_switch` the stack was mindlessly pushing the given name without taking into consideration how scopes are to be laid out. This made some variables/addresses that were previously preserved no longer reachable when crunching pending nodes. This patch also makes `force_context_switch` reset the stack before doing anything at all, which means that `force_context_pop` was no longer relevant. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Improve error message on unknown variable/scopeMiquel Sabaté Solà2025-01-092-11/+9
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent assignments on relative referencesMiquel Sabaté Solà2025-01-091-1/+17
| | | | | | | | | | | | It is a really weird thing to do, but on a twisted way I can see how someone could think of such a monstrosity. Panicking on such a case is not valid because that's not the fault from the assembler but from the programmer, and so a proper message should be displayed instead. This was detected via the fuzzer, but it was a side effect from e7c5e63d04f6 ("Evaluate bare numbers as decimal values"). Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Reset the literal mode on each macro argumentMiquel Sabaté Solà2025-01-091-0/+20
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Evaluate bare numbers as decimal valuesMiquel Sabaté Solà2025-01-091-6/+11
| | | | | | | | | | In places like macro calls, the programmer might actually prefer to pass a numeric argument as is, without any prefixes. In these cases, just evaluate it as a decimal. In fact, this was already covered when evaluating the context because the same thing happens to assignments. Hence, I just needed to expand the scope of this. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add missing binary for end-to-end testsMiquel Sabaté Solà2025-01-091-0/+0
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Rename SourceInfo::working_directory to directoryMiquel Sabaté Solà2025-01-094-10/+13
| | | | | | Also documented the struct as it is exported. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Merge all error types into a single oneMiquel Sabaté Solà2025-01-094-318/+234
| | | | | | | | They started with good intentions, but in the end they were all pretty much alike. Hence, it makes sense to simplify everything and provide a single struct. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove ContextErrorReason as it was unusedMiquel Sabaté Solà2025-01-092-23/+1
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement the .include statementMiquel Sabaté Solà2025-01-0916-292/+1141
| | | | | | | | | | | | | | This needed some heavy lifting when it comes to how files were located. This means that statements like .include/.incbin now take into consideration a new list made out of SourceInfo, which holds enough information to translate from which file a node comes from. This has also been added into errors, so they are more informative on what went wrong. In order to tests this, besides all the regular unit tests, a new e2e test has been added. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Restrict literal characters furtherMiquel Sabaté Solà2025-01-091-2/+25
| | | | | | | | | | In bad314e1cfbf ("Prevent numeric literals from having spaces") it was added already the restriction on not having whitespace characters in literal expressions. Here we go a step further and we more explicitely limit which symbol combinations can go into a literal declaration (e.g. "#$2" is valid but "##2" is not). Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent numeric literals from having spacesMiquel Sabaté Solà2025-01-091-4/+25
| | | | | | | | It has been found that having literals like "# 20" can potentially be troublesome and even introduce crashes. Hence, as it's done in other assemblers, disallow this kind of syntax. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Reset the literal mode on assignmentsMiquel Sabaté Solà2025-01-081-0/+15
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Try absolute addressing on bad zeropage addressingMiquel Sabaté Solà2025-01-081-0/+31
| | | | | | | | | | | | | | | | | | | | | | | | Sometimes, out of clarity, the programmer may have written something along the lines of: lda $40, y This is invalid because the `lda` instruction does not allow zeropage y-indexing addressing mode. That being said, it does allow for absolute y-indexing addressing mode. This commit allows this syntax by transforming code like the previous one into: lda $0040, y This cannot always be done, but the assembler should at least try if it's possible and not trouble the programmer. That being said, this is otherwise a bit shady since the programmer might think that it's a 2-byte instruction when it's a 3-byte one. Hence, maybe a future linter can pick up code like this and suggest a more explicit writing. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add the .repeat control statementMiquel Sabaté Solà2025-01-078-56/+568
| | | | | | | | | | | | | | | | | | | | | | | | This is a control statement which acts similarly as .proc/.macro/.scope, in which an inner block is allocated for it. Hence, all the previous work from 1f8a6becc7cd ("parser: Implement block bodies") and ec8b709fa24c ("Implement block bodies inside of the assembler") make this one out possible, as .repeat statements don't have an identifier that can be used for hashing. From the parser perspective this introduction raises two new things. First of all this control statement also needed a differentiation between the amount of required arguments, and the allowed ones, since there is a second optional argument to it. And second, even the identifier is not given, we have to generate one so to add a context for it. This was at first not needed, but introducing .repeat-only variables means that we have to have inner contexts which need to be named somehow so we can retrieve the context later when picking up the value for them again. This last thing brought the need for a new dependency: rand. This is used to generate a random string to identify the .repeat block. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Store a reference for macros instead of an indexMiquel Sabaté Solà2025-01-074-907/+718
| | | | | | | | | | | | | | | | | It is not safe to store a node index for macros since the list of nodes that is passed down during assembly might change depending on whether an inner block is being evaluated. Hence, the previous implementation would break on a simple macro call inside of a .proc. This also raised some concerns on the design around the API, since the lifetime of references for internal assembler data needed an explicit lifetime now, and as a side-effect functions like `assemble` had to be moved out of the inner impl Assembler. This is in retrospect also a better design choice. Fixes: ec8b709fa24c ("Implement block bodies inside of the assembler"). Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* parser: Move out the parsing of control statementsMiquel Sabaté Solà2025-01-051-50/+63
| | | | | | | | | The parsing of control statements have become more complex since the introduction of block bodies in 1f8a6becc7cd ("parser: Implement block bodies"); so it makes sense to move it into its own thing and keep `parse_statement` more clear. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement block bodies inside of the assemblerMiquel Sabaté Solà2025-01-054-194/+214
| | | | | | | | | | | Following 1f8a6becc7cd ("parser: Implement block bodies"), the support for the new way of managing block bodies have also been added into the assembler. There are still some things to iron out, but they will be fixed in later commits. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* parser: Implement block bodiesMiquel Sabaté Solà2025-01-043-109/+321
| | | | | | | | | | These are bodies which are the right node of some proc controls. This way the parser comes back to sanity for statements like .macro and the likes, and behaves more like a usual parser. This was not done in the past because I thought things could have been simpler this way, but it ended up making the assembler way more complicated that it needed to. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Rename to tools.nesMiquel Sabaté Solà2024-12-254-14/+15
| | | | | | | | | | Let's continue with the good ol' tradition of being terrible at naming things. I have also renamed another project which was referenced here, so update all references as well. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add a test on jumps and labels inside of procsMiquel Sabaté Solà2024-12-241-2/+61
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Fix usage of labels inside of macrosMiquel Sabaté Solà2024-12-242-35/+84
| | | | | | | | Instead of calling `evaluate_node` for each node on a macro, just call `Assembler::bundle` for the list of nodes so the context is preserved and labels and other statements can be catched as usual. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Forbid creating named labels inside of macrosMiquel Sabaté Solà2024-12-241-0/+37
| | | | | | | This is just prone to errors and it is confusing all around. Just prohibit developers doing that. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* nasm: Also show warnings when there are errorsMiquel Sabaté Solà2024-12-241-0/+8
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent missplaced start for procs and scopesMiquel Sabaté Solà2024-12-232-4/+86
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent early .end statementsMiquel Sabaté Solà2024-12-232-3/+99
| | | | | | | | | | Prevent a missmatch on .end{macro,proc,scope}. This was more or less already covered when there was a bad context_pop call, but it was prone to errors. Check this in eval_context as it should've always been done. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Make more explicit segment/macros are only globalMiquel Sabaté Solà2024-12-232-22/+69
| | | | | | | | Force .segment and .macro statements to be on the global scope since this is how they are meant. Hence, if the programmer tries to do this, just error out. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* nasm: Allow file paths for the configurationMiquel Sabaté Solà2024-12-223-16/+36
| | | | | | | This opens up the door for developers to pass their own configuration files. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Transform mapping configurations into toml filesMiquel Sabaté Solà2024-12-2211-390/+599
| | | | | | | | This will allow the creation of configuration files that can live outside of this tree, so developers can fine tune configuration files of their own without having to pick up whatever is currently available. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for UxROM chipsMiquel Sabaté Solà2024-12-205-4/+446
| | | | | | I have also added an end-to-end test for it. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Introduce end-to-end testsMiquel Sabaté Solà2024-12-206-0/+644
| | | | | | | | | | | For now I have added the most simple example for an NES/Famicom game, which I already had on a private repository (that I plan to open source soonish). Tests simply run `nasm` on known source files and compares the results with an .nes ROM file that has the exact bytes that we expect. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent a division by zeroMiquel Sabaté Solà2024-12-201-0/+51
| | | | | | Fixes: 8b5feeed96b3 ("assembler: Implement and add tests for operators") Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Dual license the source code under this repositoryMiquel Sabaté Solà2024-12-203-1/+183
| | | | | | | | | | | | | | | | | | | This means that the source code under `crates/` is under the GNU GPLv3 (or any later version), and that source code under `lib/` is under the GNU LGPLv3 (or any later version). In practice this relaxes a bit the previous licensing because before it was all GNU GPLv3 (or any later version). For people compiling a binary with these libraries then this would've been spilling over the GPLv3 on their binaries as well, which might not be desired. Instead, for the libraries abide by the LGPLv3 (or any later version), which has the same guarantees when it comes to free/lliure software for this specific case, but at least we don't force the GPLv3 (or any later version) onto users. That being said, users of these libraries still need to provide object files so third parties could theoretically recompile those binaries under a modified library. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Only shrink the addressing on resolved bundlesMiquel Sabaté Solà2024-12-201-3/+35
| | | | | | | | | | | | | Commit ea5f0f81b8a7 ("Shrink some absolute instructions by one byte") applied the optimization in all cases, but we cannot perform it on bundles which are yet to be resolved. This is because in unresolved bundles the value is only an offset, which usually will fit on a single byte and hence the optimization would've been carried out. That being said, whenever we resolve this it might just be the case the it wouldn't have fit in that single byte, and hence we end up with an artificially shrinked instruction for a 16-bit address. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Shrink some absolute instructions by one byteMiquel Sabaté Solà2024-12-201-5/+27
| | | | | | | | | | | Some instructions which make use of absolute addressing can actually be further compressed to act like zeropage indexing. This can be done if the immediate being used by that instruction can actually fit into a single byte. If that's the case, we will then "correct" the programmer by using zeropage indexing instead of an absolute one, thus reducing one byte for that instruction. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Improve the format of error messagesMiquel Sabaté Solà2024-12-203-74/+83
| | | | | | | | | | | It was weird to show warnings which also showed "Error: " as a message, and likewise it was weird for errors to display their kind, since users simply do not care about this kind of information. Hence, streamline the format to something closer to what it's done by modern assemblers/compilers. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* nasm: Send errors to stderr insteadMiquel Sabaté Solà2024-12-201-3/+3
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Do not spit out bytes if -Werror was passedMiquel Sabaté Solà2024-12-201-9/+13
| | | | | | | | | Even if there are no errors, if warnings are to be treated as errors then the output should behave the same as if they were errors. That is, in case there are warnings and they are to be treated as errors, then do not spit out any assembled bundle. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for warningsMiquel Sabaté Solà2024-12-202-16/+66
| | | | | | | | | | | Warnings are mere xixanta::error::Error's which are not pushed into the Err of Result. That is, instead they are accumulated into an internal `warnings` vector inside of Assembler. On the binary side we now show warnings as well, and there is an option to turn warnings into errors. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Expect macros to overwrite parameter valuesMiquel Sabaté Solà2024-12-201-32/+8
| | | | | | | This is normal when calling the same macro multiple times and in which parameter values need to be updated on each case. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Find variable values on parent scopesMiquel Sabaté Solà2024-12-202-12/+99
| | | | | | | If a given variable cannot be found on the current scope, attempt to go up the context hierarchy to find it. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* scripts: Error out on warningsMiquel Sabaté Solà2024-12-191-0/+2
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* assembler: Implement and add tests for operatorsMiquel Sabaté Solà2024-12-192-6/+228
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* parser: Add support for operatorsMiquel Sabaté Solà2024-12-192-12/+217
| | | | | | | | This includes support for both binary and unary operators. Not all of them as listed by ca65 have been moved in. Let's do that whenever it make sense on each case. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Export documentation on PString::to_isizeMiquel Sabaté Solà2024-12-191-3/+3
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* parser: Allow for parenthesized expressionsMiquel Sabaté Solà2024-12-191-0/+43
| | | | | | | | | | Some expressions might be enclosed with parenthesis in order to avoid ambiguations when evaluating them. Account for this on the parser when parsing expressions. Note that this is strictly only on the `parse_expression` function; statements or other top level constructs cannot be enclosed inside of parenthesis. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Implement the .incbin control statementMiquel Sabaté Solà2024-12-195-10/+162
| | | | | | | | This also forced us to add the current working directory to the `Assembler::assemble` public function, as otherwise this control statement and others wouldn't know how to resolve relative paths. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Merge code handling for procs and labelsMiquel Sabaté Solà2024-12-181-55/+75
| | | | | | | | | | | | | The handling of labels and proc's both when evaluating the context and when bundling is almost identical. The only change is that proc's need to change the context afterwards, but otherwise they need to create a label just as regular ones. Merge things as much as possible on both these cases, while also taking the chance to do some much needed clean up around these areas, and adding a bit of helpful comments in between. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>