mirror of
https://github.com/odin-lang/Odin.git
synced 2026-08-15 01:59:15 +00:00
Begin work on semantically type checking asm templates
This commit is contained in:
@@ -1983,10 +1983,515 @@ gb_internal void check_proc_group_decl(CheckerContext *ctx, Entity *pg_entity, D
|
||||
AttributeContext ac = {};
|
||||
check_decl_attributes(ctx, d->attributes, proc_group_attribute, &ac);
|
||||
check_objc_methods(ctx, pg_entity, ac);
|
||||
|
||||
|
||||
}
|
||||
|
||||
gb_internal bool is_valid_asm_parameter_type(Type *type) {
|
||||
if (is_type_integer(type)) {
|
||||
return true;
|
||||
}
|
||||
if (is_type_float(type)) {
|
||||
return true;
|
||||
}
|
||||
if (is_type_boolean(type)) {
|
||||
return true;
|
||||
}
|
||||
if (is_type_pointer(type) || is_type_multi_pointer(type)) {
|
||||
return true;
|
||||
}
|
||||
if (is_type_simd_vector(type)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
gb_internal Type *check_asm_template_signature_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool input_parameters, Array<AsmTemplateEntityDecl> *asm_template_entity_decls) {
|
||||
Type *tuple = alloc_type_tuple();
|
||||
if (_params == nullptr) {
|
||||
return tuple;
|
||||
}
|
||||
ast_node(field_list, FieldList, _params);
|
||||
Slice<Ast *> params = field_list->list;
|
||||
|
||||
Array<Entity *> variables = {};
|
||||
variables.allocator = heap_allocator();
|
||||
|
||||
for (Ast *param : params) {
|
||||
ast_node(field, Field, param);
|
||||
|
||||
bool prev = ctx->allow_polymorphic_types;
|
||||
ctx->allow_polymorphic_types = false;
|
||||
Type *type = check_type(ctx, field->type);
|
||||
ctx->allow_polymorphic_types = prev;
|
||||
|
||||
if (!is_valid_asm_parameter_type(type)) {
|
||||
gbString s = type_to_string(type);
|
||||
error(field->type, "Invalid type for an asm template. It must be an integer, float, boolean, pointer, multi-pointer, or #simd vector, got '%s'", type);
|
||||
gb_string_free(s);
|
||||
continue;
|
||||
}
|
||||
|
||||
for_array(j, field->names) {
|
||||
Ast *name = field->names[j];
|
||||
|
||||
bool is_poly_name = false;
|
||||
|
||||
switch (name->kind) {
|
||||
case Ast_Ident:
|
||||
break;
|
||||
case Ast_PolyType:
|
||||
GB_ASSERT(name->PolyType.specialization == nullptr);
|
||||
is_poly_name = true;
|
||||
name = name->PolyType.type;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ast_node_expect(name, Ast_Ident)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (is_blank_ident(name)) {
|
||||
error(name, "All parameters must have a name in an asm template");
|
||||
continue;
|
||||
}
|
||||
Token name_token = name->Ident.token;
|
||||
|
||||
Entity *entity = alloc_entity_param(scope, name_token, type, false, /*is_value*/true);
|
||||
entity->flags |= EntityFlag_Used;
|
||||
if (is_poly_name) {
|
||||
entity->flags |= EntityFlag_PolyConst;
|
||||
if (is_type_internally_pointer_like(type)) {
|
||||
error(name, "Parameters with a pointer-like type cannot be used as $ immediates");
|
||||
}
|
||||
}
|
||||
|
||||
Entity *found = scope_insert(scope, entity);
|
||||
if (found == nullptr) {
|
||||
array_add(&variables, entity);
|
||||
|
||||
AsmTemplateEntityDecl ed = {};
|
||||
ed.entity = entity;
|
||||
ed.kind = AsmTemplateEntityDecl_Register;
|
||||
if (is_type_internally_pointer_like(type)) {
|
||||
ed.kind = AsmTemplateEntityDecl_Memory;
|
||||
}
|
||||
if (is_poly_name) {
|
||||
ed.kind = AsmTemplateEntityDecl_Immediate;
|
||||
}
|
||||
if (input_parameters) {
|
||||
ed.param_group = AsmTemplateEntityDeclParamGroup_Input;
|
||||
} else {
|
||||
ed.param_group = AsmTemplateEntityDeclParamGroup_Output;
|
||||
}
|
||||
|
||||
array_add(asm_template_entity_decls, ed);
|
||||
} else {
|
||||
TokenPos pos = found->token.pos;
|
||||
error(name_token,
|
||||
"Redeclaration of '%.*s' in this scope\n"
|
||||
"\tat %s",
|
||||
LIT(name_token.string), token_pos_to_string(pos));
|
||||
entity = found;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tuple->Tuple.variables = slice_from_array(variables);
|
||||
|
||||
return tuple;
|
||||
}
|
||||
|
||||
gb_internal AsmTemplateEntityDeclParamGroup check_asm_find_group(Entity *entity, Array<AsmTemplateEntityDecl> const &asm_template_entity_decls) {
|
||||
for (auto const &ed : asm_template_entity_decls) {
|
||||
if (ed.entity == entity) {
|
||||
return ed.param_group;
|
||||
}
|
||||
}
|
||||
return AsmTemplateEntityDeclParamGroup_Unknown;
|
||||
};
|
||||
|
||||
gb_internal AsmTemplateEntityDeclKind check_asm_find_kind(Entity *entity, Array<AsmTemplateEntityDecl> const &asm_template_entity_decls) {
|
||||
for (auto const &ed : asm_template_entity_decls) {
|
||||
if (ed.entity == entity) {
|
||||
return ed.kind;
|
||||
}
|
||||
}
|
||||
return AsmTemplateEntityDecl_Invalid;
|
||||
};
|
||||
|
||||
|
||||
gb_internal void check_asm_specs(CheckerContext *ctx, Scope *scope, Slice<Ast *> const &specs, Array<AsmTemplateEntityDecl> *asm_template_entity_decls) {
|
||||
|
||||
for (Ast *spec_ : specs) {
|
||||
if (spec_->kind != Ast_AsmSpec) {
|
||||
continue;
|
||||
}
|
||||
ast_node(spec, AsmSpec, spec_);
|
||||
|
||||
GB_ASSERT(spec->name->kind == Ast_Ident);
|
||||
|
||||
Entity *input = scope_lookup(scope, spec->name->Ident.interned, spec->name->Ident.hash);
|
||||
|
||||
bool must_check_value = false;
|
||||
|
||||
if (spec->tied_name == nullptr) {
|
||||
if (spec->type != nullptr) {
|
||||
Type *type = check_type(ctx, spec->type);
|
||||
if (!is_valid_asm_parameter_type(type)) {
|
||||
gbString s = type_to_string(type);
|
||||
error(spec->type, "Invalid type for an asm template. It must be an integer, float, boolean, pointer, multi-pointer, or #simd vector, got '%s'", type);
|
||||
gb_string_free(s);
|
||||
continue;
|
||||
}
|
||||
|
||||
Token name_token = spec->name->Ident.token;
|
||||
|
||||
Entity *entity = alloc_entity_param(scope, name_token, type, false, /*is_value*/true);
|
||||
entity->flags |= EntityFlag_Used;
|
||||
|
||||
Entity *found = scope_insert(scope, entity);
|
||||
if (found == nullptr) {
|
||||
AsmTemplateEntityDecl ed = {};
|
||||
ed.entity = entity;
|
||||
ed.kind = AsmTemplateEntityDecl_Register;
|
||||
if (is_type_internally_pointer_like(type)) {
|
||||
ed.kind = AsmTemplateEntityDecl_Memory;
|
||||
}
|
||||
ed.param_group = AsmTemplateEntityDeclParamGroup_Scratch;
|
||||
|
||||
array_add(asm_template_entity_decls, ed);
|
||||
} else {
|
||||
TokenPos pos = found->token.pos;
|
||||
error(name_token,
|
||||
"Redeclaration of '%.*s' in this scope\n"
|
||||
"\tat %s",
|
||||
LIT(name_token.string), token_pos_to_string(pos));
|
||||
entity = found;
|
||||
continue;
|
||||
}
|
||||
} else if (input == nullptr) {
|
||||
error(spec->name, "Undefined parameter declaration '%.*s'", LIT(spec->name->Ident.token.string));
|
||||
continue;
|
||||
}
|
||||
|
||||
} else {
|
||||
GB_ASSERT(spec->tied_name->kind == Ast_Ident);
|
||||
|
||||
if (spec->type != nullptr) {
|
||||
error(spec->type, "Tied register definitions cannot have a defined type since the values are already defined");
|
||||
}
|
||||
|
||||
if (input == nullptr) {
|
||||
error(spec->name, "Undefined parameter declaration '%.*s'", LIT(spec->name->Ident.token.string));
|
||||
continue;
|
||||
}
|
||||
Entity *output = scope_lookup(scope, spec->tied_name->Ident.interned, spec->tied_name->Ident.hash);
|
||||
if (output == nullptr) {
|
||||
error(spec->name, "Undefined parameter declaration '%.*s'", LIT(spec->name->Ident.token.string));
|
||||
continue;
|
||||
}
|
||||
|
||||
auto input_group = check_asm_find_group(input, *asm_template_entity_decls);
|
||||
auto output_group = check_asm_find_group(output, *asm_template_entity_decls);
|
||||
if (input_group != AsmTemplateEntityDeclParamGroup_Input) {
|
||||
error(input->token, "Parameter tied with '%.*s' must be an input parameter", LIT(output->token.string));
|
||||
continue;
|
||||
}
|
||||
if (output_group != AsmTemplateEntityDeclParamGroup_Output) {
|
||||
error(output->token, "Parameter tied with '%.*s' must be an output parameter", LIT(input->token.string));
|
||||
continue;
|
||||
}
|
||||
|
||||
must_check_value = true;
|
||||
}
|
||||
|
||||
if (spec->value != nullptr) {
|
||||
// TODO(bill): check registers
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
gb_internal void check_asm_instruction_operand(CheckerContext *ctx, Entity *entity, Operand *operand, Ast *expr, bool allow_memory_operands) {
|
||||
if (expr == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
operand->expr = expr;
|
||||
operand->mode = Addressing_Invalid;
|
||||
operand->type = t_invalid;
|
||||
|
||||
GB_ASSERT(entity->kind == Entity_AsmTemplate);
|
||||
auto *ate = &entity->AsmTemplate;
|
||||
|
||||
Scope *param_scope = ate->param_scope;
|
||||
Scope *label_scope = ate->label_scope;
|
||||
gb_unused(param_scope);
|
||||
gb_unused(label_scope);
|
||||
|
||||
switch (expr->kind) {
|
||||
case_ast_node(i, Ident, expr);
|
||||
Entity *found = scope_lookup(param_scope, i->interned, i->hash);
|
||||
if (found == nullptr) {
|
||||
error(expr, "Undeclared asm parameter '%.*s'", LIT(i->token.string));
|
||||
return;
|
||||
}
|
||||
i->entity = found;
|
||||
operand->mode = Addressing_Value;
|
||||
operand->type = found->type;
|
||||
return;
|
||||
case_end;
|
||||
case_ast_node(bl, BasicLit, expr);
|
||||
check_expr(ctx, operand, expr);
|
||||
return;
|
||||
case_end;
|
||||
case_ast_node(i, AsmRegister, expr);
|
||||
// TODO(bill): Check asm register
|
||||
return;
|
||||
case_end;
|
||||
case_ast_node(mem_op, AsmMemoryOperand, expr);
|
||||
if (!allow_memory_operands) {
|
||||
break;
|
||||
}
|
||||
Operand base = {};
|
||||
Operand index = {};
|
||||
Operand scale = {};
|
||||
Operand disp = {};
|
||||
check_asm_instruction_operand(ctx, entity, &base, mem_op->base, false);
|
||||
check_asm_instruction_operand(ctx, entity, &index, mem_op->index, false);
|
||||
check_asm_instruction_operand(ctx, entity, &scale, mem_op->scale, false);
|
||||
check_asm_instruction_operand(ctx, entity, &disp, mem_op->disp, false);
|
||||
|
||||
for (int i = 0; base.expr && i == 0; i++) {
|
||||
if (base.expr->kind == Ast_AsmRegister) {
|
||||
// Okay for now
|
||||
} else {
|
||||
Entity *param_entity = entity_of_node(base.expr);
|
||||
if (param_entity == nullptr || param_entity->kind != Entity_Variable) {
|
||||
gbString s = expr_to_string(base.expr);
|
||||
error(base.expr, "A base value must a memory parameter, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
auto kind = check_asm_find_kind(param_entity, ate->decls);
|
||||
if (kind != AsmTemplateEntityDecl_Memory) {
|
||||
gbString s = expr_to_string(base.expr);
|
||||
error(base.expr, "A scale must be a memory parameter, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; index.expr && i == 0; i++) {
|
||||
if (index.expr->kind == Ast_AsmRegister) {
|
||||
// Okay for now
|
||||
} else {
|
||||
Entity *param_entity = entity_of_node(index.expr);
|
||||
if (param_entity == nullptr || param_entity->kind != Entity_Variable) {
|
||||
gbString s = expr_to_string(index.expr);
|
||||
error(index.expr, "An index value must an integer, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
auto kind = check_asm_find_kind(param_entity, ate->decls);
|
||||
switch (kind) {
|
||||
case AsmTemplateEntityDecl_Register:
|
||||
case AsmTemplateEntityDecl_Immediate:
|
||||
// okay:
|
||||
break;
|
||||
default:
|
||||
{
|
||||
gbString s = expr_to_string(index.expr);
|
||||
error(index.expr, "An index must be an integer value, got %s", s);
|
||||
gb_string_free(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; scale.expr && i == 0; i++) {
|
||||
if (!is_type_integer(scale.type)) {
|
||||
gbString s = expr_to_string(scale.expr);
|
||||
error(scale.expr, "A scale must be a constant integer or an immediate, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
if (scale.mode == Addressing_Constant) {
|
||||
if (scale.value.kind != ExactValue_Integer) {
|
||||
gbString s = exact_value_to_string(scale.value);
|
||||
error(scale.expr, "A scale must be a constant integer or an immediate, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
Entity *param_entity = entity_of_node(scale.expr);
|
||||
if (param_entity == nullptr || param_entity->kind != Entity_Variable) {
|
||||
gbString s = expr_to_string(scale.expr);
|
||||
error(scale.expr, "A scale must be a constant integer or an immediate, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
auto kind = check_asm_find_kind(param_entity, ate->decls);
|
||||
if (kind != AsmTemplateEntityDecl_Immediate) {
|
||||
gbString s = expr_to_string(scale.expr);
|
||||
error(scale.expr, "A scale must be a constant integer or an immediate, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; disp.expr && i == 0; i++) {
|
||||
if (disp.expr->kind == Ast_AsmRegister) {
|
||||
// Okay for now
|
||||
} else {
|
||||
Entity *param_entity = entity_of_node(disp.expr);
|
||||
if (param_entity == nullptr || param_entity->kind != Entity_Variable) {
|
||||
gbString s = expr_to_string(disp.expr);
|
||||
error(disp.expr, "An displacement value must an integer, got %s", s);
|
||||
gb_string_free(s);
|
||||
break;
|
||||
}
|
||||
auto kind = check_asm_find_kind(param_entity, ate->decls);
|
||||
switch (kind) {
|
||||
case AsmTemplateEntityDecl_Register:
|
||||
case AsmTemplateEntityDecl_Immediate:
|
||||
// okay:
|
||||
break;
|
||||
default:
|
||||
{
|
||||
gbString s = expr_to_string(disp.expr);
|
||||
error(disp.expr, "An displacement must be an integer value, got %s", s);
|
||||
gb_string_free(s);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
case_end;
|
||||
case_ast_node(label, AsmLabelDecl, expr);
|
||||
ast_node(name, Ident, label->name);
|
||||
Entity *found = scope_lookup(label_scope, name->interned, name->hash);
|
||||
if (found == nullptr) {
|
||||
error(expr, "Undeclared asm label '.%.*s'", LIT(name->token.string));
|
||||
}
|
||||
name->entity = found;
|
||||
return;
|
||||
case_end;
|
||||
}
|
||||
|
||||
{
|
||||
gbString s = expr_to_string(expr);
|
||||
error(expr, "Invalid asm operand, got %s", s);
|
||||
gb_string_free(s);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
gb_internal void check_asm_template(CheckerContext *ctx, Entity *entity, DeclInfo *d) {
|
||||
GB_ASSERT(entity->kind == Entity_AsmTemplate);
|
||||
auto *ate = &entity->AsmTemplate;
|
||||
|
||||
String asm_template_name = entity->token.string;
|
||||
gb_unused(asm_template_name);
|
||||
|
||||
ast_node(at, AsmTemplate, d->init_expr);
|
||||
|
||||
GB_ASSERT(at->signature != nullptr);
|
||||
if (at->signature->kind != Ast_ProcType) {
|
||||
error(at->signature, "Expected a valid signature, got %.*s", LIT(ast_strings[at->signature->kind]));
|
||||
return;
|
||||
}
|
||||
AstProcType *pt = &at->signature->ProcType;
|
||||
|
||||
ate->param_scope = create_scope(nullptr, nullptr);
|
||||
ate->label_scope = create_scope(nullptr, nullptr);
|
||||
|
||||
ate->decls.allocator = heap_allocator();
|
||||
|
||||
Type *params = check_asm_template_signature_params(ctx, ate->param_scope, pt->params, true, &ate->decls);
|
||||
Type *results = check_asm_template_signature_params(ctx, ate->param_scope, pt->results, false, &ate->decls);
|
||||
|
||||
Type *type = alloc_type_proc(ate->param_scope, params, params->Tuple.variables.count, results, results->Tuple.variables.count, false, pt->calling_convention);
|
||||
type->Proc.diverging = pt->diverging;
|
||||
|
||||
check_asm_specs(ctx, ate->param_scope, at->specs, &ate->decls);
|
||||
{ // check clobbers
|
||||
for (Ast *clobber_ : at->clobbers) {
|
||||
ast_node(clobber, AsmClobber, clobber_);
|
||||
switch (clobber->value->kind) {
|
||||
case Ast_AsmRegister:
|
||||
// TODO(bill): register check
|
||||
break;
|
||||
case Ast_Ident:
|
||||
{
|
||||
String str = clobber->value->Ident.token.string;
|
||||
if (str == "cc") {
|
||||
// okay
|
||||
} else if (str == "memory") {
|
||||
// okay
|
||||
} else {
|
||||
error(clobber->value, "Expected either a register, 'cc', or 'memory' for a '#clobber' specification, got '%.*s'", LIT(str));
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
error(clobber->value, "Expected either a register, 'cc', or 'memory' for a '#clobber' specification");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// collect label decls
|
||||
for (Ast *instruction_ : at->instructions) {
|
||||
switch (instruction_->kind) {
|
||||
case_ast_node(label, AsmLabelDecl, instruction_);
|
||||
GB_ASSERT(label->name->kind == Ast_Ident);
|
||||
Ast *name = label->name;
|
||||
if (is_blank_ident(name)) {
|
||||
error(name, "Asm label definition cannot be '_'");
|
||||
continue;
|
||||
}
|
||||
Entity *label_entity = alloc_entity_label(ate->label_scope, name->Ident.token, nullptr, instruction_, nullptr);
|
||||
Entity *found = scope_insert(ate->label_scope, label_entity);
|
||||
if (found != nullptr) {
|
||||
TokenPos pos = found->token.pos;
|
||||
error(name,
|
||||
"Redeclaration of the label '%.*s' in this scope\n"
|
||||
"\tat %s",
|
||||
LIT(name->Ident.token.string), token_pos_to_string(pos));
|
||||
continue;
|
||||
}
|
||||
name->Ident.entity = label_entity;
|
||||
case_end;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (Ast *instruction_ : at->instructions) {
|
||||
switch (instruction_->kind) {
|
||||
case_ast_node(instr, AsmInstruction, instruction_);
|
||||
GB_ASSERT(instr->name->kind == Ast_Ident);
|
||||
for (Ast *expr : instr->operands) {
|
||||
Operand operand = {};
|
||||
check_asm_instruction_operand(ctx, entity, &operand, expr, /*allow_memory_operands*/true);
|
||||
}
|
||||
case_end;
|
||||
case_ast_node(label, AsmLabelDecl, instruction_);
|
||||
// already done
|
||||
case_end;
|
||||
default:
|
||||
error(instruction_, "Unexpected instruction in asm template");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
gb_internal void check_entity_decl(CheckerContext *ctx, Entity *e, DeclInfo *d, Type *named_type) {
|
||||
if (e->state == EntityState_Resolved) {
|
||||
return;
|
||||
@@ -2062,6 +2567,10 @@ gb_internal void check_entity_decl(CheckerContext *ctx, Entity *e, DeclInfo *d,
|
||||
case Entity_ProcGroup:
|
||||
check_proc_group_decl(&c, e, d);
|
||||
break;
|
||||
|
||||
case Entity_AsmTemplate:
|
||||
check_asm_template(&c, e, d);
|
||||
break;
|
||||
}
|
||||
|
||||
e->state = EntityState_Resolved;
|
||||
|
||||
Reference in New Issue
Block a user