From 768fedcff414dd98981df234c4922b292a37cb40 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Mon, 10 Aug 2026 13:21:14 +0100 Subject: [PATCH] Minor fixes to labels; restrict to amd64 usage --- src/llvm_backend_asm.cpp | 22 ++++++++++++---------- src/name_canonicalization.cpp | 4 +++- src/parser.cpp | 4 ++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/llvm_backend_asm.cpp b/src/llvm_backend_asm.cpp index cc08841ff..c80e195a3 100644 --- a/src/llvm_backend_asm.cpp +++ b/src/llvm_backend_asm.cpp @@ -8,9 +8,11 @@ gb_internal AsmTemplateEntityDecl *lb_asm_entity_decl(Arraytoken.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 op_number, Array *decls, Ast *op, bool print_prefixes=true) { +gb_internal gbString lb_asm_write_operand(gbString asm_string, Entity *tmpl_entity, Array op_number, Array *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 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 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: diff --git a/src/name_canonicalization.cpp b/src/name_canonicalization.cpp index 13fa01830..1e615d7c8 100644 --- a/src/name_canonicalization.cpp +++ b/src/name_canonicalization.cpp @@ -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)) { diff --git a/src/parser.cpp b/src/parser.cpp index 48e933c63..d63c60585 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -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;