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
56 changes: 54 additions & 2 deletions spim/CPU/image.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,71 @@
#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() {
free_internals();
}

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();
}

MIPSImage &MIPSImage::operator=(MIPSImage &&other) {
free_internals();
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.local_labels = NULL;
other.label_hash_table = NULL;
other.labels_to_free.clear();

return *this;
}

void MIPSImage::free_internals() {
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);
}

int MIPSImage::get_ctx() const {
Expand Down
9 changes: 8 additions & 1 deletion spim/CPU/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,21 @@ 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;

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

MIPSImage &operator=(MIPSImage &) = delete;
MIPSImage &operator=(MIPSImage &&);

int get_ctx() const;

Expand Down
40 changes: 36 additions & 4 deletions spim/CPU/image_print_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

MIPSImagePrintStream::MIPSImagePrintStream(unsigned int ctx, std::ostream &sink, std::size_t buffer_size) :
ctx(ctx),
sink(sink),
sink(&sink),
buf(buffer_size + 1)
{
char *base = &buf.front();
Expand All @@ -21,6 +21,35 @@ 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);

move_buffer_pointers(std::move(other));
}

MIPSImagePrintStream& MIPSImagePrintStream::operator=(MIPSImagePrintStream &&other) {
ctx = other.ctx;
sink = other.sink;
buf = std::move(other.buf);

char *base = &buf.front();
setp(base, base + buf.size() - 1);

move_buffer_pointers(std::move(other));
return *this;
}

inline void MIPSImagePrintStream::move_buffer_pointers(MIPSImagePrintStream &&other) {
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,17 +80,20 @@ 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);
s[n] = 0;
if (&sink == &std::cout) {
if (sink == &std::cout) {
MAIN_THREAD_ASYNC_EM_ASM({
writeStdOut($0, UTF8ToString($1));
}, ctx, s);
return 0;
}
if (&sink == &std::cerr) {
if (sink == &std::cerr) {
MAIN_THREAD_ASYNC_EM_ASM({
writeStdErr($0, UTF8ToString($1));
}, ctx, s);
Expand All @@ -70,6 +102,6 @@ int MIPSImagePrintStream::sync() {
delete[] s;
return -1;
#else
return !((bool) sink.write(pbase(), n));
return !((bool) sink->write(pbase(), n));
#endif
}
14 changes: 10 additions & 4 deletions spim/CPU/image_print_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,30 @@
* will append it to the frontend div and append it to an array for the ctx's stdout or stderr.
*
* If compiled for unix, it will simply forward it to std::cout/std::cerr.
*
* It's unclear if we currently want to add std::ostream to the constructor, but that's an idea that
* can be implemented if needed.
*/
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& operator=(MIPSImagePrintStream&&);
~MIPSImagePrintStream();

private:
inline void move_buffer_pointers(MIPSImagePrintStream &&other);
std::streamsize xsputn(const char *s, std::streamsize n);
int_type overflow(int_type ch);
int sync();

unsigned int ctx;
std::ostream &sink;
// Note that this cannot be a reference type as std::ostream doesn't implement a public
// copy/move constructor. As such, when it comes to the assignment overloads, if sink was a
// `std::ostream &`, we cannot simply assign `this->sink = other.sink`. A pointer achieves
// the desired result without having using ostream's protected move assignment.
// Note: The use of a pointer rather than a reference makes no difference if the original
// ostream goes out of scope and gets destroyed.
std::ostream *sink;
philvukovic marked this conversation as resolved.
Show resolved Hide resolved
std::vector<char> buf;
};

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
13 changes: 8 additions & 5 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 @@ -49,7 +50,7 @@ void start_simulator(unsigned int max_contexts, std::set<unsigned int> active_ct
// Actually, what if we can use the proxy queue? Idk how useful that would be
}

void reset(unsigned int max_contexts, std::set<unsigned int> &active_ctxs) {
void shutdown() {
{
std::lock_guard<std::mutex> lock(settings_mtx);
finished = true;
Expand All @@ -59,6 +60,10 @@ void reset(unsigned int max_contexts, std::set<unsigned int> &active_ctxs) {
if (simulator_thread.joinable()) {
simulator_thread.join();
}
}

void reset(unsigned int max_contexts, std::set<unsigned int> &active_ctxs) {
shutdown();

// Since we join if a thread already exists, we dont need to lock. YIPPEE!
/* std::lock(simulator_mtx, settings_mtx); */
Expand All @@ -71,16 +76,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
1 change: 1 addition & 0 deletions spim/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern bool simulator_ready;
void start_simulator(unsigned int max_contexts, std::set<unsigned int> active_ctxs);

void reset(unsigned int max_contexts, std::set<unsigned int> &active_ctxs);
void shutdown();
void step_simulation(unsigned additional_steps);
void play_simulation();
void pause_simulation();
Expand Down