#! @file lines.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the writer of line tables: a line program, a sequence of rows each #! naming where in the source the code from an offset is from, written as the #! changes from one row to the next, and the header naming the directories and #! files the rows number. import std::io; module binary::dwarf; #! A line program being written, a sequence at a time. pub struct line_program { pub var bytes: byte_writer; #! The program. var file: u64; #! The file of the last row, numbered from 1. var line: i64; #! Its line. var column: u64; #! Its column. var at: u64; #! Its offset from where the sequence begins. } #! Makes a program with no sequences. #! @returns The program. pub def line_program::new(): line_program { return line_program { bytes = byte_writer::new(), file = 1, line = 1 }; } #! Begins a sequence, of the code at an address: its rows follow, then `end`. #! @param this The program. #! @param address Where the code begins. pub def line_program::begin(&mut this, address: u64): void { this.bytes.byte(0); uleb(&mut this.bytes, 9); this.bytes.byte(dw_lne_set_address); this.bytes.u64(address); this.file = 1; this.line = 1; this.column = 0; this.at = 0; } #! Adds a row: the code from an offset is from a place in a file, and where the #! function's prologue ends, where it is the first past it. #! @param this The program. #! @param offset Where the code is, from where the sequence begins. #! @param file The file, numbered from 1. #! @param line Its line, from 1. #! @param column Its column, from 1, or 0 for none. #! @param prologue_end Whether the row is the first past the prologue. pub def line_program::row(&mut this, offset: u64, file: u64, line: u64, column: u64, prologue_end: bool): void { if file != this.file { this.bytes.byte(dw_lns_set_file); uleb(&mut this.bytes, file); this.file = file; } if column != this.column { this.bytes.byte(dw_lns_set_column); uleb(&mut this.bytes, column); this.column = column; } if line as i64 != this.line { this.bytes.byte(dw_lns_advance_line); sleb(&mut this.bytes, line as i64 - this.line); this.line = line as i64; } if offset > this.at { this.bytes.byte(dw_lns_advance_pc); uleb(&mut this.bytes, offset - this.at); this.at = offset; } if prologue_end { this.bytes.byte(dw_lns_set_prologue_end); } this.bytes.byte(dw_lns_copy); } #! Ends a sequence, where its code ends. #! @param this The program. #! @param size The bytes of the code. pub def line_program::end(&mut this, size: u64): void { if size > this.at { this.bytes.byte(dw_lns_advance_pc); uleb(&mut this.bytes, size - this.at); } this.bytes.byte(0); uleb(&mut this.bytes, 1); this.bytes.byte(dw_lne_end_sequence); } #! Writes a line table: its header, naming the directories and the files, then #! the program. The directories are the one the build was in, which relative #! paths are within, then the folder of each absolute path, which names its #! file by its last part. The file numbered 0 is the one numbered 1, as DWARF 5 #! numbers the unit's main file both ways. Each name is in the line table's #! strings. #! @param out The section. #! @param directory The directory the build was in. #! @param paths The path of each file, as the program numbers them from 1. #! @param strings The table of the line table's strings, `.debug_line_str`. #! @param program The program. pub def write_line_table(out: &mut byte_writer, directory: strview, paths: span, strings: &mut string_table, program: span): void { val unit := out.begin_length(); out.u16(5); out.byte(8); out.byte(0); val length := out.begin_length(); out.byte(1); # The least bytes an instruction takes, out.byte(1); # the operations of one, out.byte(1); # and every row begins a statement. out.byte(0xFB); # The least line advanced by a special opcode, -5, out.byte(14); # how many lines they advance, out.byte(13); # and the first of them. val lengths: u8[12] = [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1]; out.put(lengths[:]); var directories := vector::::new(); var held_in := vector::::new(); directories.append(directory); for &text in paths { held_in.append(0); continue if text.length == 0 || text[0] != '/' as u8; var slash := text.length - 1; while text[slash] != '/' as u8 { slash--; } val parent := text.substr(0, slash > 0 ? slash : 1); var d: u64 = 1; while d < directories.length && directories[d] != parent { d++; } if d == directories.length { directories.append(parent); } held_in[held_in.length - 1] = d; } out.byte(1); uleb(out, 1); uleb(out, dw_form_line_strp); uleb(out, directories.length); for &each in directories { out.u32(strings.offset_of(each) as u64); } out.byte(2); uleb(out, 1); uleb(out, dw_form_line_strp); uleb(out, 2); uleb(out, dw_form_udata); uleb(out, paths.length + 1); for i in 0..paths.length + 1 { val k := i == 0 ? 0 : i - 1; val text := paths[k]; var name := text; val d := held_in[k]; if d != 0 { val skip := directories[d].length + (directories[d].length > 1 ? 1 : 0); name = text.substr(skip, text.length - skip); } out.u32(strings.offset_of(name) as u64); uleb(out, d); } out.end_length(length); out.put(program); out.end_length(unit); }