aboutsummaryrefslogtreecommitdiffhomepage
path: root/c
diff options
context:
space:
mode:
author lemon<lsof@mailbox.org>2025-11-15 19:53:47 +0100
committer lemon<lsof@mailbox.org>2025-11-15 19:53:47 +0100
commit2ca9a54daaa0a955dabf38862cd777be359252e0 (patch)
tree51a0e9470a5bbd4bbe0b1d00fc50da2dc9ad9d93 /c
parent1910d875cfcad320cbb87c5e8c846d5c53846a1a (diff)
ir: 'trap' jump; c: __builtin_trap; lex: __has_builtin
Diffstat (limited to 'c')
-rw-r--r--c/builtin.c24
-rw-r--r--c/c.c6
-rw-r--r--c/lex.c29
3 files changed, 50 insertions, 9 deletions
diff --git a/c/builtin.c b/c/builtin.c
index d386480..23e1872 100644
--- a/c/builtin.c
+++ b/c/builtin.c
@@ -27,9 +27,9 @@ callcheck(const struct span *span, int nparam, const union type *param, int narg
#define DEF_FNLIKE_SEMA(name, retty, ...) \
static bool \
name##_sema(struct comp *cm, struct expr *ex) { \
- static union type par[] = { __VA_ARGS__, {0} }; \
+ static union type par[] = { {{0}}, __VA_ARGS__ }; \
ex->ty = retty; \
- return callcheck(&ex->span, arraylength(par)-1, par, ex->narg, ex->sub+1); \
+ return callcheck(&ex->span, arraylength(par)-1, par+1, ex->narg, ex->sub+1); \
}
static bool
@@ -63,6 +63,15 @@ va_end_comp(struct function *fn, struct expr *ex, bool discard)
return NOREF;
}
+DEF_FNLIKE_SEMA(trap, mktype(TYVOID), )
+
+static union ref
+trap_comp(struct function *fn, struct expr *ex, bool discard)
+{
+ puttrap(fn);
+ return NOREF;
+}
+
union ref
builtin_va_arg_comp(struct function *fn, const struct expr *ex, bool discard)
{
@@ -74,6 +83,7 @@ builtin_va_arg_comp(struct function *fn, const struct expr *ex, bool discard)
#define LIST_BUILTINS(_) \
_(va_start) \
_(va_end) \
+ _(trap)
static const struct {
const char *name;
@@ -84,11 +94,11 @@ static const struct {
#undef FNS
};
-const char *intern(const char *);
void
putbuiltins(struct env *env)
{
for (int i = 0; i < arraylength(tab); ++i) {
+ const char *intern(const char *);
envadddecl(env, &(struct decl) {
.name = intern(tab[i].name),
.isbuiltin = 1,
@@ -97,5 +107,13 @@ putbuiltins(struct env *env)
}
}
+bool
+hasbuiltin(const char *name, uint len)
+{
+ for (int i = 0; i < arraylength(tab); ++i)
+ if (!strncmp(name, tab[i].name, len))
+ return 1;
+ return 0;
+}
/* vim:set ts=3 sw=3 expandtab: */
diff --git a/c/c.c b/c/c.c
index 69efed6..fc6bcc0 100644
--- a/c/c.c
+++ b/c/c.c
@@ -3438,10 +3438,12 @@ compileexpr(struct function *fn, const struct expr *ex, bool discard)
useblk(fn, tr);
expreffects(fn, &sub[1]);
end = newblk(fn);
- putbranch(fn, end);
+ if (fn->curblk)
+ putbranch(fn, end);
useblk(fn, fl);
expreffects(fn, &sub[2]);
- putbranch(fn, end);
+ if (fn->curblk)
+ putbranch(fn, end);
useblk(fn, end);
return NOREF;
}
diff --git a/c/lex.c b/c/lex.c
index eab9ce3..60ce864 100644
--- a/c/lex.c
+++ b/c/lex.c
@@ -612,6 +612,7 @@ struct macro {
const struct token *tk;
int n;
} rlist;
+ void (*handlerfn)(struct lexer *, struct token *ret, struct rlist arg);
};
};
@@ -988,7 +989,7 @@ tryexpand(struct lexer *lx, struct token *tk)
if (l->macno == macidx)
return 0;
- if (mac->special) {
+ if (mac->special && !mac->fnlike) {
mac->handler(lx, tk);
pushmacstk(lx, &span, &(struct macrostack){
.rlist = { alloccopy(lx->tmparena, tk, sizeof *tk, 0), 1 },
@@ -1080,9 +1081,13 @@ expandfnmacro(struct lexer *lx, struct span *span, struct macro *mac)
joinspan(&excessspan.ex, tk.span.ex);
error(&excessspan, "macro `%s' passed %d arguments, but takes just %d", mac->name, narg, mac->nparam);
}
-
- /* make new rlist with args replaced */
- if (mac->nparam) {
+ if (mac->special) {
+ mac->handlerfn(lx, &tk, (struct rlist){argsbuf.p, argsbuf.n});
+ pushmacstk(lx, span, &(struct macrostack){
+ .rlist = { alloccopy(lx->tmparena, &tk, sizeof tk, 0), 1 },
+ .macno = mac - macros.p,
+ });
+ } else if (mac->nparam) { /* make new rlist with args replaced */
struct token lhsargforpaste;
bool lhsargpaste = 0, rhsargpaste = 0;
for (int i = 0; i < mac->rlist.n; ++i) {
@@ -1897,6 +1902,21 @@ mac__time__handler(struct lexer *lx, struct token *tk)
}
static void
+mac__has_builtin(struct lexer *lx, struct token *tk, struct rlist arg)
+{
+ extern bool hasbuiltin(const char *, uint n);
+ bool has = 0;
+ tk->t = TKNUMLIT, tk->len = 1;
+ if (arg.n == 1) {
+ if (arg.tk->t == TKIDENT)
+ has = hasbuiltin(arg.tk->s, arg.tk->len);
+ else if (in_range(arg.tk->t, TKWBEGIN_, TKWEND_))
+ has = arg.tk->len >= sizeof "__builtin_" && !memcmp(arg.tk->s, "__builtin_", 10);
+ }
+ tk->s = &"01"[has];
+}
+
+static void
addpredefmacros(void)
{
static const struct token tok_1 = { TKNUMLIT, .s = "1", .len = 1 };
@@ -1906,6 +1926,7 @@ addpredefmacros(void)
{ "__LINE__", .predefined = 1, .special = 1, .handler = mac__line__handler },
{ "__DATE__", .predefined = 1, .special = 1, .handler = mac__date__handler },
{ "__TIME__", .predefined = 1, .special = 1, .handler = mac__time__handler },
+ { "__has_builtin", .predefined = 1, .nparam = 1, .fnlike = 1, .special = 1, .handlerfn = mac__has_builtin },
{ "__STDC__", .predefined = 1, .rlist = { &tok_1, 1 } },
{ "__STDC_VERSION__", .predefined = 1, .rlist = { &tok_ver, 1 } },
{ "__STDC_HOSTED__", .predefined = 1, .rlist = { &tok_1, 1 } },