Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added move constructor for MIPSImage #13

Merged
merged 10 commits into from
Apr 27, 2023
31 changes: 29 additions & 2 deletions spim/CPU/image.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,46 @@
#include "image.h"
#include "inst.h"
#include "spim.h"
#include "spim-utils.h"
#include "image.h"
#include "sym-tbl.h"

#include <iostream>

MIPSImage::MIPSImage(int ctx) : ctx(ctx), std_out(ctx, std::cout), std_err(ctx, std::cerr) {}
MIPSImage::MIPSImage(int ctx) :
ctx(ctx),
std_out(ctx, std::cout),
std_err(ctx, std::cerr)
{
label_hash_table = (label **) zmalloc(*this, LABEL_HASH_TABLE_SIZE * sizeof(label *));
}

MIPSImage::~MIPSImage() {
initialize_symbol_table(*this);
for (auto it = labels_to_free.begin(); it != labels_to_free.end(); it ++) {
free((*it)->name);
free(*it);
}
if (label_hash_table)
free(label_hash_table);
}

MIPSImage::MIPSImage(MIPSImage &&other) :
ctx(other.ctx),
mem_img(std::move(other.mem_img)),
reg_img(std::move(other.reg_img)),
bkpt_map(std::move(other.bkpt_map)),
local_labels(other.local_labels),
label_hash_table(other.label_hash_table),
labels_to_free(std::move(other.labels_to_free)),
std_out(std::move(other.std_out)),
std_err(std::move(other.std_err))
{
other.mem_img = {};
other.reg_img = {};
other.bkpt_map.clear();
other.local_labels = NULL;
other.label_hash_table = NULL;
other.labels_to_free.clear();
}

int MIPSImage::get_ctx() const {
Expand Down
4 changes: 3 additions & 1 deletion spim/CPU/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ class MIPSImage {
std::unordered_map<mem_addr, breakpoint> bkpt_map;
// std::unordered_map<mem_addr, label> labels;
label *local_labels = NULL; // No allocs occur here
label *label_hash_table[LABEL_HASH_TABLE_SIZE] = {0};
label **label_hash_table = NULL; // Points to an array of size LABEL_HASH_TABLE_SIZE
std::vector<label *> labels_to_free;

MIPSImagePrintStream std_out;
MIPSImagePrintStream std_err;
public:
MIPSImage(int ctx);
~MIPSImage();
MIPSImage(const MIPSImage&) = delete;
MIPSImage(MIPSImage&&);
philvukovic marked this conversation as resolved.
Show resolved Hide resolved

int get_ctx() const;

Expand Down
16 changes: 16 additions & 0 deletions spim/CPU/image_print_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ MIPSImagePrintStream::~MIPSImagePrintStream() {
sync(); // Flush the buffer on exit
}

MIPSImagePrintStream::MIPSImagePrintStream(MIPSImagePrintStream &&other) :
ctx(other.ctx),
sink(other.sink),
buf(std::move(other.buf))
{
char *base = &buf.front();
setp(base, base + buf.size() - 1);

int offset = other.pptr() - other.pbase();
pbump(offset);
other.pbump(-offset);
}

std::streamsize MIPSImagePrintStream::xsputn(const char *s, std::streamsize n) {
std::streamsize char_pos = 0;
while (char_pos < n) {
Expand Down Expand Up @@ -51,6 +64,9 @@ MIPSImagePrintStream::int_type MIPSImagePrintStream::overflow(MIPSImagePrintStre
int MIPSImagePrintStream::sync() {
std::ptrdiff_t n = pptr() - pbase();
pbump(-n);
if (!n) {
return 0;
}
#ifdef WASM
char *s = new char[n + 1];
strncpy(s, pbase(), n);
Expand Down
1 change: 1 addition & 0 deletions spim/CPU/image_print_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MIPSImagePrintStream : public std::streambuf {
public:
explicit MIPSImagePrintStream(unsigned int ctx, std::ostream &sink, std::size_t buffer_size = 256);
MIPSImagePrintStream(const MIPSImagePrintStream&) = delete;
MIPSImagePrintStream(MIPSImagePrintStream&&);
philvukovic marked this conversation as resolved.
Show resolved Hide resolved
MIPSImagePrintStream& operator=(const MIPSImagePrintStream&) = delete;
~MIPSImagePrintStream();

Expand Down
2 changes: 2 additions & 0 deletions spim/CPU/mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ void mem_dump_profile(MIPSImage &img) {
void
free_instructions (instruction **inst, int n)
{
if (!inst)
return;
for ( ; n > 0; n --, inst ++)
if (*inst)
free_inst (*inst);
Expand Down
2 changes: 2 additions & 0 deletions spim/CPU/sym-tbl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ static void resolve_a_label_sub (MIPSImage &img, label *sym, instruction *inst,
void
initialize_symbol_table (MIPSImage &img)
{
if (!img.get_label_hash_table())
return;
int i;

for (i = 0; i < LABEL_HASH_TABLE_SIZE; i ++)
Expand Down
7 changes: 3 additions & 4 deletions spim/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <optional>
#include <thread>
#include <condition_variable>
#include <utility>

#include "CPU/scanner.h"
#include "CPU/spim-utils.h"
Expand Down Expand Up @@ -71,16 +72,14 @@ void reset(unsigned int max_contexts, std::set<unsigned int> &active_ctxs) {
if (i >= max_contexts) {
continue;
}
ctxs.emplace(i, i);
MIPSImage &new_image = ctxs.at(i);
MIPSImage new_image(i);
initialize_world(new_image, DEFAULT_EXCEPTION_HANDLER, false);
initialize_run_stack(new_image, 0, nullptr);
char file_name[64];
sprintf(file_name, "./input_%d.s", i);
if (read_assembly_file(new_image, file_name)) { // check if the file exists
new_image.reg_image().PC = starting_address(new_image);
} else {
ctxs.erase(i);
ctxs.emplace(i, std::move(new_image));
}
yylex_destroy();
}
Expand Down