Skip to content

Commit

Permalink
aparentemente os arrays 1-dimensionais funcionam bem e o melhor é q n…
Browse files Browse the repository at this point in the history
… ta vazando memório hihi
  • Loading branch information
yuriserka committed Nov 17, 2019
1 parent d6a28d6 commit 374172f
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 69 deletions.
11 changes: 10 additions & 1 deletion .javasrc/ArrayTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
class ArrayTest {
public static void main(String[] args) {
int[] inteiros = {0,10,20,30,40,50};
int[] inteiros = {0, 10, 20, 30, 40, 50};
double[] reais = {0.01, 0.02, 0.03, 0.04, 0.05};
float[] quantis = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f};
long[] enormes = {1000000000000000L, 2000000000000000L, 30000000000000L};
String[] autores = {"Gabriel Castro", "Cláudio Barros", "Matheus Breder", "Yuri Serka"};

System.out.println(inteiros[2]);
System.out.println(reais[4]);
System.out.println(quantis[3]);
System.out.println(enormes[1]);
System.out.println(autores[3]);
}
}
Binary file modified classes/ArrayTest.class
Binary file not shown.
6 changes: 4 additions & 2 deletions include/utils/array_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ class Array_t {
}

template <typename T>
void insert(const T &value, int index) {
void insert(T value, int index) {
this->items[index] = value;
++this->len;
}

int length() { return this->len; }

template <typename T>
T at(const int &index) {
T get(const int &index) {
return this->items[index];
}

std::vector<Any> getCollection() { return this->items; }

private:
int len = 0;
int max_length;
Expand Down
36 changes: 33 additions & 3 deletions include/utils/errors.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef INCLUDE_UTILS_ERRORS_H_
#define INCLUDE_UTILS_ERRORS_H_

#include <stack>
#include <string>

namespace Utils {
Expand All @@ -18,12 +19,28 @@ class Exception : public std::exception {
std::string message;
};

enum errorTypes {
class JvmException : public std::exception {
public:
JvmException(const int &c, const std::string &m) : code(c), message(m) {}

const char *what() const noexcept override { return this->message.c_str(); }

const int errorCode() const { return this->code; }

void printStrackTrace(std::stack<std::string> *call_stack);

private:
std::string getExceptionString();

int code;
std::string message;
};

enum errors {
kCLASSFILE = 1,
kMODE,
KMAGIC,
kMINOR,
kNEGATIVEARRAYSIZE,
kMAJOR,
kCONSTANTPOOL,
kBYTE,
Expand All @@ -36,14 +53,27 @@ enum errorTypes {
kINSTRUCTION,
kMKDIR,
kUTFDATAFORMATEXCEPTION,
kNULLPOINTEREXCEPTION,
kSTACK,
kNOTIMPLEMENTED,
kLS,
kMEMCPY,
kVIEWER,
kFLAG
};

enum vm_errors { kINTERNAL, kOUTOFMEMORY, kSTACKOVERFLOW, kUNKNOWN };

enum java_exceptions {
kINDEXOUTOFBOUNDS,
kNULLPOINTEREXCEPTION,
kNEGATIVEARRAYSIZE,
kARRAYSTORE,
kCLASSCAST,
kINCOMPATIBLECLASSCHANGE,
kARITHMETIC,
kILLEGALACCESS,

};
} // namespace Errors
} // namespace Utils

Expand Down
9 changes: 9 additions & 0 deletions include/utils/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ class Frame {
this->operand_stack.push(operand);
}

template <typename T>
void pushOperand(T *operand) {
if (this->operand_stack.size() > this->max_operand_stack_size) {
throw Utils::Errors::Exception(Utils::Errors::kSTACK,
"Stack Frame Overflow");
}
this->operand_stack.push(operand);
}

template <typename T>
T popOperand() {
auto any = this->operand_stack.top();
Expand Down
15 changes: 12 additions & 3 deletions include/utils/memory_areas/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define INCLUDE_UTILS_MEMORY_AREAS_HEAP_H_

#include <algorithm>
#include <list>
#include <vector>
#include "utils/memory_areas/thread.h"
#include "utils/object.h"
Expand All @@ -11,6 +12,13 @@ class Heap {
public:
Heap() = default;

~Heap() {
this->object_refs.remove_if([](Utils::Object *obj) {
delete obj;
return true;
});
}

void initialize(Thread *th, const std::string &name) {
this->initialized_classes.push_back(name);
th->executeMethod("<clinit>");
Expand All @@ -22,15 +30,16 @@ class Heap {
name) != this->initialized_classes.end();
}

Utils::Object *pushReference(const Utils::Object &obj) {
Utils::Object *pushReference(Utils::Object *obj) {
this->object_refs.push_back(obj);

return &this->object_refs[this->last_obj_index++];
auto it = std::next(this->object_refs.begin(), this->last_obj_index++);
return *it;
}

private:
int last_obj_index = 0;
std::vector<Utils::Object> object_refs;
std::list<Utils::Object *> object_refs;
std::vector<std::string> initialized_classes;
};
} // namespace MemoryAreas
Expand Down
13 changes: 8 additions & 5 deletions include/utils/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define INCLUDE_UTILS_OBJECT_H_

#include <ostream>
#include "utils/array_t.h"
#include "utils/external/any.h"
#include "utils/reference_kind.h"

Expand All @@ -10,7 +11,13 @@ class Object {
public:
Object() {
this->type = Reference::objectref_types::kREF_NULL;
this->data = Any();
this->data = nullptr;
}

~Object() {
if (this->data.is<Array_t *>()) {
delete this->data.as<Array_t *>();
}
}

template <typename T>
Expand All @@ -24,8 +31,4 @@ class Object {
};
} // namespace Utils

// Any a = Object<double>(54.55521);
// std::cout << a.is<Object<double>>() << " " << a.as<Object<double>>() <<
// "\n";

#endif // INCLUDE_UTILS_OBJECT_H_
21 changes: 13 additions & 8 deletions src/instructions/instruction_set/constant_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "utils/flags.h"
#include "utils/memory.h"
#include "utils/memory_areas/heap.h"
#include "utils/memory_areas/method_area.h"
#include "utils/memory_areas/thread.h"
#include "utils/object.h"
Expand Down Expand Up @@ -33,18 +34,20 @@ std::vector<int> LoadCat1::execute(
}
case cp::kCONSTANT_STRING: {
auto kstring_info = kpool_info.getClass<cp::CONSTANT_String_info>();
auto objectref = Utils::Object(
auto objectref = new Utils::Object(
kstring_info->getValue(th->method_area->runtime_constant_pool),
Utils::Reference::objectref_types::kREF_STRING);
th->current_frame->pushOperand(objectref);
auto stringref = th->heap->pushReference(objectref);
th->current_frame->pushOperand(stringref);
break;
}
case cp::kCONSTANT_CLASS: {
auto kclass_info = kpool_info.getClass<cp::CONSTANT_Class_info>();
auto objectref = Utils::Object(
auto objectref = new Utils::Object(
kclass_info->getValue(th->method_area->runtime_constant_pool),
Utils::Reference::objectref_types::kREF_CLASS);
th->current_frame->pushOperand(objectref);
auto classref = th->heap->pushReference(objectref);
th->current_frame->pushOperand(classref);
break;
}
}
Expand Down Expand Up @@ -76,18 +79,20 @@ std::vector<int> LoadCat1Wide::execute(
}
case cp::kCONSTANT_STRING: {
auto kstring_info = kpool_info.getClass<cp::CONSTANT_String_info>();
auto objectref = Utils::Object(
auto objectref = new Utils::Object(
kstring_info->getValue(th->method_area->runtime_constant_pool),
Utils::Reference::objectref_types::kREF_STRING);
th->current_frame->pushOperand(objectref);
auto stringref = th->heap->pushReference(objectref);
th->current_frame->pushOperand(stringref);
break;
}
case cp::kCONSTANT_CLASS: {
auto kclass_info = kpool_info.getClass<cp::CONSTANT_Class_info>();
auto objectref = Utils::Object(
auto objectref = new Utils::Object(
kclass_info->getValue(th->method_area->runtime_constant_pool),
Utils::Reference::objectref_types::kREF_CLASS);
th->current_frame->pushOperand(objectref);
auto classref = th->heap->pushReference(objectref);
th->current_frame->pushOperand(classref);
break;
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/instructions/instruction_set/double.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "instructions/instruction_set/double.h"

#include <cmath>
#include "utils/array_t.h"
#include "utils/flags.h"
#include "utils/memory_areas/thread.h"
#include "utils/object.h"

namespace Instructions {
namespace Double {
Expand Down Expand Up @@ -57,6 +59,10 @@ std::vector<int> LoadFromArray::execute(
if (Utils::Flags::options.kDEBUG) {
std::cout << "Executando " << Opcodes::getMnemonic(this->opcode) << "\n";
}
int index = th->current_frame->popOperand<int>();
auto arrayref = th->current_frame->popOperand<Utils::Object *>()
->data.as<Utils::Array_t *>();
th->current_frame->pushOperand(arrayref->get<double>(index));
return {};
}
// ----------------------------------------------------------------------------
Expand All @@ -66,6 +72,11 @@ std::vector<int> StoreIntoArray::execute(
if (Utils::Flags::options.kDEBUG) {
std::cout << "Executando " << Opcodes::getMnemonic(this->opcode) << "\n";
}
auto value = th->current_frame->popOperand<double>();
auto index = th->current_frame->popOperand<int>();
auto arrayref = th->current_frame->popOperand<Utils::Object *>()
->data.as<Utils::Array_t *>();
arrayref->insert(value, index);
return {};
}
// ----------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions src/instructions/instruction_set/float.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "instructions/instruction_set/float.h"

#include <cmath>
#include "utils/array_t.h"
#include "utils/flags.h"
#include "utils/memory_areas/thread.h"
#include "utils/object.h"

namespace Instructions {
namespace Float {
Expand Down Expand Up @@ -57,6 +59,10 @@ std::vector<int> LoadFromArray::execute(
if (Utils::Flags::options.kDEBUG) {
std::cout << "Executando " << Opcodes::getMnemonic(this->opcode) << "\n";
}
int index = th->current_frame->popOperand<int>();
auto arrayref = th->current_frame->popOperand<Utils::Object *>()
->data.as<Utils::Array_t *>();
th->current_frame->pushOperand(arrayref->get<float>(index));
return {};
}
// ----------------------------------------------------------------------------
Expand All @@ -66,6 +72,11 @@ std::vector<int> StoreIntoArray::execute(
if (Utils::Flags::options.kDEBUG) {
std::cout << "Executando " << Opcodes::getMnemonic(this->opcode) << "\n";
}
auto value = th->current_frame->popOperand<float>();
auto index = th->current_frame->popOperand<int>();
auto arrayref = th->current_frame->popOperand<Utils::Object *>()
->data.as<Utils::Array_t *>();
arrayref->insert(value, index);
return {};
}
// ----------------------------------------------------------------------------
Expand Down
12 changes: 6 additions & 6 deletions src/instructions/instruction_set/integer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ std::vector<int> LoadFromArray::execute(
std::cout << "Executando " << Opcodes::getMnemonic(this->opcode) << "\n";
}
int index = th->current_frame->popOperand<int>();
auto arrayref =
th->current_frame->popOperand<Utils::Object>().data.as<Utils::Array_t>();
th->current_frame->pushOperand(arrayref.at<int>(index));
auto arrayref = th->current_frame->popOperand<Utils::Object *>()
->data.as<Utils::Array_t *>();
th->current_frame->pushOperand(arrayref->get<int>(index));
return {};
}
// ----------------------------------------------------------------------------
Expand All @@ -118,9 +118,9 @@ std::vector<int> StoreIntoArray::execute(
}
auto value = th->current_frame->popOperand<int>();
auto index = th->current_frame->popOperand<int>();
auto arrayref =
th->current_frame->popOperand<Utils::Object>().data.as<Utils::Array_t>();
arrayref.insert(value, index);
auto arrayref = th->current_frame->popOperand<Utils::Object *>()
->data.as<Utils::Array_t *>();
arrayref->insert(value, index);
return {};
}
// ----------------------------------------------------------------------------
Expand Down
Loading

0 comments on commit 374172f

Please sign in to comment.