tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] Transparent Function Aliases


From: Asher Mancinelli
Subject: [Tinycc-devel] Transparent Function Aliases
Date: Thu, 17 Mar 2022 13:42:11 -0600

Hello,

I'm attempting to get this WG14 proposal (N2901) working in TinyCC, based off of this diff from JeanHeyd Meneide. To summarize, "typedef, but for functions".

Assuming the following definition:
int __callme(int x) { return x; }

I would like this:
_Alias callme = __callme;

to be equivalent to the following declaration:
extern int callme(int x) __asm__("__callme"); // or __attribute__((alias("__callme")));

In this patch, I can access the alias target and the alias identifier, I think I just need to push the symbol with the alias identifier, with the alias_target property set to the alias target's token. Suggestions, advice, or "here be dragons" are all appreciated.

Cheers,
Asher Mancinelli
---
 tccgen.c | 41 ++++++++++++++++++++++++++++++++++++++++-
 tcctok.h |  1 +
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/tccgen.c b/tccgen.c
index 67e205b..f391a4e 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -5028,6 +5028,38 @@ static void sym_to_attr(AttributeDef *ad, Sym *s)
     merge_funcattr(&ad->f, &s->f);
 }

+/* Create a symbol with the name of the alias source, set asm label to the
+ * alias target. Return the alias's symbol. */
+static Sym* parse_transparent_alias_decl(AttributeDef *ad, CType *t)
+{
+  int target;
+  int alias = tok;
+  Sym *target_sym;
+
+  if (alias < TOK_UIDENT)
+    expect("identifier");
+
+  printf("Got alias '%s'", get_tok_str(alias, NULL));
+
+  next();
+  skip('=');
+
+  target = tok;
+
+  printf("Got alias target '%s'", get_tok_str(target, NULL));
+
+  target_sym = sym_find(target);
+  *t = target_sym->type;
+  const char* target_str = get_tok_str(target, NULL);
+
+  if (!target_sym) {
+    tcc_error("alias target '%s' is undefined", target_str);
+  }
+
+  ad->alias_target = target;
+  return sym_find(alias);
+}
+
 /* Add type qualifiers to a type. If the type is an array then the qualifiers
    are added to the element type, copied because it could be a typedef. */
 static void parse_btype_qualify(CType *type, int qualifiers)
@@ -5172,6 +5204,14 @@ static int parse_btype(CType *type, AttributeDef *ad)
                 goto basic_type2;
             }
             break;
+
+        case TOK__Alias:
+            next();
+            tcc_warning("_Alias is a non-standard extension");
+            s = parse_transparent_alias_decl(ad, type);
+            // sym_to_attr(ad, s);
+            return 1;
+
         case TOK_CONST1:
         case TOK_CONST2:
         case TOK_CONST3:
@@ -6261,7 +6301,6 @@ ST_FUNC void unary(void)
         vpushsym(&s->type, s);
         next();
         break;
-
     case TOK_GENERIC:
     {
  CType controlling_type;
diff --git a/tcctok.h b/tcctok.h
index d4c1ef5..615b277 100644
--- a/tcctok.h
+++ b/tcctok.h
@@ -19,6 +19,7 @@
      DEF(TOK_CASE, "case")

      DEF(TOK__Atomic, "_Atomic")
+     DEF(TOK__Alias, "_Alias") /* Strong transparent symbol alias */
      DEF(TOK_CONST1, "const")
      DEF(TOK_CONST2, "__const") /* gcc keyword */
      DEF(TOK_CONST3, "__const__") /* gcc keyword */
--
2.28.0

reply via email to

[Prev in Thread] Current Thread [Next in Thread]