Skip to content

Commit

Permalink
PR Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Geetis committed Mar 13, 2024
1 parent 8b2845c commit 7cd7dd9
Show file tree
Hide file tree
Showing 25 changed files with 306 additions and 445 deletions.
7 changes: 2 additions & 5 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use monaco::{
sys::{editor::IMarkerData, MarkerSeverity},
};
use std::rc::Rc;
use swim::agent::datapath_communicator::DatapathCommunicator;
use swim::agent::datapath_reducer::DatapathReducer;
use swim::agent::EmulationCoreAgent;
use swim::emulation_core::mips::datapath::Stage;
Expand All @@ -17,10 +18,6 @@ use swim::parser::parser_structs_and_enums::ProgramInfo;
use swim::ui::footer::component::Footer;
use swim::ui::regview::component::Regview;
use swim::ui::swim_editor::component::SwimEditor;
use swim::{
agent::datapath_communicator::DatapathCommunicator,
parser::parser_structs_and_enums::Architecture,
};
use swim::{
emulation_core::{architectures::AvailableDatapaths, mips::instruction::get_string_version},
ui::{
Expand All @@ -42,7 +39,7 @@ use yew_hooks::prelude::*;
// and comment the code, language, and text_model lines. IMPORTANT:
// rename fib_model to text_model to have it work.
const CONTENT: &str = include_str!("../../static/assembly_examples/riscv_test.asm");
const ARCH: Architecture = Architecture::RISCV;
const ARCH: AvailableDatapaths = AvailableDatapaths::RISCV;

#[derive(Properties, Clone, PartialEq)]
struct AppProps {
Expand Down
2 changes: 1 addition & 1 deletion src/emulation_core/architectures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::agent::messages::MipsStateUpdate;
use crate::emulation_core::mips::datapath::MipsDatapath;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub enum AvailableDatapaths {
MIPS,
RISCV,
Expand Down
220 changes: 112 additions & 108 deletions src/parser/parser_assembler_main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::emulation_core::architectures::AvailableDatapaths;
use crate::parser::assembling::{assemble_data_binary, read_operands, read_operands_riscv};
use crate::parser::parser_structs_and_enums::ErrorType::*;
use crate::parser::parser_structs_and_enums::OperandType::*;
Expand All @@ -12,120 +13,123 @@ use std::collections::HashMap;

///Parser is the starting function of the parser / assembler process. It takes a string representation of a MIPS
/// program and builds the binary of the instructions while cataloging any errors that are found.
pub fn parser(file_string: String, arch: Architecture) -> (ProgramInfo, Vec<u32>) {
if arch == Architecture::MIPS {
let mut program_info = ProgramInfo {
monaco_line_info: tokenize_program(file_string),
..Default::default()
};

(program_info.instructions, program_info.data) =
separate_data_and_text(&mut program_info.monaco_line_info);

expand_pseudo_instructions_and_assign_instruction_numbers(
&mut program_info.instructions,
&program_info.data,
&mut program_info.monaco_line_info,
);

let vec_of_data = assemble_data_binary(&mut program_info.data);

let labels: HashMap<String, usize> =
create_label_map(&mut program_info.instructions, &mut program_info.data);

complete_lw_sw_pseudo_instructions(
&mut program_info.instructions,
&labels,
&mut program_info.monaco_line_info,
);

read_instructions(
&mut program_info.instructions,
&labels,
&mut program_info.monaco_line_info,
);

program_info.console_out_post_assembly = suggest_error_corrections(
&mut program_info.instructions,
&mut program_info.data,
&labels,
&mut program_info.monaco_line_info,
arch,
);

let (binary, data_starting_point) =
create_binary_vec(program_info.instructions.clone(), vec_of_data);

for entry in &program_info.monaco_line_info {
program_info
.updated_monaco_string
.push_str(&format!("{}\n", entry.updated_monaco_string));
pub fn parser(file_string: String, arch: AvailableDatapaths) -> (ProgramInfo, Vec<u32>) {
match arch {
AvailableDatapaths::MIPS => {
let mut program_info = ProgramInfo {
monaco_line_info: tokenize_program(file_string),
..Default::default()
};

(program_info.instructions, program_info.data) =
separate_data_and_text(&mut program_info.monaco_line_info);

expand_pseudo_instructions_and_assign_instruction_numbers(
&mut program_info.instructions,
&program_info.data,
&mut program_info.monaco_line_info,
);

let vec_of_data = assemble_data_binary(&mut program_info.data);

let labels: HashMap<String, usize> =
create_label_map(&mut program_info.instructions, &mut program_info.data);

complete_lw_sw_pseudo_instructions(
&mut program_info.instructions,
&labels,
&mut program_info.monaco_line_info,
);

read_instructions(
&mut program_info.instructions,
&labels,
&mut program_info.monaco_line_info,
);

program_info.console_out_post_assembly = suggest_error_corrections(
&mut program_info.instructions,
&mut program_info.data,
&labels,
&mut program_info.monaco_line_info,
arch,
);

let (binary, data_starting_point) =
create_binary_vec(program_info.instructions.clone(), vec_of_data);

for entry in &program_info.monaco_line_info {
program_info
.updated_monaco_string
.push_str(&format!("{}\n", entry.updated_monaco_string));
}

for instruction in program_info.instructions.clone() {
program_info
.address_to_line_number
.push(instruction.line_number);
}

program_info.pc_starting_point = determine_pc_starting_point(labels);
program_info.data_starting_point = data_starting_point;

return (program_info.clone(), binary);
}
AvailableDatapaths::RISCV => {
let mut program_info = ProgramInfo {
monaco_line_info: tokenize_program(file_string),
..Default::default()
};

for instruction in program_info.instructions.clone() {
program_info
.address_to_line_number
.push(instruction.line_number);
}
(program_info.instructions, program_info.data) =
separate_data_and_text(&mut program_info.monaco_line_info);

program_info.pc_starting_point = determine_pc_starting_point(labels);
program_info.data_starting_point = data_starting_point;

(program_info.clone(), binary)
} else {
let mut program_info = ProgramInfo {
monaco_line_info: tokenize_program(file_string),
..Default::default()
};

(program_info.instructions, program_info.data) =
separate_data_and_text(&mut program_info.monaco_line_info);

// Implement a RISC-V version
expand_pseudo_instructions_and_assign_instruction_numbers_riscv(
&mut program_info.instructions,
&program_info.data,
&mut program_info.monaco_line_info,
);

let vec_of_data = assemble_data_binary(&mut program_info.data);

let labels: HashMap<String, usize> =
create_label_map(&mut program_info.instructions, &mut program_info.data);

read_instructions_riscv(
&mut program_info.instructions,
&labels,
&mut program_info.monaco_line_info,
);

program_info.console_out_post_assembly = suggest_error_corrections(
&mut program_info.instructions,
&mut program_info.data,
&labels,
&mut program_info.monaco_line_info,
arch,
);

let (binary, data_starting_point) =
create_binary_vec(program_info.instructions.clone(), vec_of_data);

for entry in &program_info.monaco_line_info {
program_info
.updated_monaco_string
.push_str(&format!("{}\n", entry.updated_monaco_string));
}
// Implement a RISC-V version
expand_pseudo_instructions_and_assign_instruction_numbers_riscv(
&mut program_info.instructions,
&program_info.data,
&mut program_info.monaco_line_info,
);

for instruction in program_info.instructions.clone() {
program_info
.address_to_line_number
.push(instruction.line_number);
}
let vec_of_data = assemble_data_binary(&mut program_info.data);

let labels: HashMap<String, usize> =
create_label_map(&mut program_info.instructions, &mut program_info.data);

read_instructions_riscv(
&mut program_info.instructions,
&labels,
&mut program_info.monaco_line_info,
);

program_info.console_out_post_assembly = suggest_error_corrections(
&mut program_info.instructions,
&mut program_info.data,
&labels,
&mut program_info.monaco_line_info,
arch,
);

program_info.pc_starting_point = determine_pc_starting_point(labels);
program_info.data_starting_point = data_starting_point;
let (binary, data_starting_point) =
create_binary_vec(program_info.instructions.clone(), vec_of_data);

(program_info.clone(), binary)
for entry in &program_info.monaco_line_info {
program_info
.updated_monaco_string
.push_str(&format!("{}\n", entry.updated_monaco_string));
}

for instruction in program_info.instructions.clone() {
program_info
.address_to_line_number
.push(instruction.line_number);
}

program_info.pc_starting_point = determine_pc_starting_point(labels);
program_info.data_starting_point = data_starting_point;

return (program_info.clone(), binary);
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions src/parser/parser_structs_and_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ pub struct ProgramInfo {
pub data_starting_point: usize,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub enum Architecture {
#[default]
MIPS,
RISCV,
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
///This struct holds all the information we gather in the parser & assembler about a single line the user wrote
pub struct MonacoLineInfo {
Expand Down
29 changes: 16 additions & 13 deletions src/parser/parsing.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::emulation_core::architectures::AvailableDatapaths;
use crate::parser::parser_structs_and_enums::ErrorType::*;
use crate::parser::parser_structs_and_enums::TokenType::{Directive, Label, Operator, Unknown};
use crate::parser::parser_structs_and_enums::{
Expand All @@ -7,7 +8,7 @@ use crate::parser::parser_structs_and_enums::{
use levenshtein::levenshtein;
use std::collections::HashMap;

use super::parser_structs_and_enums::{Architecture, SUPPORTED_INSTRUCTIONS_RISCV};
use super::parser_structs_and_enums::SUPPORTED_INSTRUCTIONS_RISCV;

///Takes the initial string of the program given by the editor and turns it into a vector of Line,
/// a struct that holds tokens and the original line number.
Expand Down Expand Up @@ -404,7 +405,7 @@ pub fn suggest_error_corrections(
data: &mut [Data],
labels: &HashMap<String, usize>,
monaco_line_info: &mut [MonacoLineInfo],
arch: Architecture,
arch: AvailableDatapaths,
) -> String {
let levenshtein_threshold = 2_f32 / 3_f32;
let mut console_out_string: String = "".to_string();
Expand Down Expand Up @@ -471,19 +472,21 @@ pub fn suggest_error_corrections(
let given_string = &instruction.operator.token_name;
let mut closest: (usize, String) = (usize::MAX, "".to_string());

if arch == Architecture::MIPS {
for instruction in SUPPORTED_INSTRUCTIONS_MIPS {
if levenshtein(given_string, instruction) < closest.0 {
closest.0 = levenshtein(given_string, instruction);
closest.1 = instruction.to_string();
match arch {
AvailableDatapaths::MIPS => {
for instruction in SUPPORTED_INSTRUCTIONS_MIPS {
if levenshtein(given_string, instruction) < closest.0 {
closest.0 = levenshtein(given_string, instruction);
closest.1 = instruction.to_string();
}
}
}
}
if arch == Architecture::RISCV {
for instruction in SUPPORTED_INSTRUCTIONS_RISCV {
if levenshtein(given_string, instruction) < closest.0 {
closest.0 = levenshtein(given_string, instruction);
closest.1 = instruction.to_string();
AvailableDatapaths::RISCV => {
for instruction in SUPPORTED_INSTRUCTIONS_RISCV {
if levenshtein(given_string, instruction) < closest.0 {
closest.0 = levenshtein(given_string, instruction);
closest.1 = instruction.to_string();
}
}
}
}
Expand Down
Loading

0 comments on commit 7cd7dd9

Please sign in to comment.