Skip to content

Commit

Permalink
Remove Decl::s.
Browse files Browse the repository at this point in the history
This was a temporary hack to make symbol refactoring possible. We can
remove it now. Every Decl that creates a symbol can cache it privately.

Unfortunately, SymbolExpr makes recovering sym() fairly difficult. For
now make it a virtual function, however, SymbolExpr will likely go away
when we unify semantic pipeline.
  • Loading branch information
dvander committed Nov 14, 2023
1 parent ac84139 commit b9f56af
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 167 deletions.
51 changes: 27 additions & 24 deletions compiler/array-helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,7 @@ ArraySizeResolver::ResolveDimExprs()
return true;
}

bool
ArraySizeResolver::ResolveDimExpr(Expr* expr, value* v)
{
bool ArraySizeResolver::ResolveDimExpr(Expr* expr, value* v) {
auto& sc = *sema_->context();
if (!expr->Bind(sc))
return false;
Expand All @@ -340,12 +338,13 @@ ArraySizeResolver::ResolveDimExpr(Expr* expr, value* v)
// int blah[X];
//
// For backward compatibility with a huge number of plugins.
auto sym = sym_expr->sym();
auto type = types_->find(sym->tag);
if (sym->decl->as<EnumDecl>() && !type->asEnumStruct() && sym->ident == iCONSTEXPR) {
*v = {};
v->set_constval(sym->addr());
return true;
auto decl = sym_expr->decl();
if (auto ed = decl->as<EnumDecl>()) {
if (ed->sym()) {
*v = {};
v->set_constval(ed->array_size());
return true;
}
}
}

Expand Down Expand Up @@ -491,16 +490,22 @@ CalcArraySize(symbol* sym)
return size;
}

bool
FixedArrayValidator::CheckArgument(Expr* init)
{
bool FixedArrayValidator::CheckArgument(Expr* init) {
// As a special exception, array arguments can be initialized with a global
// reference.
SymbolExpr* expr = init->as<SymbolExpr>();
if (!expr)
return ValidateRank(0, init);

symbol* sym = expr->sym();
Decl* decl = expr->decl();
if (!decl)
return false;

VarDecl* var = decl->as<VarDecl>();
if (!var)
return false;

symbol* sym = var->sym();
if (!sym) {
// This failed to bind, and we're still in the binding phase, so just
// return false.
Expand Down Expand Up @@ -700,12 +705,12 @@ FixedArrayValidator::ValidateEnumStruct(Expr* init)
return false;
}

symbol* field = (*field_iter)->s;
auto field = (*field_iter);

// Advance early so we can use |continue|.
field_iter++;

typeinfo_t type = TypeInfoFromSymbol(field);
const auto& type = field->type();
if (type.ident == iARRAY) {
if (!CheckArrayInitialization(sema_, type, expr))
continue;
Expand All @@ -721,7 +726,7 @@ FixedArrayValidator::ValidateEnumStruct(Expr* init)
continue;
}

matchtag(field->tag, v.tag, MATCHTAG_COERCE | MATCHTAG_ENUM_ASSN);
matchtag(type.tag(), v.tag, MATCHTAG_COERCE | MATCHTAG_ENUM_ASSN);
}
}

Expand Down Expand Up @@ -892,7 +897,7 @@ class ArrayEmitter final
cell Emit(int rank, Expr* expr);

size_t AddString(StringExpr* expr);
void AddInlineArray(symbol* field, ArrayExpr* expr);
void AddInlineArray(LayoutFieldDecl* field, ArrayExpr* expr);
void EmitPadding(size_t rank_size, int tag, size_t emitted, bool ellipses,
const ke::Maybe<cell> prev1, const ke::Maybe<cell> prev2);

Expand Down Expand Up @@ -971,15 +976,15 @@ ArrayEmitter::Emit(int rank, Expr* init)

size_t emitted = AddString(expr);

symbol* field = (*field_iter)->s;
auto field = (*field_iter);
assert(field);

EmitPadding(field->dim(0), field->tag, emitted, false, {}, {});
EmitPadding(field->type().dim[0], field->type().tag(), emitted, false, {}, {});
} else if (ArrayExpr* expr = item->as<ArrayExpr>()) {
// Subarrays can only appear in an enum struct. Normal 2D cases
// would flow through the check at the start of this function.
assert(es_);
symbol* field = (*field_iter)->s;
auto field = (*field_iter);
AddInlineArray(field, expr);
} else {
assert(item->val().ident == iCONSTEXPR);
Expand Down Expand Up @@ -1007,9 +1012,7 @@ ArrayEmitter::Emit(int rank, Expr* init)
return (start * sizeof(cell)) | kDataFlag;
}

