Skip to content

Commit

Permalink
Reference attributes with string option directly
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhsnn committed Dec 20, 2024
1 parent 8b816e5 commit 7cd2b78
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
6 changes: 3 additions & 3 deletions codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -3940,9 +3940,9 @@ static void emit_symbol(Obj *var) {
println(" .globl \"%s\"", asm_name(var));

if (var->alias_name)
println(" .set \"%s\", %.*s", var->name, var->alias_name->len, var->alias_name->loc);
println(" .set \"%s\", \"%s\"", var->name, var->alias_name);

char *vis_mode = var->visibility ? var->visibility->tval->str : opt_visibility;
char *vis_mode = var->visibility ? var->visibility : opt_visibility;
if (vis_mode && (!strcmp(vis_mode, "hidden") ||
!strcmp(vis_mode, "internal") || !strcmp(vis_mode, "protected")))
println(" .%s \"%s\"", vis_mode, asm_name(var));
Expand All @@ -3967,7 +3967,7 @@ static void emit_data(Obj *var) {
bool use_rodata = !opt_fpic && is_const_var(var);

if (var->section_name)
Printstrf(" .section \"%s\"", var->section_name->tval->str);
Printstrf(" .section \"%s\"", var->section_name);
else if (var->is_tls)
Printstrf(" .section .%s", var->init_data ? "tdata" : "tbss");
else if (use_rodata)
Expand Down
53 changes: 21 additions & 32 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ typedef struct {
bool is_weak;
bool local_only;
Obj *cleanup_fn;
Token *alias;
Token *section;
Token *visibility;
char *alias;
char *section;
char *visibility;
int align;
} VarAttr;

Expand Down Expand Up @@ -562,54 +562,43 @@ static void attr_weak(Token *loc, bool *b, TokenKind kind) {
bool_attr("weak", loc, b, kind);
}

static void str_attr(char *name, Token *loc, Token **t, TokenKind kind) {
static void str_attr(char *name, Token *loc, char **str, TokenKind kind) {
if (*str)
return;
for (Token *tok = loc->attr_next; tok; tok = tok->attr_next) {
if (tok->kind == kind && equal_ext(tok, name)) {
*t = str_tok(&(Token *){0}, skip(tok->next, "("));
Token *tok2;
*str = str_tok(&tok2, skip(tok->next, "("))->tval->str;
skip(tok2, ")");
return;
}
}
}

static void attr_alias(Token *loc, Token **str_tok, TokenKind kind) {
if (!*str_tok)
str_attr("alias", loc, str_tok, kind);
}

static void attr_section(Token *loc, Token **str_tok, TokenKind kind) {
if (!*str_tok)
str_attr("section", loc, str_tok, kind);
}

static void attr_visibility(Token *loc, Token **str_tok, TokenKind kind) {
if (!*str_tok)
str_attr("visibility", loc, str_tok, kind);
}

static void tyspec_attr(Token *tok, VarAttr *attr, TokenKind kind) {
attr_alias(tok, &attr->alias, kind);
attr_aligned(tok, &attr->align, kind);
attr_cleanup(tok, &attr->cleanup_fn, kind);
attr_section(tok, &attr->section, kind);
attr_visibility(tok, &attr->visibility, kind);
attr_weak(tok, &attr->is_weak, kind);
str_attr("alias", tok, &attr->alias, kind);
str_attr("section", tok, &attr->section, kind);
str_attr("visibility", tok, &attr->visibility, kind);
}

static void symbol_attr(Obj *var, VarAttr *attr, Token *name, Token *tok) {
var->alias_name = attr->alias;
attr_alias(name, &var->alias_name, TK_ATTR);
attr_alias(name->next, &var->alias_name, TK_BATTR);
attr_alias(tok, &var->alias_name, TK_ATTR);
str_attr("alias", name, &var->alias_name, TK_ATTR);
str_attr("alias", name->next, &var->alias_name, TK_BATTR);
str_attr("alias", tok, &var->alias_name, TK_ATTR);

var->section_name = attr->section;
attr_section(name, &var->section_name, TK_ATTR);
attr_section(name->next, &var->section_name, TK_BATTR);
attr_section(tok, &var->section_name, TK_ATTR);
str_attr("section", name, &var->section_name, TK_ATTR);
str_attr("section", name->next, &var->section_name, TK_BATTR);
str_attr("section", tok, &var->section_name, TK_ATTR);

var->visibility = attr->visibility;
attr_visibility(name, &var->visibility, TK_ATTR);
attr_visibility(name->next, &var->visibility, TK_BATTR);
attr_visibility(tok, &var->visibility, TK_ATTR);
str_attr("visibility", name, &var->visibility, TK_ATTR);
str_attr("visibility", name->next, &var->visibility, TK_BATTR);
str_attr("visibility", tok, &var->visibility, TK_ATTR);

var->is_weak = attr->is_weak;
attr_weak(name, &var->is_weak, TK_ATTR);
Expand Down
6 changes: 3 additions & 3 deletions slimcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ struct Obj {
bool is_static;
bool is_weak;
Obj *static_lvars;
Token *alias_name;
Token *visibility;
char *alias_name;
char *visibility;

// Global variable
bool is_tentative;
bool is_tls;
Token *section_name;
char *section_name;
char *init_data;
Relocation *rel;

Expand Down

0 comments on commit 7cd2b78

Please sign in to comment.