Skip to content

Commit

Permalink
modify code to support visual studio
Browse files Browse the repository at this point in the history
  • Loading branch information
meepen committed Jul 2, 2021
1 parent 0b8b058 commit 1a90281
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 62 deletions.
11 changes: 8 additions & 3 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ local libs = {
}
},
links = {
["not windows"] = {
["system:not windows"] = {
"rt"
}
}
Expand Down Expand Up @@ -202,8 +202,10 @@ workspace "lorelai"
"protoc --proto_path=../src/vm/proto/src --cpp_out=../src/vm/proto ../src/vm/proto/src/bytecode.proto"
}

pchheader "../src/vm/stdafx.h"
pchsource "../src/vm/stdafx.cpp"
includedirs { "src/vm" }

pchheader "stdafx.h"
pchsource "src/vm/stdafx.cpp"

links {
"protobuf",
Expand All @@ -222,6 +224,9 @@ workspace "lorelai"
"src/vm/bytecode/*.cpp"
}

filter { "action:vs*" }
buildoptions { "/FI stdafx.h" }

filter { "platforms:x86 or x86-64" }
configuration "with-jit"
defines { "LORELAI_X86_FASTEST" }
Expand Down
32 changes: 16 additions & 16 deletions src/vm/bytecode/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ using namespace lorelai::parser;
using namespace lorelai::parser;
using namespace lorelai::bytecode;

