dymokomi/luce-base
Source of release 0.44.0
0.44.0 / src / back / native / dwarf.lucb
#==============================================================================================
#
# dwarf - DWARF 4 debugging information
#
# DESCRIPTION:
# For development and `--release --debug` builds. Addresses and frame offsets come from
# the native generator; types and member offsets come from the checker's target layout.
#
#==============================================================================================
import back.target as targets
import back.native.frame
import back.ir.ir
import front.ast
import sema.check
import sema.types
import support.buffer
import support.in_arena
import support.list
import strings
## Emits a unit's DWARF: the line table, the function DIEs, and the sections they need.
pub struct Emitter:
var checker: check.Checker*
var program: ir.Unit*
var os: targets.Os
var arch: targets.Arch
## The line and file of the last `.loc` of an optimised function (`--release --debug`).
var last_line: u32
var last_file: u32
## The scope the last instruction of an optimised function belonged to, and each
## instruction where that changed with the scope it changed to: the runs a lexical
## block's ranges are made of.
var last_scope: u32
var last_site: u32
var changes: list.List[u32]
var change_scopes: list.List[u32]
var change_sites: list.List[u32]
## The callees the inlined subroutines stand for, one abstract subprogram each.
var origins: list.List[ir.InlineSite]
## The `.debug_ranges` entries of the lexical blocks, ahead of the section.
var ranges: buffer.Buffer
## The `.debug_loc` lists of the locals in temporaries, ahead of the section.
var locs: buffer.Buffer
## Which instructions of the optimised function get a `Ldw_v` label: each `dbg`, and
## where the life of a temporary a `dbg` names ends.
var labelled: list.List[u8]
var files: list.List[str]
var needed: list.List[u32]
var info: buffer.Buffer
var cwd: str
var serial: usize
## A DWARF emitter for `program` on `os` and `arch`, reading types from `checker`.
pub static func over(checker: check.Checker*, program: ir.Unit*, os: targets.Os, arch: targets.Arch = targets.Arch.arm64) -> Emitter!:
return Emitter(checker = checker, program = program, os = os, arch = arch, last_line = 0, last_file = 0,
last_scope = 0, last_site = 0, changes = try list.List[u32].create(0), change_scopes = try list.List[u32].create(0),
change_sites = try list.List[u32].create(0), origins = try list.List[ir.InlineSite].create(0),
ranges = try buffer.Buffer.create(0), locs = try buffer.Buffer.create(0), labelled = try list.List[u8].create(0),
files = try list.List[str].create(0), needed = try list.List[u32].create(0),
info = try buffer.Buffer.create(0), cwd = "", serial = 0)
func file(path: str) -> usize!:
var i: usize = 0
while i < self.files.count:
if self.files.get(i) == path:
return i + 1
i += 1
try self.files.push(path)
return self.files.count
# mark: The line table ====================================================================
## Emit the line-table header, declaring every source file the unit names.
pub func begin(out: buffer.Buffer*) -> !:
var format_room: u8[4096]
self.cwd = try in_arena.canonical(".")
try section(out, "line", self.os)
try out.put("Ldw_line:\n")
var i: usize = 0
while i < self.program.functions.count:
let f = &self.program.functions.items[i]
if f.debug or f.symbols:
_ = try self.file(f.source)
for point in f.points.span():
_ = try self.file(point.file)
for site in f.sites.span():
_ = try self.file(site.source)
if site.call_file != 0:
_ = try self.file(self.program.files.get((usize)site.call_file - 1))
i += 1
# the files the optimised functions' instructions name (`--release --debug`)
for path in self.program.files.span():
_ = try self.file(path)
i = 0
while i < self.files.count:
try out.put((try strings.format(format_room, f"\n .file {i + 1} ")))
try quoted(out, self.files.get(i))
try out.put("\n")
i += 1
## Emit the `.loc` that opens a function's line program.
pub func entry(out: buffer.Buffer*) -> !:
if self.files.count > 0:
try out.put("\n .loc 1 0 0 is_stmt 0\n")
## A line for code of an optimised function's instruction `x` written out of its place
## (a check's trap after the body), which the line table then names `x`'s line.
pub func line_of(out: buffer.Buffer*, f: ir.Function*, x: ir.Instr) -> !:
if not f.symbols or x.line == 0:
return
var format_room: u8[4096]
let path = self.program.files.get((usize)x.file - 1) if x.file != 0 else f.source
let file = try self.file(path)
try out.put(try strings.format(format_room, f"\n .loc {file} {x.line} 0 is_stmt 1"))
## Emit the line, and for an optimised function the scope and value labels, at `instruction`.
pub func mark(out: buffer.Buffer*, f: ir.Function*, fr: frame.Frame*, index: usize, instruction: usize) -> !:
var format_room: u8[4096]
if f.symbols:
# the optimised function: a line per instruction where the source changes
if instruction == 0:
let file = try self.file(f.source)
try out.put(try strings.format(format_room, f"\n .loc {file} {f.source_line} 0 prologue_end is_stmt 1"))
self.last_line = 0
self.last_file = 0
self.last_scope = 0
self.last_site = 0
self.changes.count = 0
self.change_scopes.count = 0
self.change_sites.count = 0
try self.plan_labels(f, fr)
if instruction < f.instrs.count:
let x = f.instrs.get(instruction)
if self.labelled.get(instruction) != 0:
try out.put(try strings.format(format_room, f"\nLdw_v_{index}_{instruction}:"))
# a label where the scope or the inlined subroutine changes; an instruction a
# pass made (no line, no scope, no site) belongs to where it lies
if (x.scope != self.last_scope or x.site != self.last_site) and (x.scope != 0 or x.site != 0 or x.line != 0):
try out.put(try strings.format(format_room, f"\nLdw_sc_{index}_{instruction}:"))
try self.changes.push(u32(instruction))
try self.change_scopes.push(x.scope)
try self.change_sites.push(x.site)
self.last_scope = x.scope
self.last_site = x.site
if x.line != 0 and (x.line != self.last_line or x.file != self.last_file):
let path = self.program.files.get((usize)x.file - 1) if x.file != 0 else f.source
let file = try self.file(path)
try out.put(try strings.format(format_room, f"\n .loc {file} {x.line} 0 is_stmt 1"))
self.last_line = x.line
self.last_file = x.file
return
if not f.debug:
return
try out.put((try strings.format(format_room, f"\nLdw_pc_{index}_{instruction}:\n")))
if instruction == 0:
let file = try self.file(f.source)
try out.put(try strings.format(format_room, f" .loc {file} {f.source_line} 0 prologue_end is_stmt 1\n"))
for point in f.points.span():
if point.instruction == instruction:
let file = try self.file(point.file)
try out.put((try strings.format(format_room, f" .loc {file} {point.line} {point.column} is_stmt 1\n")))
# mark: Function DIEs =====================================================================
## Emit the debugging information entry for function `f`: its scopes, locals and types.
pub func function(f: ir.Function*, fr: frame.Frame*, index: usize) -> !:
var format_room: u8[4096]
if not (f.debug or f.symbols) or f.naked:
return
let file = try self.file(f.source)
let o = &self.info
try o.put(" .uleb128 2\n")
try text(o, f.source_name)
try text(o, f.name)
try o.put((try strings.format(format_room, f" .uleb128 {file}\n .uleb128 {f.source_line}\n .quad ")))
try o.put("_" if (self.os == targets.Os.macos) else "")
try o.put(f.name)
try o.put(try strings.format(format_room, f"\n .quad Ldw_end_{index}\n"))
if self.os == targets.Os.windows:
# rbp points at the allocated frame's bottom on Windows. Present the
# logical frame top so shared DW_OP_fbreg locations retain their meaning.
try o.put(try strings.format(format_room, f" .uleb128 Ldw_base_end_{index}-Ldw_base_{index}\nLdw_base_{index}:\n .byte 118\n .sleb128 {fr.size}\nLdw_base_end_{index}:\n"))
else:
# the frame register by architecture: arm64's x29 is DWARF 29, x86-64's rbp is 6
try o.put(" .uleb128 1\n .byte ")
try o.put("109\n" if self.arch == targets.Arch.arm64 else "86\n") # DW_OP_reg29 / DW_OP_reg6
try self.reference(f.result_type)
try self.scope(f, fr, index, 0, 0)
try o.put(" .byte 0\n")
## Which instructions get a `Ldw_v` label: each `dbg`, and the instruction where the
## life of the temporary it names ends, when that is later.
func plan_labels(f: ir.Function*, fr: frame.Frame*) -> !:
self.labelled.count = 0
var i: usize = 0
while i <= f.instrs.count:
try self.labelled.push(0)
i += 1
i = 0
while i < f.instrs.count:
let x = f.instrs.get(i)
if x.op == ir.Op.dbg:
self.labelled.set(i, 1)
if x.a.kind == ir.RefKind.temp:
let e = life_end_index(fr, x.a.index)
if e > i and e < f.instrs.count:
self.labelled.set(e, 1)
i += 1
## The location list of local `k` of the optimised function `index`: an entry from each
## `dbg` of it to the next, or to the end of the life of the temporary it names.
## Whether the local has any.
func location_list(f: ir.Function*, fr: frame.Frame*, index: usize, k: usize) -> bool!:
var format_room: u8[4096]
var any = false
var i: usize = 0
while i < f.instrs.count:
let x = f.instrs.get(i)
if x.op == ir.Op.dbg and (usize)x.extra == k:
var stop = f.instrs.count
var j = i + 1
while j < f.instrs.count:
let y = f.instrs.get(j)
if y.op == ir.Op.dbg and (usize)y.extra == k:
stop = j
break
j += 1
if try self.location_entry(f, fr, index, x.a, i, stop, any):
any = true
i += 1
if any:
try self.locs.put(" .quad 0, 0\n")
return any
## One entry: `value` from instruction `at` to `stop`, the register or frame home of a
## temporary while it lives, the address of a slot, or a constant. Whether one was written.
func location_entry(f: ir.Function*, fr: frame.Frame*, index: usize, value: ir.Ref, at: usize, stop: usize, started: bool) -> bool!:
var format_room: u8[4096]
var until = stop
var expression: u8[24]
var length: usize = 0
if value.kind == ir.RefKind.constant:
expression[0] = 17 # DW_OP_consts
length = 1 + sleb(expression[1..], value.value)
expression[length] = 159 # DW_OP_stack_value
length += 1
elif value.kind == ir.RefKind.temp:
let slot = fr.slot_of(value)
if slot != 0:
let slot_home = fr.slot_home(slot - 1)
if slot_home == 0:
return false
expression[0] = 145 # DW_OP_fbreg
length = 1 + sleb(expression[1..], -(i64)slot_home)
expression[length] = 159
length += 1
else:
let e = life_end_index(fr, value.index)
if e <= at:
return false
if e < until:
until = e
let register = fr.temp_regs.get((usize)value.index - 1)
if register != 0:
let number = dwarf_register(self.arch, self.os, register)
if number < 32:
expression[0] = (u8)(80 + number) # DW_OP_reg0 + n
length = 1
else:
expression[0] = 144 # DW_OP_regx
length = 1 + uleb(expression[1..], (u64)number)
else:
let temp_home = fr.temp_home(value.index)
if temp_home == 0:
return false
expression[0] = 145
length = 1 + sleb(expression[1..], -(i64)temp_home)
else:
return false
if not started:
try self.locs.put((try strings.format(format_room, f"Ldw_ll_{self.serial}:\n")))
self.serial += 1
try self.locs.put((try strings.format(format_room, f" .quad Ldw_v_{index}_{at}, ")))
if until < f.instrs.count:
try self.locs.put((try strings.format(format_room, f"Ldw_v_{index}_{until}\n")))
else:
try self.locs.put((try strings.format(format_room, f"Ldw_end_{index}\n")))
try self.locs.put((try strings.format(format_room, f" .short {length}\n .byte ")))
var b: usize = 0
while b < length:
if b > 0:
try self.locs.put(",")
try self.locs.put_u64((u64)expression[b])
b += 1
try self.locs.put("\n")
return true
## The locals of `scope` in inlined subroutine `site` (0 for the function's own body),
## then the scopes and sites within it. A development build's block spans its
## instructions' labels; an optimised build's covers the runs its instructions kept,
## and a scope none kept describes its locals in its parent. An inlined subroutine
## none kept is not described at all.
func scope(f: ir.Function*, fr: frame.Frame*, index: usize, scope: usize, site: u32) -> !:
var format_room: u8[4096]
let o = &self.info
var block = scope != 0 or site != 0
if block and f.symbols:
block = try self.scope_ranges(f, index, scope, site)
if not block and scope == 0:
return
if block and scope != 0:
try o.put(" .uleb128 20\n")
try range_offset(o, (try strings.format(format_room, f"Ldw_rs_{index}_{scope}_{site}")), "Ldw_ranges", self.os)
elif block:
let where = f.sites.get((usize)site - 1)
let origin = try self.origin(where)
let call_file = try self.file(self.program.files.get((usize)where.call_file - 1) if where.call_file != 0 else f.source)
try o.put((try strings.format(format_room, f" .uleb128 21\n .long Ldw_abstract_{origin}-Ldw_info\n")))
try range_offset(o, (try strings.format(format_room, f"Ldw_rs_{index}_{scope}_{site}")), "Ldw_ranges", self.os)
try o.put((try strings.format(format_room, f" .uleb128 {call_file}\n .uleb128 {where.call_line}\n")))
elif block:
let s = f.debug_scopes.get(scope)
try o.put((try strings.format(format_room, f" .uleb128 5\n .quad Ldw_pc_{index}_{s.start}\n .quad Ldw_pc_{index}_{s.end}\n")))
var k: usize = 0
while k < f.locals.count:
let described = f.locals.get(k)
k += 1
if described.scope != scope or described.site != site:
continue
let home = fr.slot_home(described.slot)
let register = fr.slot_register(described.slot)
if f.symbols and home == 0 and register == 0:
# the optimiser lifted the local out of its slot: where its temporaries live,
# or no location when nothing of it survived, as C compilers say it
let list_label = self.serial
if try self.location_list(f, fr, index, k - 1):
try o.put(" .uleb128 23\n" if described.parameter else " .uleb128 24\n")
try text(o, described.name)
try self.reference(described.type_id)
try o.put((try strings.format(format_room, f" .uleb128 {described.line}\n")))
try range_offset(o, (try strings.format(format_room, f"Ldw_ll_{list_label}")), "Ldw_loc", self.os)
else:
try o.put(" .uleb128 3\n" if described.parameter else " .uleb128 4\n")
try text(o, described.name)
try self.reference(described.type_id)
try o.put((try strings.format(format_room, f" .uleb128 {described.line}\n .uleb128 0\n")))
continue
try o.put(" .uleb128 3\n" if described.parameter else " .uleb128 4\n")
try text(o, described.name)
try self.reference(described.type_id)
try o.put((try strings.format(format_room, f" .uleb128 {described.line}\n")))
let serial = self.serial
self.serial += 1
if register != 0:
# a promoted local lives in a callee-saved register for the whole function
let number = dwarf_register(self.arch, self.os, register)
if number < 32:
try o.put((try strings.format(format_room, f" .uleb128 1\n .byte {80 + number}\n")))
else:
try o.put((try strings.format(format_room, f" .uleb128 Ldw_expr_end_{serial}-Ldw_expr_{serial}\nLdw_expr_{serial}:\n .byte 144\n .uleb128 {number}\nLdw_expr_end_{serial}:\n")))
else:
try o.put((try strings.format(format_room, f" .uleb128 Ldw_expr_end_{serial}-Ldw_expr_{serial}\nLdw_expr_{serial}:\n .byte 145\n .sleb128 -{home}\nLdw_expr_end_{serial}:\n")))
var i: usize = 1
while i < f.debug_scopes.count:
let inner = f.debug_scopes.get(i)
if inner.parent == scope and inner.site == site and i != scope:
try self.scope(f, fr, index, i, site)
i += 1
var j: usize = 0
while j < f.sites.count:
let inner = f.sites.get(j)
if (usize)inner.scope == scope and inner.parent == site:
try self.scope(f, fr, index, 0, u32(j + 1))
j += 1
if block:
try o.put(" .byte 0\n")
## The abstract subprogram an inlined subroutine refers to, by the callee's name; its
## index among `origins`.
func origin(site: ir.InlineSite) -> usize!:
var i: usize = 0
while i < self.origins.count:
if self.origins.get(i).name == site.name and self.origins.get(i).source == site.source:
return i
i += 1
try self.origins.push(site)
return self.origins.count - 1
## The `.debug_ranges` entry of `scope` in site `site` of the optimised function `index`:
## each run of instructions in it or below it, from its first change point to the one
## that leaves it, or the function's end. Whether it kept any.
func scope_ranges(f: ir.Function*, index: usize, scope: usize, site: u32) -> bool!:
var format_room: u8[4096]
var any = false
var open = false
var k: usize = 0
while k < self.changes.count:
let inside = within(f, self.change_scopes.get(k), self.change_sites.get(k), scope, site)
if inside and not open:
if not any:
try self.ranges.put((try strings.format(format_room, f"Ldw_rs_{index}_{scope}_{site}:\n")))
any = true
try self.ranges.put((try strings.format(format_room, f" .quad Ldw_sc_{index}_{self.changes.get(k)}, ")))
open = true
elif not inside and open:
try self.ranges.put((try strings.format(format_room, f"Ldw_sc_{index}_{self.changes.get(k)}\n")))
open = false
k += 1
if open:
try self.ranges.put((try strings.format(format_room, f"Ldw_end_{index}\n")))
if any:
try self.ranges.put(" .quad 0, 0\n")
return any
func reference(id: u32) -> !:
var format_room: u8[4096]
let actual = types.void_id if id == 0 or id == types.unit_id or id == types.never_id else id
var found = false
for item in self.needed.span():
if item == actual:
found = true
break
if not found:
try self.needed.push(actual)
try self.info.put((try strings.format(format_room, f" .long Ldw_type_{actual}-Ldw_info\n")))
func member(name: str, id: u32, offset: u64) -> !:
var format_room: u8[4096]
try self.info.put(" .uleb128 9\n")
try text(&self.info, name)
try self.reference(id)
try self.info.put((try strings.format(format_room, f" .uleb128 {offset}\n")))
## The entry `Ldw_type_<id>` for type `id`: a base type, a pointer, an alias, an array,
## a signature, an enumeration, or a record of members.
func type_info(id: u32) -> !:
var format_room: u8[4096]
let checker = self.checker
let t = checker.table.get(id)
let o = &self.info
let name = try checker.spell(id)
let size = checker.table.size_of(id)
try o.put((try strings.format(format_room, f"Ldw_type_{id}:\n")))
if t.kind == types.Kind.void_:
try o.put(" .uleb128 16\n")
try text(o, name)
return
if checker.table.is_integer(id) or checker.table.is_float(id) or id == types.bool_id or id == types.char_id or id == types.error_code_id:
let encoding: u32 = 4 if checker.table.is_float(id) else (2 if id == types.bool_id else (5 if checker.table.is_signed(id) else 7))
try o.put(" .uleb128 6\n")
try text(o, name)
try o.put((try strings.format(format_room, f" .byte {encoding}\n .uleb128 {size}\n")))
return
if t.kind == types.Kind.pointer or t.kind == types.Kind.cstr_ or checker.table.is_opaque_handle(id):
try o.put(" .uleb128 7\n .byte 8\n")
try self.reference(types.u8_id if t.kind == types.Kind.cstr_ else (types.void_id if checker.table.is_opaque_handle(id) else t.elem))
return
if t.kind == types.Kind.atomic or (t.kind == types.Kind.optional and checker.table.has_null_niche(t.elem)):
try o.put(" .uleb128 10\n")
try text(o, name)
try self.reference(t.elem)
return
if t.kind == types.Kind.array:
try o.put(" .uleb128 11\n")
try self.reference(t.elem)
try o.put((try strings.format(format_room, f" .uleb128 12\n .uleb128 {t.length}\n .byte 0\n")))
return
if t.kind == types.Kind.func_:
try o.put((try strings.format(format_room, f" .uleb128 7\n .byte 8\n .long Ldw_signature_{id}-Ldw_info\nLdw_signature_{id}:\n .uleb128 13\n")))
try self.reference(t.elem)
for param in t.args:
try o.put(" .uleb128 14\n")
try self.reference(param)
try o.put(" .byte 0\n")
return
if t.kind == types.Kind.enum_:
if let decl = t.decl:
if let backing = decl.ty:
try self.backed_enum(decl, backing.type_id, name, size)
return
try o.put(" .uleb128 15\n" if t.kind == types.Kind.union_ else " .uleb128 8\n")
try text(o, name)
try o.put((try strings.format(format_room, f" .uleb128 {size}\n")))
if t.kind == types.Kind.enum_:
try self.payload_enum(id)
return
try self.members(id, t)
try o.put(" .byte 0\n")
## An enum with a backing type: an enumeration of its cases' names and values over
## the backing.
func backed_enum(decl: ast.Node*, backing: u32, name: str, size: u64) -> !:
var format_room: u8[4096]
let o = &self.info
try o.put(" .uleb128 17\n")
try text(o, name)
try o.put((try strings.format(format_room, f" .uleb128 {size}\n")))
try self.reference(backing)
var cursor = decl.body
while let item = cursor:
if item.kind == ast.Kind.enum_case:
# a signed backing's cases are `sdata` (abbreviation 19)
let signed_backing = self.checker.table.is_signed(backing)
try o.put(" .uleb128 19\n" if signed_backing else " .uleb128 18\n")
try text(o, item.text)
let case_bits = types.case_value(decl, item)
if signed_backing:
try o.put((try strings.format(format_room, f" .sleb128 {(i64)case_bits}\n")))
else:
try o.put((try strings.format(format_room, f" .uleb128 {case_bits}\n")))
cursor = item.next
try o.put(" .byte 0\n")
## The members of the record-shaped type `id`: a struct's or a union's fields, and the
## C layout of text, spans, interface views, tuples, optionals, results, and errors.
func members(id: u32, t: types.Type) -> !:
var format_room: u8[4096]
let checker = self.checker
if t.kind == types.Kind.struct_ or t.kind == types.Kind.union_:
if let decl = t.decl:
var cursor = decl.body
while let field = cursor:
if field.kind == ast.Kind.field:
let ft = try checker.substitute(field.type_id, decl, t.args)
try self.member(field.text, ft, 0 if t.kind == types.Kind.union_ else checker.table.offset_of(id, field.text))
cursor = field.next
elif t.kind == types.Kind.str_ or t.kind == types.Kind.fmt_ or t.kind == types.Kind.span:
let element = t.elem if t.kind == types.Kind.span else types.u8_id
try self.member("data", try checker.table.pointer(element, t.is_const, false), 0)
try self.member("length", types.usize_id, 8)
elif t.kind == types.Kind.interface_:
let pointer = try checker.table.pointer(types.void_id, false, false)
try self.member("data", pointer, 0)
try self.member("vtable", pointer, 8)
elif t.kind == types.Kind.tuple:
var offset: u64 = 0
var index: usize = 0
for member in t.args:
offset = round_up(offset, checker.table.align_of(member))
try self.member((try strings.format(format_room, f"_{index}")), member, offset)
offset += checker.table.size_of(member)
index += 1
elif t.kind == types.Kind.optional:
try self.member("value", t.elem, 0)
try self.member("present", types.bool_id, checker.table.size_of(t.elem))
elif t.kind == types.Kind.fallible:
let payload = checker.table.size_of(t.elem)
let error_at = round_up(payload, 8)
if payload > 0:
try self.member("value", t.elem, 0)
try self.member("error", types.error_value_id, error_at)
try self.member("failed", types.bool_id, error_at + 24)
elif t.kind == types.Kind.error_value:
try self.member("code", types.error_code_id, 0)
try self.member("at", types.i32_id, 4)
# the message's data and its 32-bit length, `called_at` above it (§11.3)
try self.member("message", try checker.table.pointer(types.u8_id, true, false), 8)
try self.member("length", types.u32_id, 16)
try self.member("called_at", types.i32_id, 20)
## A payload enum (§6.1): its `tag`, an enumeration of the case names in case order, and
## `payload`, a union of one record per case with a payload, at the offset the lowerer
## lays the payloads at (`payload_offset`). The debuggers read that C shape as it is.
func payload_enum(id: u32) -> !:
var format_room: u8[4096]
let checker = self.checker
let t = checker.table.get(id)
let o = &self.info
let size = checker.table.size_of(id)
if t.decl == none:
try o.put(" .byte 0\n")
return
let decl = t.decl else return
var widest: u64 = 4
var with_payload = false
var cursor = decl.body
while let c = cursor:
if c.kind == ast.Kind.enum_case:
var q = c.body
while let param = q:
let pt = try checker.substitute(param.type_id, decl, t.args)
let a = checker.table.align_of(pt)
widest = a if a > widest else widest
with_payload = true
q = param.next
cursor = c.next
let base = round_up(4, widest)
try self.member_of("tag", (try strings.format(format_room, f"Ldw_tag_{id}")), 0)
if with_payload and size > base:
try self.member_of("payload", (try strings.format(format_room, f"Ldw_payload_{id}")), base)
try o.put(" .byte 0\n")
# the tag's enumeration
try o.put((try strings.format(format_room, f"Ldw_tag_{id}:\n .uleb128 17\n")))
try text(o, "tag")
try o.put(" .uleb128 4\n")
try self.reference(types.u32_id)
var index: u64 = 0
cursor = decl.body
while let c = cursor:
if c.kind == ast.Kind.enum_case:
try o.put(" .uleb128 18\n")
try text(o, c.text)
try o.put((try strings.format(format_room, f" .uleb128 {index}\n")))
index += 1
cursor = c.next
try o.put(" .byte 0\n")
if not (with_payload and size > base):
return
# the union of the cases' records, then each record
try o.put((try strings.format(format_room, f"Ldw_payload_{id}:\n .uleb128 15\n")))
try text(o, "payload")
try o.put((try strings.format(format_room, f" .uleb128 {size - base}\n")))
index = 0
cursor = decl.body
while let c = cursor:
if c.kind == ast.Kind.enum_case:
if c.body != none:
try self.member_of(c.text, (try strings.format(format_room, f"Ldw_case_{id}_{index}")), 0)
index += 1
cursor = c.next
try o.put(" .byte 0\n")
index = 0
cursor = decl.body
while let c = cursor:
if c.kind == ast.Kind.enum_case:
if c.body != none:
var offset: u64 = 0
var q = c.body
while let param = q:
let pt = try checker.substitute(param.type_id, decl, t.args)
offset = round_up(offset, checker.table.align_of(pt))
offset += checker.table.size_of(pt)
q = param.next
try o.put((try strings.format(format_room, f"Ldw_case_{id}_{index}:\n .uleb128 8\n")))
try text(o, c.text)
try o.put((try strings.format(format_room, f" .uleb128 {offset}\n")))
offset = 0
q = c.body
while let param = q:
let pt = try checker.substitute(param.type_id, decl, t.args)
offset = round_up(offset, checker.table.align_of(pt))
try self.member(param.text, pt, offset)
offset += checker.table.size_of(pt)
q = param.next
try o.put(" .byte 0\n")
index += 1
cursor = c.next
## A member whose type is a DIE of this emitter's own, by label.
func member_of(name: str, label: str, offset: u64) -> !:
var format_room: u8[4096]
try self.info.put(" .uleb128 9\n")
try text(&self.info, name)
try self.info.put((try strings.format(format_room, f" .long {label}-Ldw_info\n .uleb128 {offset}\n")))
# mark: The sections ======================================================================
## Emit the abbreviation, info, ranges and location sections, after every function.
pub func finish(out: buffer.Buffer*) -> !:
var format_room: u8[4096]
if self.files.count == 0:
return
var i: usize = 0
while i < self.origins.count:
# the callees the inlined subroutines stand for, by name; their types may add
# to `needed`, so before the types
let origin = self.origins.get(i)
let file = try self.file(origin.source)
try self.info.put((try strings.format(format_room, f"Ldw_abstract_{i}:\n .uleb128 22\n")))
try text(&self.info, origin.name)
try self.info.put((try strings.format(format_room, f" .uleb128 {file}\n .uleb128 {origin.line}\n .byte 1\n")))
try self.reference(origin.result_type)
i += 1
i = 0
while i < self.needed.count:
try self.type_info(self.needed.get(i))
i += 1
try section(out, "abbrev", self.os)
try out.put("Ldw_abbrev:\n")
try out.put(abbreviations)
try section(out, "info", self.os)
try out.put("Ldw_info:\n .long Ldw_info_end-Ldw_info-4\n .short 4\n")
# Mach-O's debug linker rebases each object's offsets. ELF concatenates
# sections, so offsets into other sections must carry relocations.
try section_offset(out, "Ldw_abbrev", self.os)
try out.put(" .byte 8\n .uleb128 1\n")
try text(out, "luce-base native DWARF 4")
try out.put(" .short 12\n") # C99 representation understood by both debuggers
try text(out, self.files.get(0))
try text(out, self.cwd)
try section_offset(out, "Ldw_line", self.os)
try section_offset(out, "Ldw_ranges", self.os)
try out.put(" .quad 0\n") # statement list and ranges, section offsets
try out.put(self.info.view())
try out.put(" .byte 0\nLdw_info_end:\n")
try section(out, "ranges", self.os)
try out.put("Ldw_ranges:\n")
i = 0
while i < self.program.functions.count:
let f = &self.program.functions.items[i]
if (f.debug or f.symbols) and not f.naked:
try out.put(" .quad ")
try out.put("_" if (self.os == targets.Os.macos) else "")
try out.put(f.name)
try out.put((try strings.format(format_room, f", Ldw_end_{i}\n")))
i += 1
try out.put(" .quad 0, 0\n")
try out.put(self.ranges.view())
try section(out, "loc", self.os)
try out.put("Ldw_loc:\n")
try out.put(self.locs.view())
## Whether scope `inner` of site `inner_site` is `outer` of `outer_site` or lies within it:
## up the scopes of one site, then out through the site's call to the scope it was in.
func within(f: ir.Function*, inner: u32, inner_site: u32, outer: usize, outer_site: u32) -> bool:
var s = (usize)inner
var k = inner_site
while true:
if s == outer and k == outer_site:
return true
if s != 0:
s = f.debug_scopes.get(s).parent
elif k != 0:
let site = f.sites.get((usize)k - 1)
s = (usize)site.scope
k = site.parent
else:
return false
# mark: Helpers ===============================================================================
func round_up(n: u64, alignment: u64) -> u64:
return (n + alignment - 1) // alignment * alignment if alignment > 0 else n
## Debug sections are non-allocating ELF sections, Mach-O debug sections, or
## discardable read-only COFF sections. Cross-section references use the matching relocation.
func section(out: buffer.Buffer*, name: str, os: targets.Os) -> !:
var format_room: u8[4096]
if os == targets.Os.macos:
try out.put(try strings.format(format_room, f"\n .section __DWARF,__debug_{name},regular,debug\n"))
elif os == targets.Os.windows:
try out.put(try strings.format(format_room, f"\n .section .debug_{name},\"dr\"\n"))
else:
try out.put(try strings.format(format_room, f"\n .section .debug_{name},\"\",@progbits\n"))
func section_offset(out: buffer.Buffer*, label: str, os: targets.Os) -> !:
if os == targets.Os.macos:
try out.put(" .long 0\n")
return
try out.put(" .secrel32 " if os == targets.Os.windows else " .long ")
try out.put(label)
try out.put("\n")
## A section offset (`DW_AT_ranges`, `DW_AT_location` as a list) to `label` in the section
## that starts at `base`: Mach-O's debug linker rebases each object's sections, so the
## offset is from the section's start; ELF and COFF relocate the label.
func range_offset(out: buffer.Buffer*, label: str, base: str, os: targets.Os) -> !:
if os == targets.Os.macos:
try out.put(" .long ")
try out.put(label)
try out.put("-")
try out.put(base)
try out.put("\n")
return
try out.put(" .secrel32 " if os == targets.Os.windows else " .long ")
try out.put(label)
try out.put("\n")
func text(out: buffer.Buffer*, value: str) -> !:
try out.put(" .asciz ")
try quoted(out, value)
try out.put("\n")
func quoted(out: buffer.Buffer*, value: str) -> !:
try out.put("\"")
for byte in value.bytes:
if byte == 34 or byte == 92:
try out.put("\\")
try out.put_byte(byte)
elif byte < 32 or byte >= 127:
try out.put("\\")
try out.put_byte(48 + byte // 64)
try out.put_byte(48 + byte // 8 % 8)
try out.put_byte(48 + byte % 8)
else:
try out.put_byte(byte)
try out.put("\"")
## Each row is code, tag, children, then (attribute, form) pairs and a zero pair.
## DW_FORM: addr=1, data2=5, data1=11, string=8, udata=15, ref4=19,
## sec_offset=23, exprloc=24. See dwarfstd.org/doc/DWARF4.pdf, sections 7.5–7.7.
let abbreviations: str = r"""
.byte 1,17,1,37,8,19,5,3,8,27,8,16,23,85,23,17,1,0,0
.byte 2,46,1,3,8
.uleb128 110
.byte 8,58,15,59,15,17,1,18,1,64,24,73,19,0,0
.byte 3,5,0,3,8,73,19,59,15,2,24,0,0
.byte 4,52,0,3,8,73,19,59,15,2,24,0,0
.byte 5,11,1,17,1,18,1,0,0
.byte 6,36,0,3,8,62,11,11,15,0,0
.byte 7,15,0,11,11,73,19,0,0
.byte 8,19,1,3,8,11,15,0,0
.byte 9,13,0,3,8,73,19,56,15,0,0
.byte 10,22,0,3,8,73,19,0,0
.byte 11,1,1,73,19,0,0
.byte 12,33,0,55,15,0,0
.byte 13,21,1,73,19,0,0
.byte 14,5,0,73,19,0,0
.byte 15,23,1,3,8,11,15,0,0
.byte 16,59,0,3,8,0,0
.byte 17,4,1,3,8,11,15,73,19,0,0
.byte 18,40,0,3,8,28,15,0,0
.byte 19,40,0,3,8,28,13,0,0
.byte 20,11,1,85,23,0,0
.byte 21,29,1,49,19,85,23,88,15,89,15,0,0
.byte 22,46,0,3,8,58,15,59,15,32,11,73,19,0,0
.byte 23,5,0,3,8,73,19,59,15,2,23,0,0
.byte 24,52,0,3,8,73,19,59,15,2,23,0,0
.byte 0
"""
## The DWARF register number of an allocator code, by the generators' name tables: arm64's
## x19–x28 (codes 1–10) are 19–28, d8–d15 (11–18) are 72–79, the pooled x14, x15, x12, x13
## and x7 down to x0 (19–30) are their numbers, d24–d31 (31–38) are 88–95, and d19–d23
## (39–43) are 83–87, the d registers' numbers being 64 more than their own. x86-64's rbx
## is 3 and r12–r15 are 12–15 on both conventions, xmmN is 17 + N; under System V r8, r9,
## rsi, rdi, rdx and rcx (6–11) are 8, 9, 4, 5, 1 and 2, then come xmm8–xmm12, xmm7 down to
## xmm0 and xmm13 (12–25); on Windows xmm6–xmm15 (6–15), r8, r9, rdx, rcx, rsi and rdi
## (16–21), then xmm3 down to xmm0 (22–25).
func dwarf_register(arch: targets.Arch, os: targets.Os, code: u8) -> u32:
if arch == targets.Arch.arm64:
if code <= 10:
return 18 + (u32)code
if code <= 18:
return 61 + (u32)code
if code <= 30:
let pooled: u32[12] = [14, 15, 12, 13, 7, 6, 5, 4, 3, 2, 1, 0]
return pooled[(usize)code - 19]
if code <= 38:
return 57 + (u32)code
return 44 + (u32)code
if code == 1:
return 3
if code <= 5:
return 10 + (u32)code
if os == targets.Os.windows:
if code <= 15:
return 17 + (u32)code
if code <= 21:
let integers: u32[6] = [8, 9, 1, 2, 4, 5]
return integers[(usize)code - 16]
return 17 + 25 - (u32)code
if code <= 11:
let integers: u32[6] = [8, 9, 4, 5, 1, 2]
return integers[(usize)code - 6]
if code <= 16:
return 17 + (u32)code - 4
if code <= 24:
return 17 + 24 - (u32)code
return 17 + 13
## The instruction index the life ending at doubled position `end` of temporary `t` reaches:
## a last read at instruction `i` (position `2i`) keeps the value through the reads of `i`,
## so the entry ends where `i` writes.
func life_end_index(fr: frame.Frame*, t: u32) -> usize:
let end = fr.temp_end(t)
return (usize)((end + 1) // 2)
## `value` as a signed LEB128 into `out`; how many bytes.
func sleb(out: u8[], value: i64) -> usize:
var v = value
var n: usize = 0
while true:
let byte = (u8)(v & 127)
v >>= 7
if (v == 0 and (byte & 64) == 0) or (v == -1 and (byte & 64) != 0):
out[n] = byte
return n + 1
out[n] = byte | 128
n += 1
## `value` as an unsigned LEB128 into `out`; how many bytes.
func uleb(out: u8[], value: u64) -> usize:
var v = value
var n: usize = 0
while true:
let byte = (u8)(v & 127)
v >>= 7
if v == 0:
out[n] = byte
return n + 1
out[n] = byte | 128
n += 1