Crates.io | sponge |
lib.rs | sponge |
version | 0.2.1 |
source | src |
created_at | 2024-06-20 06:16:34.41162 |
updated_at | 2024-06-20 22:12:04.591318 |
description | A powerful Rust module to convert Rust to ASM |
homepage | https://github.com/Ulladay-Hub/sponge-build |
repository | https://github.com/Ulladay-Hub/sponge-build |
max_upload_size | |
id | 1277588 |
size | 48,026 |
Sponge is a powerful Rust module designed to convert Rust code into assembly language (ASM). It tokenizes Rust code, parses the tokens, and generates corresponding assembly code, making it easier to understand and optimize your Rust programs at the assembly level.
Sponge aims to bridge the gap between high-level Rust code and low-level assembly code. By converting Rust code into ASM, developers can gain insights into how their Rust code translates into machine instructions, which is valuable for performance optimization and learning purposes.
Add the following to your Cargo.toml
:
[dependencies]
sponge = "0.2.0"
Run cargo build
to install the dependencies.
Sponge can be used in multiple ways to suit your needs. You can use it to tokenize Rust code, parse the tokens, or generate ASM code.
To tokenize a Rust file:
extern crate sponge;
use sponge::decompose_tokens::decompose;
fn main() {
let filename = "script.rs";
match decompose(filename) {
Ok(tokens) => println!("{:?}", tokens),
Err(e) => eprintln!("Error: {}", e),
}
}
To parse the tokens into a structured format:
extern crate sponge;
use sponge::decompose_tokens::decompose;
use sponge::parse_tokens::parse;
fn main() {
let filename = "script.rs";
match decompose(filename) {
Ok(tokens) => {
let parsed_tokens = parse(tokens);
println!("{:?}", parsed_tokens);
},
Err(e) => eprintln!("Error: {}", e),
}
}
To generate ASM code from parsed tokens:
extern crate sponge;
use sponge::decompose_tokens::decompose;
use sponge::parse_tokens::parse;
use sponge::generate_asm::generate;
fn main() {
let filename = "script.rs";
match decompose(filename) {
Ok(tokens) => {
let parsed_tokens = parse(tokens);
let asm_code = generate(&parsed_tokens);
println!("{}", asm_code);
},
Err(e) => eprintln!("Error: {}", e),
}
}
Sponge also provides a simple CLI to facilitate the conversion process. The available options are:
-i, --input <FILE>
: Specifies the input Rust file.-o, --output <FILE>
: Specifies the output ASM file.Create a Rust file (script.rs
) with the following content:
fn main() {
let REG_1: i32 = 50;
}
Run the following command in your terminal to generate the ASM code:
sponge -i script.rs -o script.asm
This will produce the script.asm
file:
section .data
section .bss
section .text
global _start
_start:
mov r1, 50 ; Move the value 50 into register 1
; Exit the program
mov rax, 60 ; System call for exit
xor rdi, rdi ; Exit code 0
syscall ; Invoke the system call
We welcome contributions to Sponge! Here’s how you can help:
git checkout -b feature-branch
).git commit -am 'Add new feature'
).git push origin feature-branch
).Please ensure your code follows the project’s coding standards and includes appropriate tests.
This project is licensed under the MIT License - see the LICENSE file for details.
For any inquiries or issues, please contact Bradinator at imnotamilkglass@gmail.com.
Special thanks to all contributors and the Rust community for their support and contributions.