Skip to content

Commit

Permalink
Make SymbolTable type-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Sep 30, 2024
1 parent c1090da commit 6db6cd2
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 56 deletions.
13 changes: 5 additions & 8 deletions driver/function_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ bool FunctionStore::hasFunction(const StrScanner &_name) const {
return lookupFunction(_name) != nullptr;
}

const void *FunctionStore::lookupFunction(const StrScanner &name) const {
const Functor *FunctionStore::lookupFunction(const StrScanner &name) const {
auto it = _functions.find(std::string(name.str(), name.size()));
if (it == _functions.end())
return nullptr;
const Function *fn = &it->second;
return reinterpret_cast<const void *>(fn);
return it == _functions.end() ? nullptr : &it->second;
}

Error FunctionStore::internFunction(const StrScanner &name_, const Parameters &params,
Expand All @@ -76,14 +73,14 @@ bool FunctionStore::Binding::hasSymbol(const StrScanner &symbol) const {
parent->hasSymbol(symbol);
}

SymbolTable::symval_t FunctionStore::Binding::lookupSymbol(const StrScanner &symbol) const {
const Value *FunctionStore::Binding::lookupSymbol(const StrScanner &symbol) const {
const auto it = paramsAt.find(std::string(symbol.str(), symbol.size()));
if (it == paramsAt.end())
return parent->lookupSymbol(symbol);
return stack.at(it->second).getSigned();
return &stack.at(it->second);
}

const void *FunctionStore::Binding::lookupFunction(const StrScanner &symbol) const {
const Functor *FunctionStore::Binding::lookupFunction(const StrScanner &symbol) const {
return parent->lookupFunction(symbol);
}

Expand Down
6 changes: 3 additions & 3 deletions driver/function_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct FunctionStore final {
void clear();

bool hasFunction(const StrScanner &name) const;
const void *lookupFunction(const StrScanner &symbol) const;
const Functor *lookupFunction(const StrScanner &symbol) const;

using Parameters = std::list<StrScanner>;
Error internFunction(const StrScanner &name, const Parameters &params, const StrScanner &body,
Expand Down Expand Up @@ -71,8 +71,8 @@ struct FunctionStore final {
Binding(const ParametersAt &paramsAt_, const ValueStack &stack_, const SymbolTable *parent_)
: paramsAt(paramsAt_), stack(stack_), parent(parent_) {}
bool hasSymbol(const StrScanner &symbol) const override;
symval_t lookupSymbol(const StrScanner &symbol) const override;
const void *lookupFunction(const StrScanner &symbol) const override;
const Value *lookupSymbol(const StrScanner &symbol) const override;
const Functor *lookupFunction(const StrScanner &symbol) const override;

private:
const ParametersAt &paramsAt;
Expand Down
17 changes: 6 additions & 11 deletions driver/symbol_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ bool SymbolStoreImpl::hasSymbol(const StrScanner &symbol) const {
return hasValue(symbol, false) || hasValue(symbol, true);
}

SymbolTable::symval_t SymbolStoreImpl::lookupSymbol(const StrScanner &symbol) const {
const Value *SymbolStoreImpl::lookupSymbol(const StrScanner &symbol) const {
const auto key = std::string(symbol.str(), symbol.size());
const auto s = _symbols.find(key);
if (s != _symbols.end())
return s->second;
return &s->second;
const auto v = _variables.find(key);
if (v != _variables.end())
return v->second;
return 0;
return &v->second;
return nullptr;
}

bool SymbolStoreImpl::hasValue(const StrScanner &symbol, bool variable) const {
Expand All @@ -74,20 +74,15 @@ Error SymbolStoreImpl::internSymbol(const Value &value, const StrScanner &symbol

const auto key = std::string(symbol.str(), symbol.size());
auto &map = variable ? _variables : _symbols;
auto it = map.find(key);
if (it == map.end()) {
map.emplace(key, value.getInteger());
} else {
it->second = value.getInteger();
}
map[key] = value;
return OK;
}

bool SymbolStoreImpl::hasFunction(const StrScanner &name) const {
return _functions.hasFunction(name);
}

const void *SymbolStoreImpl::lookupFunction(const StrScanner &name) const {
const Functor *SymbolStoreImpl::lookupFunction(const StrScanner &name) const {
return _functions.lookupFunction(name);
}

Expand Down
10 changes: 5 additions & 5 deletions driver/symbol_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ struct SymbolStoreImpl final : SymbolStore {
void clearFunctions();

// SymbolTable
const char *lookupValue(symval_t) const override { return nullptr; }
const char *lookupValue(const Value &) const override { return nullptr; }
bool hasSymbol(const StrScanner &name) const override;
symval_t lookupSymbol(const StrScanner &symbol) const override;
const void *lookupFunction(const StrScanner &name) const override;
const Value *lookupSymbol(const StrScanner &symbol) const override;
const Functor *lookupFunction(const StrScanner &name) const override;

bool hasValue(const StrScanner &symbol, bool variable) const override;
Error internSymbol(
Expand All @@ -62,8 +62,8 @@ struct SymbolStoreImpl final : SymbolStore {
const StrScanner &body, const ValueParser &parser) override;

private:
std::map<std::string, symval_t, std::less<>> _symbols;
std::map<std::string, symval_t, std::less<>> _variables;
std::map<std::string, Value> _symbols;
std::map<std::string, Value> _variables;
FunctionStore _functions;
};

Expand Down
10 changes: 7 additions & 3 deletions src/dis_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,13 @@ Error Disassembler::decode(
const char *Disassembler::lookup(uint32_t addr, uint8_t addrWidth) const {
const char *symbol = nullptr;
if (_symtab) {
symbol = _symtab->lookupValue(addr);
if (!symbol)
symbol = _symtab->lookupValue(config().signExtend(addr, addrWidth));
Value val;
val.setUnsigned(addr);
symbol = _symtab->lookupValue(val);
if (!symbol) {
val.setSigned(config().signExtend(addr, addrWidth));
symbol = _symtab->lookupValue(val);
}
}
return symbol;
}
Expand Down
14 changes: 7 additions & 7 deletions src/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
#define __LIBASM_SYMBOL_TABLE_H__

#include <stdint.h>
#include "str_scanner.h"
#include "value.h"

namespace libasm {

struct SymbolTable {
using symval_t = int32_t;
struct Functor;
struct StrScanner;
struct Value;

virtual const char *lookupValue(symval_t) const { return nullptr; }
struct SymbolTable {
virtual const char *lookupValue(const Value &) const { return nullptr; }
virtual bool hasSymbol(const StrScanner &symbol) const = 0;
virtual symval_t lookupSymbol(const StrScanner &symbol) const = 0;
virtual const void *lookupFunction(const StrScanner &symbol) const = 0;
virtual const Value *lookupSymbol(const StrScanner &symbol) const = 0;
virtual const Functor *lookupFunction(const StrScanner &symbol) const = 0;
};

} // namespace libasm
Expand Down
10 changes: 2 additions & 8 deletions src/value_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Value ValueParser::_eval(
if (readFunctionName(scan, symbol) == OK) {
auto fn = _function.lookupFunction(symbol);
if (fn == nullptr && symtab)
fn = reinterpret_cast<const Functor *>(symtab->lookupFunction(symbol));
fn = symtab->lookupFunction(symbol);
if (fn) {
const auto open = scan.skipSpaces();
if (!_operators.isOpenExpr(scan)) {
Expand All @@ -136,13 +136,7 @@ Value ValueParser::_eval(
return Value();
}
if (symtab && symtab->hasSymbol(symbol)) {
Value value;
const auto v = symtab->lookupSymbol(symbol);
if (v < 0) {
value.setSigned(v);
} else {
value.setUnsigned(v);
}
const auto value = *symtab->lookupSymbol(symbol);
vstack.push(value);
} else {
error.setErrorIf(at, UNDEFINED_SYMBOL);
Expand Down
30 changes: 19 additions & 11 deletions test/test_symtab.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
#include <map>
#include <string>

#include "operators.h"
#include "symbol_table.h"
#include "value.h"

namespace libasm {
namespace test {

struct TestSymtab final : SymbolTable {
const char *lookupValue(symval_t val) const override {
const char *lookupValue(const Value &val) const override {
auto it = _values.find(val);
return it == _values.end() ? nullptr : it->second.c_str();
}
Expand All @@ -38,24 +40,30 @@ struct TestSymtab final : SymbolTable {
return _symbols.find(key) != _symbols.end();
}

symval_t lookupSymbol(const StrScanner &symbol) const override {
const Value *lookupSymbol(const StrScanner &symbol) const override {
const std::string key(symbol.str(), symbol.size());
auto it = _symbols.find(key);
return it == _symbols.end() ? 0 : it->second;
return it == _symbols.end() ? nullptr : &it->second;
}

const void *lookupFunction(const StrScanner &symbol) const override {
const Functor *lookupFunction(const StrScanner &symbol) const override {
const std::string key(symbol.str(), symbol.size());
auto it = _functions.find(key);
return it == _functions.end() ? nullptr : it->second;
}

void intern(int32_t s, const std::string &key) {
_symbols[key] = s;
_values[s] = key;
void intern(int32_t val, const std::string &key) {
Value v;
v.setSigned(val);
intern(v, key);
}

void internFunction(const void *value, const std::string &key) { _functions[key] = value; }
void intern(const Value &val, const std::string &key) {
_symbols[key] = val;
_values[val] = key;
}

void internFunction(const Functor *fn, const std::string &key) { _functions[key] = fn; }

void reset() {
_symbols.clear();
Expand All @@ -64,9 +72,9 @@ struct TestSymtab final : SymbolTable {
}

private:
std::map<std::string, symval_t, std::less<>> _symbols;
std::map<symval_t, std::string> _values;
std::map<std::string, const void *, std::less<>> _functions;
std::map<std::string, Value> _symbols;
std::map<const Value, std::string> _values;
std::map<std::string, const Functor *> _functions;
};

} // namespace test
Expand Down

0 comments on commit 6db6cd2

Please sign in to comment.