static void generate_numberexpression(bytecodegenerator &gen, node &expr, size_t target, size_t size) {
static void generate_numberexpression(bytecodegenerator &gen, node &expr, std::uint32_t target, std::uint32_t size) {
if (size == 0) {
return;
}
gen.emit(bytecode::instruction_opcode_NUMBER, target, gen.add(dynamic_cast<expressions::numberexpression *>(&expr)->data));
}

static void generate_stringexpression(bytecodegenerator &gen, node &expr, size_t target, size_t size) {
static void generate_stringexpression(bytecodegenerator &gen, node &expr, std::uint32_t target, std::uint32_t size) {
if (size == 0) {
return;
}
gen.emit(bytecode::instruction_opcode_STRING, target, gen.add(dynamic_cast<expressions::stringexpression *>(&expr)->data));
}

static void generate_enclosedexpression(bytecodegenerator &gen, node &expr, size_t target, size_t size) {
static void generate_enclosedexpression(bytecodegenerator &gen, node &expr, std::uint32_t target, std::uint32_t size) {
auto &child = *dynamic_cast<expressions::enclosedexpression *>(&expr)->children[0].get();
gen.runexpressionhandler(child, target, size > 0 ? 1 : 0);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ the ideal output would be

class binopsimplifier {
public:
binopsimplifier(bytecodegenerator &_gen, expressions::binopexpression &expr, size_t target, size_t size) : gen(_gen) {
binopsimplifier(bytecodegenerator &_gen, expressions::binopexpression &expr, std::uint32_t target, std::uint32_t size) : gen(_gen) {
if (size == 0) {
auto find = binoplookup.find(expr.op);
if (find == binoplookup.end()) {
Expand All @@ -98,7 +98,7 @@ class binopsimplifier {
}
}
else {
size_t rhs_stack;
std::uint32_t rhs_stack;
bool free_right = false;
gen.runexpressionhandler(expr.lhs, target, 1);

Expand All @@ -115,7 +115,7 @@ class binopsimplifier {
}
}

bool get(std::shared_ptr<node> &expr, size_t *stackposout, bool leftside) {
bool get(std::shared_ptr<node> &expr, std::uint32_t *stackposout, bool leftside) {
if (auto name = dynamic_cast<expressions::nameexpression *>(expr.get())) {
if (auto scope = gen.curfunc.curscope->findvariablescope(name->name)) {
*stackposout = scope->getvariableindex(name->name);
Expand All @@ -130,7 +130,7 @@ class binopsimplifier {
bytecodegenerator &gen;
};

static void generate_binopexpression(bytecodegenerator &gen, node &_expr, size_t target, size_t size) {
static void generate_binopexpression(bytecodegenerator &gen, node &_expr, std::uint32_t target, std::uint32_t size) {
auto &expr = *dynamic_cast<expressions::binopexpression *>(&_expr);
binopsimplifier simplify(gen, expr, target, size);
}
Expand All @@ -142,7 +142,7 @@ std::unordered_map<string, bytecode::instruction_opcode> unoplookup = {
{ "-", bytecode::instruction_opcode_MINUS },
};

static void generate_unopexpression(bytecodegenerator &gen, node &_expr, size_t target, size_t size) {
static void generate_unopexpression(bytecodegenerator &gen, node &_expr, std::uint32_t target, std::uint32_t size) {
auto &expr = *dynamic_cast<expressions::unopexpression *>(&_expr);

if (size > 0) {
Expand All @@ -162,31 +162,31 @@ static void generate_unopexpression(bytecodegenerator &gen, node &_expr, size_t
}
}

static void generate_nilexpression(bytecodegenerator &gen, node &expr, size_t target, size_t size) {
static void generate_nilexpression(bytecodegenerator &gen, node &expr, std::uint32_t target, std::uint32_t size) {
if (size == 0) {
return;
}

gen.emit(bytecode::instruction_opcode_CONSTANT, target, 2);
}

static void generate_falseexpression(bytecodegenerator &gen, node &expr, size_t target, size_t size) {
static void generate_falseexpression(bytecodegenerator &gen, node &expr, std::uint32_t target, std::uint32_t size) {
if (size == 0) {
return;
}

gen.emit(bytecode::instruction_opcode_CONSTANT, target, 1);
}

static void generate_trueexpression(bytecodegenerator &gen, node &expr, size_t target, size_t size) {
static void generate_trueexpression(bytecodegenerator &gen, node &expr, std::uint32_t target, std::uint32_t size) {
if (size == 0) {
return;
}

gen.emit(bytecode::instruction_opcode_CONSTANT, target, 0);
}

static void generate_indexexpression(bytecodegenerator &gen, node &_expr, size_t target, size_t size) {
static void generate_indexexpression(bytecodegenerator &gen, node &_expr, std::uint32_t target, std::uint32_t size) {
auto &expr = *dynamic_cast<expressions::indexexpression *>(&_expr);

bool is_temp = size == 0;
Expand All @@ -207,7 +207,7 @@ static void generate_indexexpression(bytecodegenerator &gen, node &_expr, size_t
}
}

static void generate_dotexpression(bytecodegenerator &gen, node &_expr, size_t target, size_t size) {
static void generate_dotexpression(bytecodegenerator &gen, node &_expr, std::uint32_t target, std::uint32_t size) {
auto &expr = *dynamic_cast<expressions::dotexpression *>(&_expr);
bool is_temp = size == 0;

Expand All @@ -228,7 +228,7 @@ static void generate_dotexpression(bytecodegenerator &gen, node &_expr, size_t t
}
}

static void generate_nameexpression(bytecodegenerator &gen, node &_expr, size_t target, size_t size) {
static void generate_nameexpression(bytecodegenerator &gen, node &_expr, std::uint32_t target, std::uint32_t size) {
if (size == 0) {
return;
}
Expand All @@ -248,11 +248,11 @@ static void generate_nameexpression(bytecodegenerator &gen, node &_expr, size_t
}
}

static void generate_functioncallexpression(bytecodegenerator &gen, node &_expr, size_t target, size_t size) {
static void generate_functioncallexpression(bytecodegenerator &gen, node &_expr, std::uint32_t target, std::uint32_t size) {
auto &expr = *dynamic_cast<expressions::functioncallexpression *>(&_expr);
auto &arglist = *dynamic_cast<args *>(expr.arglist.get());

size_t stacksize = std::max(size, arglist.children.size() + 1 + (expr.methodname ? 1 : 0));
size_t stacksize = std::max(size, static_cast<std::uint32_t>(arglist.children.size() + 1 + (expr.methodname ? 1 : 0)));

bool using_temp = stacksize > size || target == -1;
auto functionindex = target;
Expand Down
3 changes: 2 additions & 1 deletion src/vm/bytecode/expressions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <unordered_map>
#include <typeindex>
#include <cstdint>

namespace lorelai {
namespace parser {
Expand All @@ -11,7 +12,7 @@ namespace lorelai {

namespace bytecode {
class bytecodegenerator;
using expressiongenerator = void (*)(bytecodegenerator &gen, parser::node &expr, size_t stackindex, size_t size);
using expressiongenerator = void (*)(bytecodegenerator &gen, parser::node &expr, std::uint32_t stackindex, std::uint32_t size);

extern std::unordered_map<std::type_index, expressiongenerator> expressionmap;
}
Expand Down
10 changes: 5 additions & 5 deletions src/vm/bytecode/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace lorelai {
}

// finds a local variable and returns a stack position
size_t findlocal(string name) {
std::uint32_t findlocal(string name) {
auto found = curscope->findvariablescope(name, firstscope);
if (!found) {
// not in current scope, what do?
Expand All @@ -55,7 +55,7 @@ namespace lorelai {
return found->getvariableindex(name);
}

size_t createlocal(string name) {
std::uint32_t createlocal(string name) {
// since it already exists and won't be used passed this point, reuse slot
// this might have to change if used as an upvalue?
if (curscope->hasvariable(name)) {
Expand All @@ -66,7 +66,7 @@ namespace lorelai {
return scopeindex;
}

size_t createlocals(std::vector<string> names) {
std::uint32_t createlocals(std::vector<string> names) {
if (names.size() == 0) {
return 0;
}
Expand All @@ -85,11 +85,11 @@ namespace lorelai {
return target;
}

size_t gettemp(size_t amount = 1) {
std::uint32_t gettemp(size_t amount = 1) {
return funcstack.getslots(amount);
}

void freetemp(size_t slot, size_t amount = 1) {
void freetemp(std::uint32_t slot, std::uint32_t amount = 1) {
funcstack.freeslots(slot, amount);
}

Expand Down
20 changes: 10 additions & 10 deletions src/vm/bytecode/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ using namespace lorelai::bytecode;


LORELAI_VISIT_DEFINE(bytecodegenerator, statements::localassignmentstatement) { // TODO: VARARG
std::deque<size_t> indexes;
std::deque<std::uint32_t> indexes;
for (auto &_name : obj.left) {
auto name = dynamic_cast<expressions::nameexpression *>(_name.get());
if (!name) {
Expand Down Expand Up @@ -36,7 +36,7 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::localassignmentstatement) {
}

// fill the rest with nil
for (int i = obj.right.size(); i < obj.left.size(); i++) {
for (auto i = obj.right.size(); i < obj.left.size(); i++) {
auto index = indexes[0];
indexes.pop_front();

Expand All @@ -55,11 +55,11 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::assignmentstatement) { // TO
if (auto name = dynamic_cast<expressions::nameexpression *>(lhs.get())) {
auto scope = curfunc.curscope->findvariablescope(name->name);
if (scope) {
size_t target = scope->getvariableindex(name->name);
auto target = scope->getvariableindex(name->name);
pushornil(obj.right, i, target);
} // TODO: upvalues
else {
size_t target = curfunc.gettemp(2);
auto target = curfunc.gettemp(2);

pushornil(obj.right, i, target + 1);
emit(instruction_opcode_ENVIRONMENTSET, target, add(name->name), target + 1);
Expand All @@ -68,7 +68,7 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::assignmentstatement) { // TO
}
}
else if (auto index = dynamic_cast<expressions::dotexpression *>(lhs.get())) {
size_t target = curfunc.gettemp(3);
auto target = curfunc.gettemp(3);

runexpressionhandler(index->prefix, target, 1);
expressions::stringexpression string(index->index->tostring());
Expand All @@ -80,7 +80,7 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::assignmentstatement) { // TO
curfunc.freetemp(target, 3);
}
else if (auto index = dynamic_cast<expressions::indexexpression *>(lhs.get())) {
size_t target = curfunc.gettemp(3);
auto target = curfunc.gettemp(3);

runexpressionhandler(index->prefix, target, 1);
runexpressionhandler(index->index, target + 1, 1);
Expand Down Expand Up @@ -222,9 +222,9 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::forinstatement) {
}

// loop prep: local f, s, v = inexprs
for (int i = 0; i < obj.inexprs.size(); i++) {
for (size_t i = 0; i < obj.inexprs.size(); i++) {
auto &inexpr = obj.inexprs[i];
size_t amount;
std::uint32_t amount;
if (i == obj.inexprs.size() - 1 && i < 3) {
amount = 3 - i;
}
Expand Down Expand Up @@ -323,7 +323,7 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::returnstatement) {
auto realrets = obj.children.size();
auto rets = realrets;
auto target = curfunc.gettemp(realrets);
size_t varargtype = 0;
std::uint32_t varargtype = 0;
for (int i = 0; i < obj.children.size(); i++) {
auto &child = obj.children[i];

Expand All @@ -347,7 +347,7 @@ LORELAI_VISIT_DEFINE(bytecodegenerator, statements::returnstatement) {
return false;
}

void bytecodegenerator::pushornil(std::vector<std::shared_ptr<lorelai::parser::node>> &v, int index, size_t target) {
void bytecodegenerator::pushornil(std::vector<std::shared_ptr<lorelai::parser::node>> &v, int index, std::uint32_t target) {
if (index < v.size()) {
runexpressionhandler(v[index], target, 1);
}
Expand Down
12 changes: 6 additions & 6 deletions src/vm/bytecode/generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ namespace lorelai {
struct _ifqueue {
bytecode::instruction *patch = nullptr;
std::vector<bytecode::instruction *> jmpends;
size_t target;
std::uint32_t target;
};

std::vector<_ifqueue> ifqueue;

struct _loopqueue {
int startinstr;
size_t stackreserved;
size_t extrastack;
std::uint32_t stackreserved;
std::uint32_t extrastack;
std::vector<bytecode::instruction *> patches;
};

Expand Down Expand Up @@ -96,10 +96,10 @@ namespace lorelai {
LORELAI_VISIT_FUNCTION(statements::returnstatement);

private:
void pushornil(std::vector<std::shared_ptr<lorelai::parser::node>> &v, int index, size_t target);
void pushornil(std::vector<std::shared_ptr<lorelai::parser::node>> &v, int index, std::uint32_t target);

public:
void runexpressionhandler(lorelai::parser::node &_expr, size_t target, size_t size) {
void runexpressionhandler(lorelai::parser::node &_expr, std::uint32_t target, std::uint32_t size) {
auto found = expressionmap.find(typeid(_expr));

if (found == expressionmap.end()) {
Expand All @@ -109,7 +109,7 @@ namespace lorelai {
return found->second(*this, _expr, target, size);
}

void runexpressionhandler(std::shared_ptr<lorelai::parser::node> _expr, size_t target, size_t size) {
void runexpressionhandler(std::shared_ptr<lorelai::parser::node> _expr, std::uint32_t target, std::uint32_t size) {
return runexpressionhandler(*_expr.get(), target, size);
}

Expand Down
8 changes: 4 additions & 4 deletions src/vm/bytecode/scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ namespace lorelai {
return nullptr;
}

size_t addvariable(string name, size_t stackpos) {
std::uint32_t addvariable(string name, std::uint32_t stackpos) {
variables[name] = stackpos;
return stackpos;
}

size_t getvariableindex(string name) {
std::uint32_t getvariableindex(string name) {
auto var = variables.find(name);
if (var != variables.end()) {
return var->second;
Expand All @@ -37,12 +37,12 @@ namespace lorelai {
return -1;
}

size_t hasvariable(string name) {
std::uint32_t hasvariable(string name) {
return variables.find(name) != variables.end();
}

public:
std::unordered_map<string, size_t> variables;
std::unordered_map<string, std::uint32_t> variables;
std::shared_ptr<scope> parent;
};
}
Expand Down
Loading

0 comments on commit 1a90281

Please sign in to comment.