aboutsummaryrefslogtreecommitdiff
Commit message (Collapse)AuthorAgeFilesLines
...
* 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>
* Reset literal mode on pending nodesMiquel Sabaté Solà2024-12-181-3/+5
| | | | | | | | | Failing to do so would spill over the previous literal mode into the evaluation of nodes pending to be crunched. Fixes: a9f50efdef40 ("Fix label references on control statements") Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for jumping into .proc'sMiquel Sabaté Solà2024-12-182-7/+67
| | | | | | | | Up until now defining a proc only involved pushing/popping the context. Here we also allow it to create a label so it can be referenced by instructions like jsr. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Fix label references on control statementsMiquel Sabaté Solà2024-12-181-1/+47
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* scripts: Add a script to perform all tests at onceMiquel Sabaté Solà2024-12-181-0/+16
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Rename xixanta's fuzz targetsMiquel Sabaté Solà2024-12-184-6/+6
| | | | | | | | They were named with increasing numbers due to the fact that that's the default behaviour from cargo fuzz's command. So, just rename them to proper human-readable names. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent a crash on bad binary literalsMiquel Sabaté Solà2024-12-181-27/+33
| | | | | | | | | | | | | | When evaluating binary literals we allowed the shift value to grow as needed and we checked whether it was a good value after evaluating the literal. This is bad for performance reasons: if we are expecting an exact size for a literal (8 digits here), do not even attempt to do anything at all if the size doesn't match. Moreover, in some extreme cases this could result into an overflow of the 'shift' variable, which was promptly catched by Rust's bound checker. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove unneeded public referencesMiquel Sabaté Solà2024-12-182-4/+4
| | | | | | | | Some of the functions are actually private, so don't export them. Moreover, there was an import cycle which was not needed and was forcing a public import. Remove that as well. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Split crunch_and_resolve_pendingMiquel Sabaté Solà2024-12-181-5/+21
| | | | | | | | | | | This function originally came from the naive idea I had with how mappings and segments ought to work. For this reason, the function grew more ever more complex. Split this function into more clear responsabilities for each new function. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove unneeded Init and Parsing stage valuesMiquel Sabaté Solà2024-12-181-7/+19
| | | | | | | These two enum values were actually never used and they are not relevant for the assembling process. Let's remove it for simplicity's sake. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove unneeded Ok callMiquel Sabaté Solà2024-12-181-1/+1
| | | | | | Fixes: 03dae41b2ec2 ("Prevent addresses which are out of bounds") Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Prevent addresses which are out of boundsMiquel Sabaté Solà2024-12-182-13/+41
| | | | | | | | | | In some bad scenarios addresses might be pointing out of bounds (e.g. a reference further than 0xFFFF). This has to be avoided and through fuzzy testing we even got Rust panics for out of bounds u16 arithmetic. Hence, just go through usize for the actual computation and check with u16::MAX. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* nasm: Remove the option to disassemble a fileMiquel Sabaté Solà2024-12-181-37/+13
| | | | | | | | | | This never quite worked and since multiple re-writes of the assembler/parser it was even commented out. Moreover, this functionality appears to be more suitable to the new `readrom` binary introduced in commit 407048d0037c ("Add the readrom binary"). Hence, just remove this altogether. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Improve documentation on nasmMiquel Sabaté Solà2024-12-181-5/+22
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* github: Add issue/PR templatesMiquel Sabaté Solà2024-12-182-0/+29
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Remove From<std::io::Error> from errorsMiquel Sabaté Solà2024-12-181-30/+0
| | | | | | | | Having a From<std::io::Error> came from a hacky beginning in which std::io::Error was being abused. Now the error handling is saner, so there's no more need for that. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* header: Add fuzzy testingMiquel Sabaté Solà2024-12-185-0/+119
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add the readrom binaryMiquel Sabaté Solà2024-12-188-2/+431
| | | | | | | | | | | | | | | The readrom binary is similar to `readelf` from Linux and it will allow to display information from an NES/Famicom ROM file. For now the information being shown is just the header, but in the future we might also include disassembling parts of the code, or retrieving the "CHARS" section for a given ROM file, and similar. In order to implement the header parsing part a new library has been introduced, simply named "header" which abstracts everything away so you just need to call `Header::try_from("my bytes")` to fetch the actual information. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Update the README for open sourcing the projectMiquel Sabaté Solà2024-12-164-3/+25
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Fix the mapping of addresses on labelsMiquel Sabaté Solà2024-12-165-328/+867
| | | | | | | | | | | | | | | | | There was a big missunderstanding on how things were to be laid out in the end file, and so it was needed to create a proper understanding on what's a Mapping and what's a Segment. These turned out to be fundamental concepts that I failed to grok up until this commit. Hence, this commit re-arranges completely how variables and labels are stored in the Context, and how these objects can then be translated into bundles that can be spit out to the caller. This commit, besides introducing the new Mapping struct, also introduced a more general Object, which abstracts things from the Bundle struct, and allows us to pass certain metadata about the bundle at hand. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* ci: Run a fuzzy test also for the assemblerMiquel Sabaté Solà2024-12-133-2/+40
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* parser: Fix crash on non-ASCII char literalsMiquel Sabaté Solà2024-12-131-3/+3
| | | | | | | | | | | | | If the given char literal was not an ASCII one, there was the chance for the character iterator to mess things up. Hence, when checking the closing single quote, it might encounter a None value. This is simply mitigated my moving the check of ASCII alphanumeric before checking for the closing quote. Fixes: b1623996c76f ("Add support for character literals") Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Be more informative on segment overflow errorsMiquel Sabaté Solà2024-12-121-2/+2
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* nasm: Add a parameter for linker configurationMiquel Sabaté Solà2024-12-122-2/+64
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for character literalsMiquel Sabaté Solà2024-12-121-2/+76
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Re-work the handling of segments in the assemblerMiquel Sabaté Solà2024-12-125-18/+286
| | | | | | | | There were a lot of assumptions on the assembler that stemmed from a fundamental missunderstanding from my side on how segments are laid out on the final file. This commit is the first step to address this. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Add support for anonymous labelsMiquel Sabaté Solà2024-12-124-30/+329
| | | | | | | Allow for anonymous labels to be defined and referenced by using the same syntax as cc65. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Don't rely on the identifier for control identityMiquel Sabaté Solà2024-12-128-103/+182
| | | | | | | | | Instead, use a new enum type to identify the control statements that we actually support. This way the assembler is more easily aware on the control that it's dealing with, and can be more sure on certain aspects of the implementation on control support. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Fix bug on multiple arguments for macrosMiquel Sabaté Solà2024-12-121-5/+69
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Initial support for branchingMiquel Sabaté Solà2024-12-123-7/+114
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Re-work the support on labels, variables and jumpsMiquel Sabaté Solà2024-12-126-90/+283
| | | | | | | | | | | As a way to firstly adapt on the latest changes from the parser since 184c39579227 ("Re-work the parser from scratch"), the assembler had to leave out some features on 16114b2ca358 ("Adapt the assembler to the changes on the parser"). This commit reintroduces support for settings labels, variables and referencing them, while also providing a more robust implementation at that. Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Allow for relative jumps at the parser levelMiquel Sabaté Solà2024-12-121-20/+153
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
* Allow for .byte/.address statementsMiquel Sabaté Solà2024-12-122-30/+143
| | | | Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>