Minor fixes to labels; restrict to amd64 usage

This commit is contained in:
gingerBill
2026-08-10 13:21:14 +01:00
parent cee5bd79e0
commit 768fedcff4
3 changed files with 19 additions and 11 deletions

View File

@@ -8,9 +8,11 @@ gb_internal AsmTemplateEntityDecl *lb_asm_entity_decl(Array<AsmTemplateEntityDec
return nullptr;
}
gb_internal gbString lb_asm_write_label_name(gbString asm_string, AstIdent *label_ident) {
gb_internal gbString lb_asm_write_label_name(gbString asm_string, Entity *tmpl_entity, AstIdent *label_ident) {
String name = label_ident->token.string;
asm_string = gb_string_appendc(asm_string, ".L");
asm_string = gb_string_appendc(asm_string, ".L_");
asm_string = gb_string_append_length(asm_string, tmpl_entity->token.string.text, tmpl_entity->token.string.len);
asm_string = gb_string_appendc(asm_string, "_");
asm_string = gb_string_append_length(asm_string, name.text, name.len);
// ${:uid} expands to a per-instantiation unique integer, so repeated
// inlining of the same template can't collide on the label symbol.
@@ -18,7 +20,7 @@ gb_internal gbString lb_asm_write_label_name(gbString asm_string, AstIdent *labe
return asm_string;
}
gb_internal gbString lb_asm_write_operand(gbString asm_string, Array<i32> op_number, Array<AsmTemplateEntityDecl> *decls, Ast *op, bool print_prefixes=true) {
gb_internal gbString lb_asm_write_operand(gbString asm_string, Entity *tmpl_entity, Array<i32> op_number, Array<AsmTemplateEntityDecl> *decls, Ast *op, bool print_prefixes=true) {
switch (op->kind) {
case_ast_node(i, Ident, op);
Entity *e = entity_of_node(op);
@@ -30,17 +32,17 @@ gb_internal gbString lb_asm_write_operand(gbString asm_string, Array<i32> op_num
case_end;
case_ast_node(mem_op, AsmMemoryOperand, op);
if (mem_op->disp) {
asm_string = lb_asm_write_operand(asm_string, op_number, decls, mem_op->disp, /*print_prefixes*/false);
asm_string = lb_asm_write_operand(asm_string, tmpl_entity, op_number, decls, mem_op->disp, /*print_prefixes*/false);
}
asm_string = gb_string_appendc(asm_string, "(");
GB_ASSERT(mem_op->base != nullptr);
asm_string = lb_asm_write_operand(asm_string, op_number, decls, mem_op->base);
asm_string = lb_asm_write_operand(asm_string, tmpl_entity, op_number, decls, mem_op->base);
if (mem_op->index) {
asm_string = gb_string_appendc(asm_string, ",");
asm_string = lb_asm_write_operand(asm_string, op_number, decls, mem_op->index);
asm_string = lb_asm_write_operand(asm_string, tmpl_entity, op_number, decls, mem_op->index);
if (mem_op->scale) {
asm_string = gb_string_appendc(asm_string, ",");
asm_string = lb_asm_write_operand(asm_string, op_number, decls, mem_op->scale, /*print_prefixes*/false);
asm_string = lb_asm_write_operand(asm_string, tmpl_entity, op_number, decls, mem_op->scale, /*print_prefixes*/false);
}
}
asm_string = gb_string_appendc(asm_string, ")");
@@ -66,7 +68,7 @@ gb_internal gbString lb_asm_write_operand(gbString asm_string, Array<i32> op_num
case_end;
case_ast_node(label, AsmLabelDecl, op);
asm_string = lb_asm_write_label_name(asm_string, &label->name->Ident);
asm_string = lb_asm_write_label_name(asm_string, tmpl_entity, &label->name->Ident);
case_end;
default:
GB_PANIC("TODO %s", expr_to_string(op));
@@ -248,11 +250,11 @@ gb_internal lbValue lb_emit_asm_template_call(lbProcedure *p, Entity *entity, Ar
if (j < instr->operands.count-1) {
asm_string = gb_string_appendc(asm_string, ", ");
}
asm_string = lb_asm_write_operand(asm_string, op_number, &ops, op);
asm_string = lb_asm_write_operand(asm_string, entity, op_number, &ops, op);
}
case_end;
case_ast_node(label, AsmLabelDecl, instr_);
asm_string = lb_asm_write_label_name(asm_string, &label->name->Ident);
asm_string = lb_asm_write_label_name(asm_string, entity, &label->name->Ident);
asm_string = gb_string_appendc(asm_string, ":");
case_end;
default:

View File

@@ -573,7 +573,8 @@ gb_internal gbString string_canonical_entity_name(gbAllocator allocator, Entity
gb_internal void write_canonical_parent_prefix(TypeWriter *w, Entity *e) {
GB_ASSERT(e != nullptr);
if (e->kind == Entity_Procedure || e->kind == Entity_TypeName || e->kind == Entity_Variable) {
if (e->kind == Entity_Procedure || e->kind == Entity_AsmTemplate ||
e->kind == Entity_TypeName || e->kind == Entity_Variable) {
if (e->kind == Entity_Procedure && (e->Procedure.is_export || e->Procedure.is_foreign)) {
// no prefix
return;
@@ -740,6 +741,7 @@ write_base_name:
// For debug symbols only
/*fallthrough*/
case Entity_Procedure:
case Entity_AsmTemplate:
case Entity_Variable:
type_writer_append(w, e->token.string.text, e->token.string.len);
if (is_type_polymorphic(e->type)) {

View File

@@ -2696,6 +2696,10 @@ gb_internal Ast *parse_asm_template(AstFile *f) {
asm_instructions = slice_from_array(instructions);
}
if (build_context.metrics.arch != TargetArch_amd64) {
syntax_error(token, "asm templates are currently only supported on -target:amd64");
}
Ast *asm_template = alloc_ast_node(f, Ast_AsmTemplate);
asm_template->AsmTemplate.token = token;
asm_template->AsmTemplate.has_side_effects = has_side_effects;