Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-10 09:45:43 -06:00
commit 8b72f6a52b
33 files changed
+7510

No files matched your search

+250
View File
@@ -0,0 +1,250 @@
#! @file lines.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the writer of a module's lines, as the subsections after its
#! symbols hold them: the files the module's code is from, named in the PDB
#! file's table of names, the lines of each procedure's code, and the file and
#! line each function inlined somewhere begins at. And the annotations an
#! inline site says which of its procedure's code it is with, and the lines of
#! each part.
import std::io;
module binary::pdb;
#! Adds a subsection: its kind, its bytes, then what it holds, padded to four
#! bytes.
#! @param out Where.
#! @param kind Its kind, as `debug_s_lines`.
#! @param data What it holds.
pub def subsection(out: &mut byte_writer, kind: u32, data: span<u8>): void {
out.u32(kind);
out.u32(data.length);
out.put(data);
out.align_to(4);
}
#! The files a module's code is from, as its subsection of checksums names
#! them, each with no checksum.
pub struct file_checksums {
pub var bytes: byte_writer; #! The subsection's contents.
}
#! Makes a subsection naming no files.
#! @returns The subsection.
pub def file_checksums::new(): file_checksums {
return file_checksums { bytes = byte_writer::new() };
}
#! Names a file, giving where its entry is, which lines name the file by.
#! @param this The subsection.
#! @param name Where the file's path is in the PDB file's table of names.
#! @returns Where its entry is.
pub def file_checksums::file(&mut this, name: u32): u32 {
val at := this.bytes.length as u32;
this.bytes.u32(name);
this.bytes.byte(0); # No checksum,
this.bytes.byte(0); # of no kind.
this.bytes.align_to(4);
return at;
}
#! A line of a procedure's code.
struct line_entry {
var offset: u32; #! Where its code begins, from where the procedure's does.
var line: u32;
var column: u16;
}
#! The lines of a procedure's code, as a subsection of lines holds them: each
#! run of lines of one file in a block of its own.
pub struct function_lines {
pub var bytes: byte_writer; #! The subsection's contents.
var file: u32; #! The file of the block being written.
var pending: vector<line_entry>; #! Its lines.
}
#! Begins the lines of a procedure's code.
#! @param section The section holding its code.
#! @param offset Where its code begins in it.
#! @param size The bytes of its code.
#! @returns The lines.
pub def function_lines::new(section: u16, offset: u32, size: u32): function_lines {
var made := function_lines { bytes = byte_writer::new(), file = 0, pending = vector::<line_entry>::new() };
made.bytes.u32(offset);
made.bytes.u16(section);
made.bytes.u16(1); # With columns.
made.bytes.u32(size);
return made;
}
#! Adds a line: the code from an offset is from a line and column of a file.
#! @param this The lines.
#! @param file Where the file's entry is among the checksums.
#! @param offset Where the code begins, from where the procedure's does.
#! @param line The line, from 1.
#! @param column The column, from 1, or 0 for none.
pub def function_lines::line(&mut this, file: u32, offset: u32, line: u32, column: u16): void {
if this.pending.length != 0 && file != this.file {
this.flush();
}
this.file = file;
this.pending.append(line_entry { offset = offset, line = line, column = column });
}
#! Ends the lines, writing the last block.
#! @param this The lines.
pub def function_lines::finish(&mut this): void {
if this.pending.length != 0 {
this.flush();
}
}
# Writes the block of the lines kept, each a statement, then their columns.
def function_lines::flush(&mut this): void {
val count := this.pending.length;
this.bytes.u32(this.file);
this.bytes.u32(count);
this.bytes.u32(12 + 12 * count);
for i in 0..count {
val each: line_entry = this.pending[i];
this.bytes.u32(each.offset);
this.bytes.u32((each.line & 0xFFFFFF) | 0x80000000);
}
for i in 0..count {
val each: line_entry = this.pending[i];
this.bytes.u16(each.column);
this.bytes.u16(0);
}
this.pending.clear();
}
#! The file and line each function inlined somewhere begins at, as the
#! subsection of inlinees' lines holds them.
pub struct inlinee_lines {
pub var bytes: byte_writer; #! The subsection's contents.
pub var count: u64; #! How many functions it names.
}
#! Makes a subsection naming no functions.
#! @returns The subsection.
pub def inlinee_lines::new(): inlinee_lines {
var made := inlinee_lines { bytes = byte_writer::new(), count = 0 };
made.bytes.u32(0); # Each entry naming one file.
return made;
}
#! Names where a function inlined somewhere begins.
#! @param this The subsection.
#! @param inlinee The function's ID.
#! @param file Where the file's entry is among the checksums.
#! @param line The line it is declared at.
pub def inlinee_lines::add(&mut this, inlinee: u32, file: u32, line: u32): void {
this.bytes.u32(inlinee);
this.bytes.u32(file);
this.bytes.u32(line);
this.count++;
}
# Annotations.
# What each annotation does.
const ba_change_code_offset: u32 = 3;
const ba_change_code_length: u32 = 4;
const ba_change_file: u32 = 5;
const ba_change_line_offset: u32 = 6;
const ba_change_code_offset_and_line_offset: u32 = 11;
#! The annotations of an inline site, written as its code is met: each part of
#! the procedure's code the site is, from where the procedure's code begins,
#! and the line of each, from the line the function inlined begins at. Parts
#! of the same line are one.
pub struct annotations {
pub var bytes: byte_writer; #! The annotations.
var file: u32; #! The file of the last part, as its entry among the checksums.
var line: u32; #! Its line.
var at: u32; #! Where the last annotation left the code.
var open: bool; #! Whether the last part is not yet ended.
var begun: bool; #! Whether a part has begun.
}
#! Begins the annotations of a site, from where the function inlined begins.
#! @param file The file it is declared in, as its entry among the checksums.
#! @param line The line it is declared at.
#! @returns The annotations.
pub def annotations::new(file: u32, line: u32): annotations {
return annotations { bytes = byte_writer::new(), file = file, line = line, at = 0, open = false, begun = false };
}
# Adds an annotation and its operand, each in as few bytes as hold it.
def annotations::put(&mut this, value: u32): void {
if value < 0x80 {
this.bytes.byte(value as u8);
} else if value < 0x4000 {
this.bytes.byte(((value >> 8) | 0x80) as u8);
this.bytes.byte((value & 0xFF) as u8);
} else {
this.bytes.byte(((value >> 24) | 0xC0) as u8);
this.bytes.byte(((value >> 16) & 0xFF) as u8);
this.bytes.byte(((value >> 8) & 0xFF) as u8);
this.bytes.byte((value & 0xFF) as u8);
}
}
#! The site's code from an offset is from a line of a file. Where the part
#! before it is from the same line, it goes on.
#! @param this The annotations.
#! @param offset Where the code begins, from where the procedure's does.
#! @param file Its file, as its entry among the checksums.
#! @param line Its line.
pub def annotations::at_line(&mut this, offset: u32, file: u32, line: u32): void {
return if this.open && file == this.file && line == this.line;
this.open = true;
if file != this.file {
this.put(ba_change_file);
this.put(file);
}
val delta := line as i64 - this.line as i64;
val encoded: u32 = delta < 0 ? ((-delta) as u32 << 1) | 1 : (delta as u32) << 1;
val code := offset - this.at;
if encoded < 8 && code <= 0xF {
this.put(ba_change_code_offset_and_line_offset);
this.put((encoded << 4) | code);
} else {
# The first part's line is said though it is where the function begins,
# as some readers take a part's line only from a change to it.
if delta != 0 || !this.begun {
this.put(ba_change_line_offset);
this.put(encoded);
}
this.put(ba_change_code_offset);
this.put(code);
}
this.begun = true;
this.at = offset;
this.file = file;
this.line = line;
}
#! The code from an offset is not the site's, so the part before it ends.
#! @param this The annotations.
#! @param offset Where that code begins.
pub def annotations::away(&mut this, offset: u32): void {
if this.open {
this.put(ba_change_code_length);
this.put(offset - this.at);
this.at = offset;
}
this.open = false;
}
#! Ends the annotations where the site's last part ends.
#! @param this The annotations.
#! @param offset Where it ends.
pub def annotations::finish(&mut this, offset: u32): void {
this.put(ba_change_code_length);
this.put(offset - this.at);
}