void
ArrayEmitter::AddInlineArray(symbol* field, ArrayExpr* array)
{
void ArrayEmitter::AddInlineArray(LayoutFieldDecl* field, ArrayExpr* array) {
ke::Maybe<cell> prev1, prev2;

for (const auto& item : array->exprs()) {
Expand All @@ -1019,7 +1022,7 @@ ArrayEmitter::AddInlineArray(symbol* field, ArrayExpr* array)
prev1 = ke::Some(item->val().constval());
}

EmitPadding(field->dim(0), field->tag, array->exprs().size(),
EmitPadding(field->type().dim[0], field->type().tag(), array->exprs().size(),
array->ellipses(), prev1, prev2);
}

Expand Down
33 changes: 15 additions & 18 deletions compiler/assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class RttiBuilder
void encode_signature_into(std::vector<uint8_t>& bytes, functag_t* ft);
void encode_enum_into(std::vector<uint8_t>& bytes, Type* type);
void encode_tag_into(std::vector<uint8_t>& bytes, int tag);
void encode_ret_array_into(std::vector<uint8_t>& bytes, symbol* sym);
void encode_ret_array_into(std::vector<uint8_t>& bytes, const typeinfo_t& type);
void encode_funcenum_into(std::vector<uint8_t>& bytes, Type* type, funcenum_t* fe);
void encode_var_type(std::vector<uint8_t>& bytes, const variable_type_t& info);
void encode_struct_into(std::vector<uint8_t>& bytes, Type* type);
Expand Down Expand Up @@ -447,7 +447,7 @@ RttiBuilder::add_enumstruct(Type* type)
smx_rtti_enumstruct es = {};
es.name = names_->add(*cc_.atoms(), type->name());
es.first_field = es_fields_->count();
es.size = es_decl->s->addr();
es.size = es_decl->sym()->addr();
enumstructs_->add(es);

// Pre-allocate storage in case of nested types.
Expand All @@ -458,21 +458,20 @@ RttiBuilder::add_enumstruct(Type* type)
// Add all fields.
size_t index = 0;
for (auto iter = enumlist.begin(); iter != enumlist.end(); iter++) {
auto field_decl = (*iter);
auto field = field_decl->s;
auto field = (*iter);

int dims[1], dimcount = 0;
if (field->dim_count())
dims[dimcount++] = field->dim(0);
if (field->type().numdim())
dims[dimcount++] = field->type().dim[0];

variable_type_t type = {field->semantic_tag, dims, dimcount, false};
variable_type_t type = {field->type().semantic_tag(), dims, dimcount, false};
std::vector<uint8_t> encoding;
encode_var_type(encoding, type);

smx_rtti_es_field info;
info.name = names_->add(field_decl->name());
info.name = names_->add(field->name());
info.type_id = to_typeid(encoding);
info.offset = field->addr();
info.offset = field->offset();
es_fields_->at(es.first_field + index) = info;
index++;
}
Expand Down Expand Up @@ -539,7 +538,7 @@ RttiBuilder::to_typeid(const std::vector<uint8_t>& bytes)

uint32_t RttiBuilder::encode_signature(FunctionDecl* fun) {
assert(fun == fun->canonical());
auto sym = fun->s;
auto sym = fun->sym();

std::vector<uint8_t> bytes;

Expand All @@ -552,8 +551,8 @@ uint32_t RttiBuilder::encode_signature(FunctionDecl* fun) {
bytes.push_back(cb::kVariadic);

VarDecl* child = fun->return_array() ? fun->return_array()->var : nullptr;
if (child && child->s->dim_count()) {
encode_ret_array_into(bytes, child->s);
if (child && child->type().numdim()) {
encode_ret_array_into(bytes, child->type());
} else if (sym->tag == types_->tag_void()) {
bytes.push_back(cb::kVoid);
} else {
Expand Down Expand Up @@ -668,14 +667,12 @@ RttiBuilder::encode_enumstruct_into(std::vector<uint8_t>& bytes, Type* type)
CompactEncodeUint32(bytes, add_enumstruct(type));
}

void
RttiBuilder::encode_ret_array_into(std::vector<uint8_t>& bytes, symbol* sym)
{
for (int i = 0; i < sym->dim_count(); i++) {
void RttiBuilder::encode_ret_array_into(std::vector<uint8_t>& bytes, const typeinfo_t& type) {
for (int i = 0; i < type.numdim(); i++) {
bytes.push_back(cb::kFixedArray);
CompactEncodeUint32(bytes, sym->dim(i));
CompactEncodeUint32(bytes, type.dim[i]);
}
encode_tag_into(bytes, sym->tag);
encode_tag_into(bytes, type.tag());
}

uint8_t
Expand Down
38 changes: 26 additions & 12 deletions compiler/code-generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ CodeGenerator::EmitPstruct(VarDeclBase* decl)
} else if (auto expr = field->value->as<TaggedValueExpr>()) {
values[arg->index] = expr->value();
} else if (auto expr = field->value->as<SymbolExpr>()) {
values[arg->index] = expr->sym()->addr();
values[arg->index] = expr->decl()->sym()->addr();
} else {
assert(false);
}
Expand Down Expand Up @@ -516,8 +516,8 @@ CodeGenerator::EmitExpr(Expr* expr)
}
case ExprKind::ThisExpr: {
auto e = expr->to<ThisExpr>();
if (e->sym()->ident == iREFARRAY)
__ address(e->sym(), sPRI);
if (e->decl()->sym()->ident == iREFARRAY)
__ address(e->decl()->sym(), sPRI);
break;
}
case ExprKind::StringExpr: {
Expand Down Expand Up @@ -1075,7 +1075,7 @@ CodeGenerator::EmitTernaryExpr(TernaryExpr* expr)
void
CodeGenerator::EmitSymbolExpr(SymbolExpr* expr)
{
symbol* sym = expr->sym();
symbol* sym = expr->decl()->sym();
switch (sym->ident) {
case iARRAY:
case iREFARRAY:
Expand Down Expand Up @@ -1161,9 +1161,15 @@ CodeGenerator::EmitFieldAccessExpr(FieldAccessExpr* expr)
// reserved for RvalueExpr().
EmitExpr(expr->base());

if (expr->resolved() && expr->resolved()->s->addr()) {
__ const_alt(expr->resolved()->s->addr() << 2);
__ emit(OP_ADD);
// Only enum struct accesses have a resolved decl.
if (!expr->resolved())
return;

if (LayoutFieldDecl* field = expr->resolved()->as<LayoutFieldDecl>()) {
if (field->offset()) {
__ const_alt(field->offset() << 2);
__ emit(OP_ADD);
}
}
}

Expand Down Expand Up @@ -1311,7 +1317,7 @@ CodeGenerator::EmitNewArrayExpr(NewArrayExpr* expr)
// that when synthesizing a NewArrayExpr for old-style declarations,
// it is impossible to have an enum struct.
// :TODO: test this
__ emit(OP_PUSH_C, es->s->addr());
__ emit(OP_PUSH_C, es->array_size());
numdim++;
}

Expand Down Expand Up @@ -1351,7 +1357,7 @@ CodeGenerator::EmitReturnArrayStmt(ReturnStmt* stmt)
auto info = fun_->return_array();
if (array.iv.empty()) {
VarDecl* sub_decl = info->var;
symbol* sub = sub_decl->s;
symbol* sub = sub_decl->sym();

// A much simpler copy can be emitted.
__ load_hidden_arg(fun_, sub, true);
Expand Down Expand Up @@ -1391,7 +1397,7 @@ CodeGenerator::EmitReturnArrayStmt(ReturnStmt* stmt)
// add.c <iv-size * 4> ; address to data
// memcopy <data-size>
__ emit(OP_PUSH_PRI);
__ load_hidden_arg(fun_, info->var->s, false);
__ load_hidden_arg(fun_, info->var->sym(), false);
__ emit(OP_INITARRAY_ALT, dat_addr, iv_size, 0, 0, 0);
__ emit(OP_MOVE_PRI);
__ emit(OP_ADD_C, iv_size * sizeof(cell));
Expand Down Expand Up @@ -2189,9 +2195,17 @@ int CodeGenerator::DynamicMemorySize() const {
}

void CodeGenerator::EnqueueDebugSymbol(Decl* decl, uint32_t pc) {
if (decl->s->vclass == sGLOBAL) {
int vclass = 0;
if (auto fun = decl->as<FunctionDecl>())
vclass = fun->is_static() ? sSTATIC : sGLOBAL;
else if (auto var = decl->as<VarDeclBase>())
vclass = var->vclass();
else
assert(false);

if (vclass == sGLOBAL) {
global_syms_.emplace_back(decl, pc);
} else if (decl->s->vclass == sSTATIC && !func_) {
} else if (vclass == sSTATIC && !func_) {
static_syms_.back().second.emplace_back(decl, pc);
} else {
local_syms_.back().emplace_back(decl, pc);
Expand Down
12 changes: 6 additions & 6 deletions compiler/expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ find_userop(SemaContext& sc, int oper, int tag1, int tag2, int numparam, const v
if (!chain)
return false;

symbol* sym = nullptr;
FunctionDecl* decl = nullptr;
bool swapparams;
bool is_commutative = commutative(oper);
for (auto iter = chain; iter; iter = iter->next) {
Expand All @@ -166,13 +166,13 @@ find_userop(SemaContext& sc, int oper, int tag1, int tag2, int numparam, const v
swapped = true;
}
if (matched) {
sym = iter->s;
decl = fun;
swapparams = swapped;
break;
}
}

if (!sym)
if (!decl)
return false;

/* we don't want to use the redefined operator in the function that
Expand All @@ -181,13 +181,13 @@ find_userop(SemaContext& sc, int oper, int tag1, int tag2, int numparam, const v
* fixed:operator+(fixed:a, fixed:b)
* return a + b
*/
if (sym == sc.func()) {
if (decl == sc.func_node()) {
report(408);
}

markusage(sym, uREAD);
markusage(decl, uREAD);

op->sym = sym;
op->sym = decl->sym();
op->oper = oper;
op->paramspassed = (oper == 0) ? 1 : numparam;
op->savepri = savepri;
Expand Down
1 change: 1 addition & 0 deletions compiler/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,5 @@ static const char* errmsg_ex[] = {
/*442*/ "method aliases are no longer supported\n",
/*443*/ "enum %s already has a methodmap\n",
/*444*/ "cannot find method \"%s.%s\"\n",
/*445*/ "%s must be a field\n",
};
Loading

0 comments on commit b9f56af

Please sign in to comment.