Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ jobs:
sudo apt-get install -y --no-install-recommends \
qemu-system-arm qemu-system-misc qemu-system-mips \
qemu-system-ppc qemu-system-sparc qemu-system-x86 \
${{ matrix.qemu-riscv }} libglib2.0-dev \
${{ matrix.qemu-riscv }} libglib2.0-dev gdb-multiarch \
clang-${{ matrix.llvm }} llvm-${{ matrix.llvm }} lld-${{ matrix.llvm }}
echo "/usr/lib/llvm-${{ matrix.llvm }}/bin" >> "$GITHUB_PATH"
# The TriCore harness's output and the instruction counts are QEMU
Expand Down
11 changes: 9 additions & 2 deletions src/arch/regalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2487,12 +2487,19 @@ void ra_debug_locs(struct ir_func *fn, const struct ir_vhome *home)

/* -g: room for each instruction's code offset (ir_func.ins_pc), when the
* function has a variable mem2reg followed; NULL otherwise. */
/* -g: does a local live in a nested block? Its DW_TAG_lexical_block needs
* its instructions' code, so ins_pc (dwarf.c, emit_scopes). */
static int has_block_locals(const struct ir_func *fn)
{
return fn->nscopes > 1 && fn->scope_up;
}

void ra_debug_pc_begin(struct ir_func *fn)
{
free(fn->ins_pc);
fn->ins_pc = NULL;
if (((target_debug_info() && fn->vreg_var) || target_stack_info()) &&
fn->nins) {
if (((target_debug_info() && (fn->vreg_var || has_block_locals(fn))) ||
target_stack_info()) && fn->nins) {
fn->ins_pc = xmalloc(((size_t)fn->nins + 1) * sizeof *fn->ins_pc);
for (int k = 0; k <= fn->nins; k++)
fn->ins_pc[k] = -1;
Expand Down
507 changes: 460 additions & 47 deletions src/debug/dwarf.c

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ struct ir_ins {
int line;
int col;
int synth;
/* -g: the lexical scope the instruction's code belongs to -- 0 the
* function's own, else an index into ir_func.scope_up (irgen). Copied
* with the instruction, as its line is; an instruction a pass invents
* reads 0, which leaves its code outside any nested block. */
int scope;
int dst, a, b;
int c; /* IR_CMPXCHG: the third operand (desired value) */
int nrp; /* IR_RET of a struct as its register units
Expand Down Expand Up @@ -547,6 +552,12 @@ struct ir_dbgvar {
int is_param;
struct type *ty;
int line, col;
int scope; /* its block's lexical scope (ir_ins.scope) */
/* A variable of an inlined call: the function it was declared in and
* its index among that function's dbgvars (the DWARF's abstract
* origin); NULL and -1 for the function's own. */
struct func *origin;
int origin_idx;
/* mem2reg took this variable out of memory although something
* assigned it: its slot no longer follows it. Its only reader is a
* backend deciding whether the slot is a true DW_AT_location -- for a
Expand Down Expand Up @@ -647,6 +658,18 @@ struct ir_func {
* function-level locals span the whole function; only nested-block locals get
* a narrower range. Length nvars; unused (NULL) when there are no locals. */
int *var_scope_lo, *var_scope_hi;
/* -g: the lexical scopes of the blocks that declare locals, 1 to
* nscopes - 1 (0 is the function's own): scope_up[s] the one it is
* nested in. Instructions name theirs (ir_ins.scope), and the DWARF
* writer makes a DW_TAG_lexical_block of each from its instructions'
* code -- which, unlike var_scope_lo/hi, no reordering makes stale. */
int *scope_up;
int nscopes, scope_cap;
/* ...and where a scope is an inlined call (the inliner), the function
* inlined there and the call's line and column; NULL and 0 for a
* block. Each array nscopes long, or NULL while no call is inlined. */
struct func **scope_fn;
int *scope_cline, *scope_ccol;
/* exception regions (C++'s lowering), the catch types their landing
* pads' selectors number (1-based; NULL: catch-all), and — codegen —
* every call's code */
Expand Down
97 changes: 85 additions & 12 deletions src/ir/irgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,17 @@ void ir_locals_fill(struct ir_func *fn, struct func *f, int nvars)
}
}

/* -g: the lexical scope being generated (ir_ins.scope). collect_locals
* gives each STMT_BLOCK or STMT_FOR that declares a local its scope, in
* the statement itself (dbg_scope): a lookup by pointer was a search, and
* 200,000 blocks in one function made it quadratic (many-declarations). */
static int g_cur_scope;

static int scope_of_stmt(const struct stmt *s)
{
return s->dbg_scope - 1;
}

struct ir_ins *emit(struct ir_func *fn)
{
if (fn->nins == fn->cap) {
Expand All @@ -200,6 +211,7 @@ struct ir_ins *emit(struct ir_func *fn)
i->line = g_cur_line;
i->col = g_cur_col;
i->synth = g_synth;
i->scope = g_cur_scope;
i->dst = i->a = i->b = -1;
i->w = 4;
i->size = 4;
Expand Down Expand Up @@ -7083,6 +7095,10 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s,
lc.cont = new_label(fn);
lc.brk = new_label(fn);
int for_vla = g_nvla;
int for_lo = fn->nins;
int for_scope = g_cur_scope, fs = scope_of_stmt(s);
if (fs >= 0)
g_cur_scope = fs;
lc.brk_vla = lc.cont_vla = g_nvla;
if (s->initdecl)
gen_stmt(fn, s->initdecl, &lc);
Expand All @@ -7105,6 +7121,17 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s,
emit_label(fn, lc.brk);
vla_release(fn, for_vla);
g_nvla = for_vla;
/* `for (int i = 0; ...)`: i's scope is the for statement
* (C11 6.8.5p5), as a block's local's is its block -- so two
* loops' i are two variables a debugger tells apart, and may
* share a slot */
for (struct stmt *c = s->initdecl; c; c = c->next)
if (c->kind == STMT_DECL && !c->is_extern && !c->sglob &&
c->var_index >= 0 && c->var_index < fn->nvars) {
fn->var_scope_lo[c->var_index] = for_lo;
fn->var_scope_hi[c->var_index] = fn->nins;
}
g_cur_scope = for_scope;
break;
}
case STMT_BLOCK: {
Expand All @@ -7114,7 +7141,11 @@ static void gen_stmt(struct ir_func *fn, struct stmt *s,
* (codegen). Static/extern decls have no frame slot — skip them. */
int lo = fn->nins;
int depth = g_nvla;
int up_scope = g_cur_scope, bs = scope_of_stmt(s);
if (bs >= 0)
g_cur_scope = bs;
gen_stmt(fn, s->body, loop);
g_cur_scope = up_scope;
vla_release(fn, depth); /* the block's VLAs end with it */
g_nvla = depth;
int hi = fn->nins;
Expand Down Expand Up @@ -7195,42 +7226,79 @@ static void add_dbgvar(struct ir_func *fn, const char *name, int vreg,
v->line = line;
v->col = col;
v->moved = 0;
v->scope = 0;
v->origin = NULL;
v->origin_idx = -1;
}

/* -g: walk the body for block-scope locals. Each STMT_DECL owns a var slot
* (var_index); a static local became a global (sglob) and has no frame slot,
* so it is skipped. Flattened into the subprogram — lexical-block scoping is a
* later refinement, not needed to print a local by name. */
static void collect_locals(struct ir_func *fn, struct stmt *s)
/* Does this list of statements declare a local of its own (a slot, not an
* extern or a static)? */
static int declares_local(const struct stmt *s)
{
for (; s; s = s->next)
if (s->kind == STMT_DECL && !s->is_extern && !s->is_vm_typedef &&
!s->sglob)
return 1;
return 0;
}

/* A new lexical scope for block s, inside scope `up`. */
static int new_scope(struct ir_func *fn, struct stmt *s, int up)
{
if (fn->nscopes == fn->scope_cap) {
fn->scope_cap = fn->scope_cap ? fn->scope_cap * 2 : 8;
fn->scope_up = xrealloc(fn->scope_up, (size_t)fn->scope_cap * sizeof *fn->scope_up);
}
int id = fn->nscopes++;
fn->scope_up[id] = up;
s->dbg_scope = id + 1;
return id;
}

static void collect_locals(struct ir_func *fn, struct stmt *s, int scope)
{
for (; s; s = s->next) {
switch (s->kind) {
case STMT_DECL:
if (s->is_extern || s->is_vm_typedef) /* no local slot at all */
break;
if (!s->sglob)
if (!s->sglob) {
add_dbgvar(fn, s->name, s->var_index, 0,
fn->src->var_tys[s->var_index], s->line, s->col);
fn->dbgvars[fn->ndbgvars - 1].scope = scope;
}
break;
case STMT_IF:
collect_locals(fn, s->thn);
collect_locals(fn, s->els);
collect_locals(fn, s->thn, scope);
collect_locals(fn, s->els, scope);
break;
case STMT_EHREGION:
collect_locals(fn, s->body);
collect_locals(fn, s->thn);
collect_locals(fn, s->body, scope);
collect_locals(fn, s->thn, scope);
break;
case STMT_WHILE:
case STMT_DO:
collect_locals(fn, s->body);
collect_locals(fn, s->body, scope);
break;
case STMT_FOR:
collect_locals(fn, s->initdecl);
collect_locals(fn, s->body);
case STMT_FOR: {
/* `for (int i ...)`: i's scope is the for statement */
s->dbg_scope = 0;
int in = declares_local(s->initdecl) ? new_scope(fn, s, scope) : scope;
collect_locals(fn, s->initdecl, in);
collect_locals(fn, s->body, in);
break;
}
case STMT_BLOCK:
s->dbg_scope = 0;
collect_locals(fn, s->body,
declares_local(s->body) ? new_scope(fn, s, scope) : scope);
break;
case STMT_SWITCH:
collect_locals(fn, s->body);
collect_locals(fn, s->body, scope);
break;
default:
break;
Expand Down Expand Up @@ -7406,7 +7474,12 @@ static void gen_func_(struct ir_func *fn, struct func *f)
for (int i = 0; i < f->nparams; i++)
add_dbgvar(fn, f->params[i], i, 1, f->param_tys[i],
f->param_lines[i], f->param_cols[i]);
collect_locals(fn, f->body);
g_cur_scope = 0;
fn->nscopes = 1;
fn->scope_cap = 8;
fn->scope_up = xmalloc((size_t)fn->scope_cap * sizeof *fn->scope_up);
fn->scope_up[0] = -1;
collect_locals(fn, f->body, 0);
/* Local scope ranges: default to the whole function ([0, +inf), narrowed to
* the real end below) so any local not inside a nested block never coalesces
* — the safe default. gen_stmt's STMT_BLOCK case narrows nested-block locals. */
Expand Down
7 changes: 6 additions & 1 deletion src/link/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,12 @@ static void collect_sections(struct linker *l, struct object *o)
* Concatenated per name; the offset this object's piece
* landed at is what its relocations resolve against. */
const char *nm = o->shstr + sh->sh_name;
if (l->keep_debug && sh->sh_type == SHT_PROGBITS &&
/* (MIPS objects from GCC and clang type theirs
* SHT_MIPS_DWARF, 0x7000001e, not PROGBITS: GNU ld keeps
* them, and this dropped every one, so a clang-built MIPS
* image had no debug info at all) */
if (l->keep_debug && (sh->sh_type == SHT_PROGBITS ||
sh->sh_type == 0x7000001eu) &&
strncmp(nm, ".debug_", 7) == 0 && sh->sh_size &&
!(sh->sh_flags & 0x80000000u /* SHF_EXCLUDE */)) {
int d = dbgsec_for(l, nm);
Expand Down
69 changes: 69 additions & 0 deletions src/opt/inline.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ static int inlinable(struct ir_func *cf, int force, int sole, const char **why,
detail[0] = 0;

if (c->is_varargs) { *why = "callee-is-varargs"; return 0; }
/* main stays a frame of its own, as GCC keeps it: called once, from a
* reset handler or crt0, it gains nothing inlined, and inlined it is
* an inline frame of its caller -- gdb's backtrace then runs past the
* entry function into 0x0, and `break main` stops in reset's code */
if (cf->name && !strcmp(cf->name, "main")) { *why = "callee-is-main"; return 0; }
if (cf->nins == 0) { *why = "callee-not-defined-here"; return 0; }
/* __attribute__((always_inline)) overrides the SIZE budget and
* nothing else. Every other test below is a thing this inliner
Expand Down Expand Up @@ -587,6 +592,46 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf)
ad->line = call.line; ad->col = call.col;
}

/* -g: the callee's code is an inlined call, a scope of the caller's
* inside the call's, and the callee's own scopes inside that one --
* its blocks, and the calls inlined into it before -- renumbered from
* `base`. The DWARF writer makes it a DW_TAG_inlined_subroutine. */
int base = -1;
if (fn->scope_up && cf->scope_up && cf->nscopes > 0) {
base = fn->nscopes;
int ns = base + cf->nscopes;
int had = fn->scope_fn != NULL;
fn->scope_up = xrealloc(fn->scope_up, (size_t)ns * sizeof *fn->scope_up);
fn->scope_cap = ns;
fn->scope_fn = xrealloc(fn->scope_fn, (size_t)ns * sizeof *fn->scope_fn);
fn->scope_cline = xrealloc(fn->scope_cline, (size_t)ns * sizeof *fn->scope_cline);
fn->scope_ccol = xrealloc(fn->scope_ccol, (size_t)ns * sizeof *fn->scope_ccol);
if (!had)
for (int t = 0; t < base; t++) {
fn->scope_fn[t] = NULL;
fn->scope_cline[t] = fn->scope_ccol[t] = 0;
}
for (int t = 0; t < cf->nscopes; t++) {
int id = base + t;
if (t == 0) {
fn->scope_up[id] = call.scope;
fn->scope_fn[id] = cf->src;
fn->scope_cline[id] = call.line;
fn->scope_ccol[id] = call.col;
} else {
fn->scope_up[id] = base + cf->scope_up[t];
fn->scope_fn[id] = cf->scope_fn ? cf->scope_fn[t] : NULL;
fn->scope_cline[id] = cf->scope_cline ? cf->scope_cline[t] : 0;
fn->scope_ccol[id] = cf->scope_ccol ? cf->scope_ccol[t] : 0;
}
}
fn->nscopes = ns;
}
for (int k = 0; k < m; k++)
buf[k].scope = base < 0 ? call.scope
: base + (buf[k].scope > 0 && buf[k].scope < cf->nscopes
? buf[k].scope : 0);

/* 3. Splice buf over the call. */
int newn = fn->nins - 1 + m;
struct ir_ins *ni = xmalloc((size_t)newn * sizeof *ni);
Expand Down Expand Up @@ -655,6 +700,30 @@ static void inline_call(struct ir_func *fn, int ci, struct ir_func *cf)
fn->var_scope_lo = lo; fn->var_scope_hi = hi;
}
fn->nvars = nv;

/* -g: the callee's parameters and locals are the caller's slots V + k
* now, so the caller's own mem2reg follows them (vreg_var) and they get
* location lists like any local; each in its inlined scope, its origin
* the callee's variable (or, inlined into the callee before, that
* one's own). */
if (base >= 0)
for (int d = 0; d < cf->ndbgvars; d++) {
struct ir_dbgvar x = cf->dbgvars[d];
if (x.vreg < 0 || x.vreg >= v)
continue;
x.vreg = V + x.vreg;
x.scope = base + (x.scope > 0 && x.scope < cf->nscopes ? x.scope : 0);
if (!x.origin) {
x.origin = cf->src;
x.origin_idx = d;
}
if (fn->ndbgvars == fn->dbgvarcap) {
fn->dbgvarcap = fn->dbgvarcap ? fn->dbgvarcap * 2 : 8;
fn->dbgvars = xrealloc(fn->dbgvars,
(size_t)fn->dbgvarcap * sizeof *fn->dbgvars);
}
fn->dbgvars[fn->ndbgvars++] = x;
}
}

/* How many calls in the whole unit name this function, and does its
Expand Down
3 changes: 3 additions & 0 deletions src/parse/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ struct asm_stmt {

struct stmt {
enum stmt_kind kind;
/* -g: 1 + the lexical scope irgen gave this block or for statement
* (ir_func.scope_up), 0 when it declares no local of its own */
int dbg_scope;
int line;
int col; /* 1-based column, for caret diagnostics (0 = unknown) */
const char *name; /* STMT_DECL */
Expand Down
Loading
Loading