Skip to content

Commit

Permalink
Remove index member of FunctionDef struct
Browse files Browse the repository at this point in the history
FunctionDef's index was not really serving any useful purpose.

Signed-off-by: Nazim Uddin Bhuiyan <[email protected]>
  • Loading branch information
nbhuiyan authored and rwy7 committed Jul 12, 2018
1 parent 93365b8 commit 142a367
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 29 deletions.
1 change: 0 additions & 1 deletion b9/include/b9/Module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class VirtualMachine;

struct FunctionDef {
std::string name;
uint32_t index;
std::vector<Instruction> instructions;
std::uint32_t nparams;
std::uint32_t nlocals;
Expand Down
5 changes: 2 additions & 3 deletions b9/include/b9/deserialize.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,9 @@ void readStringSection(std::istream &in, std::vector<std::string> &strings);

bool readInstructions(std::istream &in, std::vector<Instruction> &instructions);

void readFunctionData(std::istream &in, FunctionDef &functionSpec,
uint32_t index);
void readFunctionData(std::istream &in, FunctionDef &functionSpec);

void readFunction(std::istream &in, FunctionDef &functionDef, uint32_t index);
void readFunction(std::istream &in, FunctionDef &functionDef);

void readFunctionSection(std::istream &in, std::vector<FunctionDef> &functions);

Expand Down
13 changes: 5 additions & 8 deletions b9/src/deserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@ bool readInstructions(std::istream &in,
return true;
}

void readFunctionData(std::istream &in, FunctionDef &functionDef,
uint32_t index) {
void readFunctionData(std::istream &in, FunctionDef &functionDef) {
readString(in, functionDef.name);
functionDef.index = index;
bool ok = readNumber(in, functionDef.nparams) &&
readNumber(in, functionDef.nlocals);
if (!ok) {
throw DeserializeException{"Error reading function data"};
}
}

void readFunction(std::istream &in, FunctionDef &functionDef, uint32_t index) {
readFunctionData(in, functionDef, index);
void readFunction(std::istream &in, FunctionDef &functionDef) {
readFunctionData(in, functionDef);
if (!readInstructions(in, functionDef.instructions)) {
throw DeserializeException{"Error reading instructions"};
}
Expand All @@ -59,9 +57,8 @@ void readFunctionSection(std::istream &in,
throw DeserializeException{"Error reading function count"};
}
for (uint32_t i = 0; i < functionCount; i++) {
functions.emplace_back(
FunctionDef{"", 0, std::vector<Instruction>{}, 0, 0});
readFunction(in, functions.back(), i);
functions.emplace_back(FunctionDef{"", std::vector<Instruction>{}, 0, 0});
readFunction(in, functions.back());
}
}

Expand Down
3 changes: 1 addition & 2 deletions js_compiler/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ var LabelTable = function () {
/// A section of code. CodeBody has information about params and locals, but no information about indexes or it's name.
/// The name-to-index mapping is managed externally, by the module's FunctionTable. Eventually, the params and locals
/// might move to a lexical environment, and this will become a simple bytecode array.
function FunctionDefinition(outer, name, index) {
function FunctionDefinition(outer, name) {
this.name = name;
this.index = index;
this.params = new SymbolTable();
this.locals = new SymbolTable();
this.labels = new LabelTable();
Expand Down
9 changes: 4 additions & 5 deletions test/b9test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ TEST(MyTest, arguments) {
{OpCode::INT_ADD},
{OpCode::FUNCTION_RETURN},
END_SECTION};
uint32_t index = 0;
m->functions.push_back(b9::FunctionDef{"add_args", index, i, 2, 0});
m->functions.push_back(b9::FunctionDef{"add_args", i, 2, 0});
vm.load(m);
auto r = vm.run("add_args", {{AS_INT48, 1}, {AS_INT48, 2}});
EXPECT_EQ(r, Value(AS_INT48, 3));
Expand All @@ -155,7 +154,7 @@ TEST(MyTest, jitSimpleProgram) {
std::vector<Instruction> i = {{OpCode::INT_PUSH_CONSTANT, 0xdead},
{OpCode::FUNCTION_RETURN},
END_SECTION};
m->functions.push_back(b9::FunctionDef{"add", 0, i, 0, 0});
m->functions.push_back(b9::FunctionDef{"add", i, 0, 0});
vm.load(m);
vm.generateAllCode();
auto r = vm.run("add", {});
Expand All @@ -170,7 +169,7 @@ TEST(MyTest, haveAVariable) {
std::vector<Instruction> i = {{OpCode::INT_PUSH_CONSTANT, 0xdead},
{OpCode::FUNCTION_RETURN},
END_SECTION};
m->functions.push_back(b9::FunctionDef{"add", 0, i, 0, 0});
m->functions.push_back(b9::FunctionDef{"add", i, 0, 0});
vm.load(m);
vm.generateAllCode();
auto r = vm.run("add", {});
Expand All @@ -193,7 +192,7 @@ TEST(ObjectTest, allocateSomething) {
{OpCode::FUNCTION_RETURN}, // finish with constant 0
END_SECTION};
m->strings.push_back("Hello, World");
m->functions.push_back(b9::FunctionDef{"allocate_object", 0, i, 0, 1});
m->functions.push_back(b9::FunctionDef{"allocate_object", i, 0, 1});
vm.load(m);
Value r = vm.run("allocate_object", {});
EXPECT_EQ(r, Value(AS_INT48, 0));
Expand Down
20 changes: 10 additions & 10 deletions test/testDisasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ std::shared_ptr<Module> makeSimpleModule() {
{OpCode::FUNCTION_RETURN},
END_SECTION};

m->functions.push_back(b9::FunctionDef{"add_args", 0, i, 2, 4});
m->functions.push_back(b9::FunctionDef{"add_args", i, 2, 4});
m->strings = {"FruitBat", "cantaloupe", "123$#*"};

return m;
Expand All @@ -41,23 +41,23 @@ std::shared_ptr<Module> makeComplexModule() {
{OpCode::INT_ADD},
{OpCode::FUNCTION_RETURN},
END_SECTION};
m->functions.push_back(b9::FunctionDef{"add_args", 0, i1, 1, 1});
m->functions.push_back(b9::FunctionDef{"add_args", i1, 1, 1});

std::vector<Instruction> i2 = {{OpCode::PUSH_FROM_LOCAL, 0},
{OpCode::PRIMITIVE_CALL, 0},
{OpCode::DROP, 0},
{OpCode::INT_PUSH_CONSTANT, 0},
{OpCode::FUNCTION_RETURN, 0},
END_SECTION};
m->functions.push_back(b9::FunctionDef{"b9PrintString", 1, i2, 2, 2});
m->functions.push_back(b9::FunctionDef{"b9PrintString", i2, 2, 2});

std::vector<Instruction> i3 = {{OpCode::PUSH_FROM_LOCAL, 0},
{OpCode::PRIMITIVE_CALL, 1},
{OpCode::DROP, 0},
{OpCode::INT_PUSH_CONSTANT, 0},
{OpCode::FUNCTION_RETURN, 0},
END_SECTION};
m->functions.push_back(b9::FunctionDef{"b9PrintNumber", 2, i3, 3, 3});
m->functions.push_back(b9::FunctionDef{"b9PrintNumber", i3, 3, 3});

m->strings = {"mercury", "Venus", "EARTH", "mars", "JuPiTeR", "sAtUrN"};

Expand Down Expand Up @@ -127,8 +127,8 @@ void roundTripFunctionData(FunctionDef& f) {
EXPECT_TRUE(buffer.good());

std::vector<Instruction> i2;
auto f2 = FunctionDef{"", 0, i2, 0, 0};
readFunctionData(buffer, f2, 0);
auto f2 = FunctionDef{"", i2, 0, 0};
readFunctionData(buffer, f2);
EXPECT_EQ(f, f2);
}

Expand All @@ -138,10 +138,10 @@ TEST(RoundTripSerializationTest, testFunctionData) {
{OpCode::INT_ADD},
{OpCode::FUNCTION_RETURN},
END_SECTION};
auto f1 = FunctionDef{"testName", 0, i1, 4, 5};
auto f1 = FunctionDef{"testName", i1, 4, 5};

std::vector<Instruction> i2 = {};
auto f2 = FunctionDef{"testName", 0, i2, 4, 5};
auto f2 = FunctionDef{"testName", i2, 4, 5};

roundTripFunctionData(f1);
roundTripFunctionData(f2);
Expand All @@ -168,7 +168,7 @@ TEST(RoundTripSerializationTest, testFunctionSection) {
{OpCode::INT_ADD},
{OpCode::FUNCTION_RETURN},
END_SECTION};
auto f1 = FunctionDef{"testName", 0, i1, 4, 5};
auto f1 = FunctionDef{"testName", i1, 4, 5};
std::vector<FunctionDef> functions;
functions.push_back(f1);

Expand Down Expand Up @@ -201,7 +201,7 @@ TEST(RoundTripSerializationTest, testSerializeDeserialize) {
{OpCode::INT_ADD},
{OpCode::FUNCTION_RETURN},
END_SECTION};
auto f = FunctionDef{"testName", 0, i, 4, 5};
auto f = FunctionDef{"testName", i, 4, 5};
std::vector<FunctionDef> functions;
functions.push_back(f);
m3->functions = functions;
Expand Down

0 comments on commit 142a367

Please sign in to comment.