Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Cargo.lock

# temporary files
*.bin
*.arm.s
*.as
a.out

.vscode/
test_binary_translate*
35 changes: 26 additions & 9 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
#!/usr/bin/env bash

ASM_FILE=$1
ARCH=$1
ASM_FILE=$2
PLATFORM=""
LD_FLAGS=""
BENCHMARKING="false" # "true" to enable
BENCHMARKING="true" # "true" to enable
QEMU=""

if [[ -z "$ASM_FILE" ]]; then
echo "Error: Assembly (.S) file is not passed in."
echo "Usage: ./run.sh test_binary_translate_add.S"
if [[ -z "$ARCH" || -z "$ASM_FILE" ]]; then
echo "Error: Architecture and assembly (.S) file must be provided."
echo "Usage: ./run.sh [riscv|arm] <assembly_file.S>"
exit 1
fi

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
PLATFORM="aarch64-linux-gnu"
if [[ "$ARCH" != "riscv" && "$ARCH" != "arm" ]]; then
echo "Error: Architecture must be either 'riscv' or 'arm'."
echo "Usage: ./run.sh [riscv|arm] <assembly_file.S>"
exit 1
fi

if [[ "$ARCH" == "riscv" ]]; then
QEMU="qemu-riscv64"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
PLATFORM="riscv64-unknown-linux-gnu-"
fi
elif [[ "$ARCH" == "arm" ]]; then
QEMU="qemu-aarch64"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
PLATFORM="aarch64-unknown-linux-gnu-"
fi
fi

if [[ "$OSTYPE" == "darwin"* ]]; then
Expand All @@ -23,9 +39,10 @@ fi
"$PLATFORM"as "$ASM_FILE" -o "$ASM_FILE".as || { echo "Assembly compilation failed"; exit 1; }
"$PLATFORM"ld "$ASM_FILE".as -o "$ASM_FILE".bin $LD_FLAGS || { echo "Linking failed"; exit 1; }

./"$ASM_FILE".bin
"$QEMU" ./"$ASM_FILE".bin
echo "$?"

if [ "$BENCHMARKING" = true ]; then
hyperfine -r 1000 -w 100 -Ni ./"$ASM_FILE".bin
hyperfine -r 1000 -w 100 -Ni ""$QEMU" ./"$ASM_FILE".bin"
fi

63 changes: 62 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,68 @@ use std::fs;

use crate::{instruction::RiscVInstruction, translate::translate_instrs};

pub const START: &str = r#"
/// Loop main() 10,000 times. Uses a3.
pub const RISCV_LOOP_START: &str = r#"
.text

.global _start
.global _main

.balign 4 # not sure if these are needed for RISC-V
_start:
# while i < 10,000
li a3, 10000
.loop:
addi a3, a3, -1
ble a3, x0, .end

# main()
jal ra, main

# while loop
j .loop
.end:
# exit(0)
li a7,93
ecall

.balign 4
_main:
main:
"#;

/// Loop main() 10,000 times. Uses x3
pub const ARM_LOOP_START: &str = r#"
.text

.global _start
.global _main

.balign 4
_start:
# i = 10,000
mov x3, #10000
# while i > 0
.loop:
sub x3, x3, 1

cmp x3, xzr
ble .end

# main()
bl main

b .loop
.end:
mov x8, #93
svc #0

.balign 4
_main:
main:
"#;

pub const ARM_START: &str = r#"
.text

.global _start
Expand Down
34 changes: 0 additions & 34 deletions tests/add/add.arm.s

This file was deleted.

22 changes: 22 additions & 0 deletions tests/add/add.riscv.s
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
.text

.global _start
.global _main

.balign 4 # not sure if these are needed for RISC-V
_start:
# while i < 10,000
li a3, 10000
.loop:
addi a3, a3, -1
ble a3, x0, .end

# main()
jal ra, main

# while loop
j .loop
.end:
# exit(0)
li a7,93
ecall
main:
addi sp,sp,-32
sd ra,24(sp)
Expand Down
6 changes: 4 additions & 2 deletions tests/add/test_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ mod tests {
use binary_room::translate::*;
use binary_room::utils;
use binary_room::utils::translate_to_file;
use binary_room::utils::START;
use binary_room::utils::ARM_LOOP_START;

#[test]
fn test_binary_translate() {
let riscv_asm: Vec<RiscVInstruction> = vec![
RiscVInstruction::Verbatim { text: START.to_string() },
RiscVInstruction::Verbatim {
text: ARM_LOOP_START.to_string(),
},
RiscVInstruction::Addi {
dest: RiscVRegister::SP,
src: RiscVRegister::SP,
Expand Down
1 change: 1 addition & 0 deletions tests/binaries/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# Use the same mkShell as documented above
packages = with pkgs; [
gcc
# qemu
# pkgs.clang-tools
];
};
Expand Down
2 changes: 1 addition & 1 deletion tests/binaries/hello_world.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import <stdio.h>
#include <stdio.h>

int main(void) {
printf("hello world\n");
Expand Down
9 changes: 9 additions & 0 deletions tests/binaries/riscv/cross.arm.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with import <nixpkgs> {
crossSystem = {
config = "aarch64-unknown-linux-gnu";
};
};

mkShell {
buildInputs = [ ]; # your dependencies here
}
9 changes: 9 additions & 0 deletions tests/binaries/riscv/cross.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with import <nixpkgs> {
crossSystem = {
config = "riscv64-unknown-linux-gnu";
};
};

mkShell {
buildInputs = [ ]; # your dependencies here
}
27 changes: 0 additions & 27 deletions tests/binaries/riscv/flake.lock

This file was deleted.

26 changes: 0 additions & 26 deletions tests/binaries/riscv/flake.nix

This file was deleted.

29 changes: 0 additions & 29 deletions tests/echo/echo.arm.s

This file was deleted.

4 changes: 2 additions & 2 deletions tests/echo/test_echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod tests {
use binary_room::translate::*;
use binary_room::utils;
use binary_room::utils::translate_to_file;
use binary_room::utils::START;
use binary_room::utils::ARM_START;

const buf: &str = r#"
.buf:
Expand All @@ -16,7 +16,7 @@ mod tests {
let riscv_asm: Vec<RiscVInstruction> = vec![
// RiscVInstruction::Verbatim { text: buf.to_string() },
RiscVInstruction::Verbatim {
text: START.to_string(),
text: ARM_START.to_string(),
},
// read syscall
RiscVInstruction::Addi {
Expand Down
28 changes: 0 additions & 28 deletions tests/print/print.arm.s

This file was deleted.

Loading