Skip to content

Commit

Permalink
Compiler: emit DWARF2 line number information
Browse files Browse the repository at this point in the history
  • Loading branch information
vbgl committed Jan 16, 2024
1 parent 801bbad commit 71f95bb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
against Spectre).
([PR #631](https://github.com/jasmin-lang/jasmin/pull/631)).

- Interleave references to source-code positions within the assembly listings.
([PR #684](https://github.com/jasmin-lang/jasmin/pull/684)).

## Bug fixes

- Type-checking rejects invalid variants of primitive operators
Expand Down
27 changes: 27 additions & 0 deletions compiler/src/debugInfo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
open Utils
open Location
open Format

(* TODO: remove global mutable state *)

(** Give a unique number to the given file name.
Second return value is true the first time this file name is seed. *)
let internFile =
let count = ref 0 in
let internedFiles = Hashtbl.create 17 in
fun s ->
match Hashtbl.find internedFiles s with
| v -> (v, false)
| exception Not_found ->
incr count;
let v = !count in
Hashtbl.add internedFiles s v;
(v, true)

let source_positions ii =
if Location.isdummy ii then []
else
let n, isFresh = internFile ii.loc_fname in
let line, col = ii.loc_start in
let loc = [ asprintf ".loc %d %d %d\n" n line col ] in
if isFresh then asprintf ".file %d %S\n" n ii.loc_fname :: loc else loc
2 changes: 2 additions & 0 deletions compiler/src/debugInfo.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val source_positions : Location.t -> string list
(** List of .file and .loc directives to decorate assembly listings. *)
6 changes: 4 additions & 2 deletions compiler/src/pp_arm_m4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,10 @@ let pp_instr fn _ i =

let pp_body fn fmt =
let open List in
concat_map @@ fun { asmi_i = i ; _ } ->
pp_instr fn fmt i
concat_map @@ fun { asmi_i = i ; asmi_ii = (ii, _) } ->
append
(map (fun i -> LInstr (i, [])) (DebugInfo.source_positions ii.base_loc))
(pp_instr fn fmt i)

(* -------------------------------------------------------------------- *)
(* TODO_ARM: This is architecture-independent. *)
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/ppasm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,14 @@ module Printer (BP:BPrinter) = struct
let args = pp_asm_args pp.pp_aop_args in
`Instr(name, args)

(* -------------------------------------------------------------------- *)
let pp_ii ({ Location.base_loc = ii; _}, _) =
List.map (fun i -> `Instr(i, [])) (DebugInfo.source_positions ii)

(* -------------------------------------------------------------------- *)
let pp_instr name (fmt : Format.formatter) (i : (_, _, _, _, _, _) Arch_decl.asm_i) =
let Arch_decl.({ asmi_i = i ; _ }) = i in
let Arch_decl.({ asmi_i = i ; asmi_ii = ii }) = i in
List.iter (pp_gen fmt) (pp_ii ii);
pp_gen fmt (pp_instr name i)

(* -------------------------------------------------------------------- *)
Expand Down

0 comments on commit 71f95bb

Please sign in to comment.