xo-expression: refactor: use GlobalEnv for MachPipeline::global_env

This commit is contained in:
Roland Conybeare 2024-06-30 19:33:18 -04:00
commit 28884e1f4f
3 changed files with 23 additions and 18 deletions

View file

@ -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<Variable> lookup_var(const std::string & vname) const = 0;
virtual ref::brw<Expression> lookup_var(const std::string & vname) const = 0;
};
} /*namespace ast*/
} /*namespace xo*/

View file

@ -12,32 +12,36 @@ namespace xo {
namespace ast {
class GlobalEnv : public Environment {
public:
ref::brw<Variable> require_global(ref::brw<Variable> var) {
const std::string & vname = var->name();
/** create instance. Probably only need one of these **/
static ref::rp<GlobalEnv> 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<Expression> require_global(const std::string & vname,
ref::brw<Expression> expr) {
global_map_[vname] = expr.get();
return expr;
} /*require_global*/
// ----- Environment -----
virtual ref::brw<Variable> lookup_var(const std::string & vname) const {
auto ix = var_map_.find(vname);
virtual ref::brw<Expression> lookup_var(const std::string & vname) const {
auto ix = global_map_.find(vname);
if (ix == var_map_.end())
return ref::brw<Variable>::from_native(nullptr);
if (ix == global_map_.end()) {
/* not found */
return ref::brw<Expression>::from_native(nullptr);
}
return ix->second;
}
private:
std::map<std::string, ref::rp<Variable>> var_map_;
GlobalEnv() = default;
private:
/* for assignable globals, need to allocate memory
* addresses for these.
*/
std::map<std::string, ref::rp<Expression>> global_map_;
};
} /*namespace ast*/
} /*namespace xo*/

View file

@ -38,7 +38,7 @@ namespace xo {
// ----- Environment -----
virtual ref::brw<Variable> lookup_var(const std::string & target) const override {
virtual ref::brw<Expression> lookup_var(const std::string & target) const override {
for (const auto & arg : argv_) {
if (arg->name() == target)
return arg;