make "switch" internals accessible to ZAM; tidying of same

This commit is contained in:
Vern Paxson 2021-05-30 18:13:17 -07:00
parent c662f8fbe8
commit c81a331944
2 changed files with 20 additions and 9 deletions

View file

@ -710,7 +710,7 @@ void SwitchStmt::Init()
t->Append(e->GetType()); t->Append(e->GetType());
comp_hash = new CompositeHash(std::move(t)); comp_hash = new CompositeHash(std::move(t));
case_label_value_map.SetDeleteFunc(int_del_func); case_label_hash_map.SetDeleteFunc(int_del_func);
} }
SwitchStmt::SwitchStmt(ExprPtr index, case_list* arg_cases) SwitchStmt::SwitchStmt(ExprPtr index, case_list* arg_cases)
@ -855,12 +855,13 @@ bool SwitchStmt::AddCaseLabelValueMapping(const Val* v, int idx)
type_name(e->GetType()->Tag())); type_name(e->GetType()->Tag()));
} }
int* label_idx = case_label_value_map.Lookup(hk.get()); int* label_idx = case_label_hash_map.Lookup(hk.get());
if ( label_idx ) if ( label_idx )
return false; return false;
case_label_value_map.Insert(hk.get(), new int(idx)); case_label_value_map[v] = idx;
case_label_hash_map.Insert(hk.get(), new int(idx));
return true; return true;
} }
@ -884,7 +885,7 @@ std::pair<int, ID*> SwitchStmt::FindCaseLabelMatch(const Val* v) const
ID* label_id = nullptr; ID* label_id = nullptr;
// Find matching expression cases. // Find matching expression cases.
if ( case_label_value_map.Length() ) if ( case_label_hash_map.Length() )
{ {
auto hk = comp_hash->MakeHashKey(*v, true); auto hk = comp_hash->MakeHashKey(*v, true);
@ -897,7 +898,7 @@ std::pair<int, ID*> SwitchStmt::FindCaseLabelMatch(const Val* v) const
return std::make_pair(-1, nullptr); return std::make_pair(-1, nullptr);
} }
if ( auto i = case_label_value_map.Lookup(hk.get()) ) if ( auto i = case_label_hash_map.Lookup(hk.get()) )
label_idx = *i; label_idx = *i;
} }

View file

@ -183,15 +183,24 @@ public:
bool NoFlowAfter(bool ignore_break) const override; bool NoFlowAfter(bool ignore_break) const override;
protected: protected:
friend class ZAMCompiler;
int DefaultCaseIndex() const { return default_case_idx; }
const auto& ValueMap() const { return case_label_value_map; }
const std::vector<std::pair<ID*, int>>* TypeMap() const
{ return &case_label_type_list; }
const CompositeHash* CompHash() const { return comp_hash; }
ValPtr DoExec(Frame* f, Val* v, StmtFlowType& flow) override; ValPtr DoExec(Frame* f, Val* v, StmtFlowType& flow) override;
bool IsPure() const override; bool IsPure() const override;
// Initialize composite hash and case label map. // Initialize composite hash and case label map.
void Init(); void Init();
// Adds an entry in case_label_value_map for the given value to associate it // Adds entries in case_label_value_map and case_label_hash_map
// with the given index in the cases list. If the entry already exists, // for the given value to associate it with the given index in
// returns false, else returns true. // the cases list. If the entry already exists, returns false,
// else returns true.
bool AddCaseLabelValueMapping(const Val* v, int idx); bool AddCaseLabelValueMapping(const Val* v, int idx);
// Adds an entry in case_label_type_map for the given type (w/ ID) to // Adds an entry in case_label_type_map for the given type (w/ ID) to
@ -208,7 +217,8 @@ protected:
case_list* cases; case_list* cases;
int default_case_idx; int default_case_idx;
CompositeHash* comp_hash; CompositeHash* comp_hash;
PDict<int> case_label_value_map; std::unordered_map<const Val*, int> case_label_value_map;
PDict<int> case_label_hash_map;
std::vector<std::pair<ID*, int>> case_label_type_list; std::vector<std::pair<ID*, int>> case_label_type_list;
}; };