Skip to content

Commit

Permalink
perf: inline global constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 20, 2024
1 parent 32d9c32 commit c8aef44
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/bytecode/builtin_functions/DefineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ namespace Bytecode::BuiltinFunctions {
if (args[0]->type == SyntaxTreeNode::TokenNode) {
const auto reg = program.declare_global(
((TokenNode *) args[0])->getName());
if (args[1]->type == SyntaxTreeNode::TokenNode &&
((TokenNode *) args[1])->token.type != Token::Symbol) {
program.declare_constant(
((TokenNode *) args[0])->getName(),
StackObject(((TokenNode *) args[1])->token));
return;
}
args[1]->compile(result, program, instructions);
instructions.push_back(new StoreGlobal(reg));
} else if (args[0]->type == SyntaxTreeNode::Expression) {
Expand Down
7 changes: 7 additions & 0 deletions src/bytecode/compiler/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@ namespace Bytecode {
return globals_table[name];
return -1;
}
void Program::declare_constant(const std::string &name, StackObject object) {
constants_table[name] = object;
}
StackObject *Program::find_constant(const std::string &name) {
if (constants_table.find(name) == constants_table.end()) return nullptr;
return &constants_table[name];
}
}// namespace Bytecode
3 changes: 3 additions & 0 deletions src/bytecode/compiler/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Bytecode {
class Program {
std::unordered_map<std::string, size_t> globals_table;
std::unordered_map<std::string, size_t> segments_table;
std::unordered_map<std::string, StackObject> constants_table;

public:
std::vector<Segment *> segments;
Expand All @@ -23,7 +24,9 @@ namespace Bytecode {
size_t declare_global(const std::string &);
size_t declare_function(const std::string &, Segment *);
size_t declare_lambda(Segment *);
void declare_constant(const std::string &, StackObject);
size_t find_global(const std::string &);
size_t find_function(const std::string &);
StackObject *find_constant(const std::string &);
};
}// namespace Bytecode
20 changes: 20 additions & 0 deletions src/bytecode/objects/StackObject.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "lexer/Token.h"
#include <cstdint>
#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -42,6 +43,25 @@ namespace Bytecode {
// TODO: This is a dumb workaround, fix it later
data.string = strdup(str.c_str());
};
explicit StackObject(const Token &token) {
switch (token.type) {
case Token::Number:
type = Number;
data = {token.asNumber()};
break;
case Token::Boolean:
type = Boolean;
data = {.boolean = token.token == "#true"};
break;
case Token::String:
type = String;
data = {.string = strdup(token.token.c_str())};
break;
default:
type = None;
break;
}
}

[[nodiscard]] std::string asString() const {
return data.string;
Expand Down
8 changes: 5 additions & 3 deletions src/parser/SyntaxTreeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ void TokenNode::compile(
new Bytecode::LoadLiteral(token.asString()));
break;
case Token::Symbol:
if (segment->find_variable(token.token) != -1) {
if (program.find_constant(token.token) != nullptr) {
instructions.push_back(new Bytecode::LoadLiteral(
program.find_constant(token.token)));
} else if (segment->find_variable(token.token) != -1) {
instructions.push_back(new Bytecode::LoadLocal(
segment->find_variable(token.asString())));
} else if (program.find_global(token.token) != -1) {
Expand Down Expand Up @@ -78,8 +81,7 @@ void Expression::compile(
{"read", new Bytecode::BuiltinFunctions::Read},
{"not", new Bytecode::BuiltinFunctions::Not},
{"or", new Bytecode::BuiltinFunctions::Or},
{"and", new Bytecode::BuiltinFunctions::And}
};
{"and", new Bytecode::BuiltinFunctions::And}};

if (program.find_global(function.token) != -1) {
for (const auto &argument: args)
Expand Down

0 comments on commit c8aef44

Please sign in to comment.