You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.6 KiB

#pragma once
#include "SysYParser.h"
#include "sem/SymbolTable.h"
#include <string>
#include <unordered_map>
#include <vector>
struct GlobalSemanticInfo {
SemanticType type = SemanticType::Int;
bool is_const = false;
bool is_array = false;
std::vector<int> dims;
};
struct FunctionSemanticInfo {
SemanticType return_type = SemanticType::Void;
std::vector<bool> param_is_array;
bool is_builtin = false;
bool is_defined = false;
bool reads_global_memory = false;
bool writes_global_memory = false;
bool reads_param_memory = false;
bool writes_param_memory = false;
bool has_io = false;
bool has_unknown_effects = true;
bool is_recursive = false;
std::vector<std::string> direct_callees;
bool MayReadMemory() const {
return has_unknown_effects || reads_global_memory || writes_global_memory ||
reads_param_memory || writes_param_memory;
}
bool MayWriteMemory() const {
return has_unknown_effects || writes_global_memory || writes_param_memory;
}
bool HasObservableSideEffects() const {
return has_unknown_effects || writes_global_memory || writes_param_memory ||
has_io;
}
bool CanDiscardUnusedCall() const {
return !has_unknown_effects && !writes_global_memory &&
!writes_param_memory && !has_io && !is_recursive;
}
};
class SemanticContext {
public:
FunctionSemanticInfo* LookupFunction(const std::string& name) {
auto it = functions_.find(name);
return it == functions_.end() ? nullptr : &it->second;
}
const FunctionSemanticInfo* LookupFunction(const std::string& name) const {
auto it = functions_.find(name);
return it == functions_.end() ? nullptr : &it->second;
}
GlobalSemanticInfo* LookupGlobal(const std::string& name) {
auto it = globals_.find(name);
return it == globals_.end() ? nullptr : &it->second;
}
const GlobalSemanticInfo* LookupGlobal(const std::string& name) const {
auto it = globals_.find(name);
return it == globals_.end() ? nullptr : &it->second;
}
FunctionSemanticInfo& UpsertFunction(const std::string& name) {
return functions_[name];
}
GlobalSemanticInfo& UpsertGlobal(const std::string& name) {
return globals_[name];
}
const std::unordered_map<std::string, FunctionSemanticInfo>& GetFunctions() const {
return functions_;
}
const std::unordered_map<std::string, GlobalSemanticInfo>& GetGlobals() const {
return globals_;
}
private:
std::unordered_map<std::string, FunctionSemanticInfo> functions_;
std::unordered_map<std::string, GlobalSemanticInfo> globals_;
};
SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit);