From 28884e1f4f97ed8cdde0c7ebcc1f20698c923963 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 30 Jun 2024 19:33:18 -0400 Subject: [PATCH] xo-expression: refactor: use GlobalEnv for MachPipeline::global_env --- include/xo/expression/Environment.hpp | 5 ++-- include/xo/expression/GlobalEnv.hpp | 34 +++++++++++++++------------ include/xo/expression/LocalEnv.hpp | 2 +- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/include/xo/expression/Environment.hpp b/include/xo/expression/Environment.hpp index 834366c7..6fec9b32 100644 --- a/include/xo/expression/Environment.hpp +++ b/include/xo/expression/Environment.hpp @@ -12,9 +12,10 @@ namespace xo { namespace ast { class Environment : public ref::Refcount { public: - /** lookup variable-expression @p vname in this environment + /** lookup variable-expression @p vname in this environment. + * returns llvm::Value representing code that produces a value for vname **/ - virtual ref::brw lookup_var(const std::string & vname) const = 0; + virtual ref::brw lookup_var(const std::string & vname) const = 0; }; } /*namespace ast*/ } /*namespace xo*/ diff --git a/include/xo/expression/GlobalEnv.hpp b/include/xo/expression/GlobalEnv.hpp index 223fc9df..fbc9ef9c 100644 --- a/include/xo/expression/GlobalEnv.hpp +++ b/include/xo/expression/GlobalEnv.hpp @@ -12,32 +12,36 @@ namespace xo { namespace ast { class GlobalEnv : public Environment { public: - ref::brw require_global(ref::brw var) { - const std::string & vname = var->name(); + /** create instance. Probably only need one of these **/ + static ref::rp make() { return new GlobalEnv(); } - auto ix = var_map_.find(vname); - - if (ix == var_map_.end()) { - var_map_[vname] = var.get(); - return var; - } else { - return ix->second; - } + ref::brw require_global(const std::string & vname, + ref::brw expr) { + global_map_[vname] = expr.get(); + return expr; } /*require_global*/ // ----- Environment ----- - virtual ref::brw lookup_var(const std::string & vname) const { - auto ix = var_map_.find(vname); + virtual ref::brw lookup_var(const std::string & vname) const { + auto ix = global_map_.find(vname); - if (ix == var_map_.end()) - return ref::brw::from_native(nullptr); + if (ix == global_map_.end()) { + /* not found */ + return ref::brw::from_native(nullptr); + } return ix->second; } private: - std::map> var_map_; + GlobalEnv() = default; + + private: + /* for assignable globals, need to allocate memory + * addresses for these. + */ + std::map> global_map_; }; } /*namespace ast*/ } /*namespace xo*/ diff --git a/include/xo/expression/LocalEnv.hpp b/include/xo/expression/LocalEnv.hpp index 7f5f12dd..b206bddf 100644 --- a/include/xo/expression/LocalEnv.hpp +++ b/include/xo/expression/LocalEnv.hpp @@ -38,7 +38,7 @@ namespace xo { // ----- Environment ----- - virtual ref::brw lookup_var(const std::string & target) const override { + virtual ref::brw lookup_var(const std::string & target) const override { for (const auto & arg : argv_) { if (arg->name() == target) return arg;