33 Author: Henny Sipma
44 ------------------------------------------------------------------------------
55 The MIT License (MIT)
6-
6+
77 Copyright (c) 2021-2024 Aarno Labs LLC
88
99 Permission is hereby granted, free of charge, to any person obtaining a copy
1212 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1313 copies of the Software, and to permit persons to whom the Software is
1414 furnished to do so, subject to the following conditions:
15-
15+
1616 The above copyright notice and this permission notice shall be included in all
1717 copies or substantial portions of the Software.
18-
18+
1919 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2121 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -60,6 +60,9 @@ object (self)
6060 val gvardecls = H. create 3 (* bvname -> (varinfo.ix, loc.ix) *)
6161 val gvars = H. create 3 (* idem *)
6262 val gfuns = H. create 3 (* bsvar.bvname -> (fundec, loc.ix) *)
63+ val vinfo_srcmap = H. create 3
64+ (* varinfo.ix -> (ix(filename), linenr, binloc, [ix(notes)]) *)
65+
6366 val mutable varinfo_vid_counter = 10000
6467
6568 method private get_varinfo_id =
@@ -70,6 +73,24 @@ object (self)
7073
7174 method add_bcfile (f : bcfile_t ) =
7275 let i = bcd#index_location in
76+
77+ let add_srcmapinfo (vinfo : bvarinfo_t ) =
78+ match BCHBCAttributes. gcc_attributes_to_srcmapinfo vinfo.bvattr with
79+ | Some srcmap ->
80+ let vix = bcd#index_varinfo vinfo in
81+ if H. mem vinfo_srcmap vix then
82+ ()
83+ else
84+ let srcloc = srcmap.srcmap_srcloc in
85+ let ixfilename = bcd#index_string srcloc.srcloc_filename in
86+ let linenumber = srcloc.srcloc_linenumber in
87+ let ixnotes = List. map bcd#index_string srcloc.srcloc_notes in
88+ let binloc =
89+ match srcmap.srcmap_binloc with
90+ | Some s -> s
91+ | _ -> " none" in
92+ H. add vinfo_srcmap vix (ixfilename, linenumber, binloc, ixnotes)
93+ | _ -> () in
7394 begin
7495 List. iter (fun g ->
7596 match g with
@@ -100,8 +121,10 @@ object (self)
100121 ()
101122 else
102123 let _ = chlog#add " bcfiles:add gvardecl" (STR vinfo.bvname) in
124+ let _ = add_srcmapinfo vinfo in
103125 H. replace gvardecls vinfo.bvname (bcd#index_varinfo vinfo, i loc)
104126 | GVar (vinfo , iinfo , loc ) ->
127+ let _ = add_srcmapinfo vinfo in
105128 let _ = chlog#add " bcfiles:add gvar" (STR vinfo.bvname) in
106129 H. replace gvars
107130 vinfo.bvname
@@ -112,7 +135,7 @@ object (self)
112135 i loc)
113136 | GFun (fundec , loc ) ->
114137 let _ = chlog#add " bcfiles:add gfun" (STR fundec.bsvar.bvname) in
115- H. replace gfuns fundec.bsvar.bvname (fundec, bcd#index_location loc);
138+ H. replace gfuns fundec.bsvar.bvname (fundec, bcd#index_location loc);
116139 | _ -> () ) f.bglobals;
117140 chlog#add
118141 " bcfiles:add_bcfile"
@@ -411,7 +434,7 @@ object (self)
411434 result := (v2s (bcd#get_varinfo ix)) :: ! result) gvardecls;
412435 ! result
413436 end
414-
437+
415438 method has_gfun (name : string ) = H. mem gfuns name
416439
417440 method get_gfun (name : string ) =
@@ -495,7 +518,7 @@ object (self)
495518 match cn#getIntListAttribute " ixs" with
496519 | [cix; locix] -> H. add gcomptags (name, ckey) (cix, locix)
497520 | _ -> () ) (getcc " cid" )
498-
521+
499522 method private write_xml_genums (node : xml_element_int ) =
500523 let genums = H. fold (fun k v a -> (k, v)::a) genumtags [] in
501524 node#appendChildren
@@ -533,7 +556,7 @@ object (self)
533556 match en#getIntListAttribute " ixs" with
534557 | [eix; locix] -> H. add genumtagdecls name (eix, locix)
535558 | _ -> () ) (getcc " eid" )
536-
559+
537560 method private write_xml_gvars (node : xml_element_int ) =
538561 let gvarinfos = H. fold (fun k v a -> (k, v)::a) gvars [] in
539562 node#appendChildren
@@ -591,7 +614,29 @@ object (self)
591614 let locix = gn#getIntAttribute " locix" in
592615 let fundec = read_xml_function_definition gn in
593616 H. add gfuns name (fundec, locix)) (getcc " gfun" )
594-
617+
618+ method private write_xml_srcmap (node : xml_element_int ) =
619+ let srcmapentries = H. fold (fun k v a -> (k, v)::a) vinfo_srcmap [] in
620+ node#appendChildren
621+ (List. map (fun (vix , (ixfn , lnr , binloc , _ixnotes )) ->
622+ let snode = xmlElement " srcloc" in
623+ begin
624+ snode#setIntAttribute " vix" vix;
625+ snode#setIntAttribute " ixfn" ixfn;
626+ snode#setIntAttribute " lnr" lnr;
627+ snode#setAttribute " binloc" binloc;
628+ snode
629+ end ) srcmapentries)
630+
631+ method private read_xml_srcmap (node : xml_element_int ) =
632+ let getcc = node#getTaggedChildren in
633+ List. iter (fun gs ->
634+ let vix = gs#getIntAttribute " vix" in
635+ let ixfn = gs#getIntAttribute " ixfn" in
636+ let lnr = gs#getIntAttribute " lnr" in
637+ let binloc = gs#getAttribute " binloc" in
638+ H. add vinfo_srcmap vix (ixfn, lnr, binloc, [] )) (getcc " srcloc" )
639+
595640 method write_xml (node : xml_element_int ) =
596641 let tnode = xmlElement " typeinfos" in
597642 let cnode = xmlElement " compinfos" in
@@ -601,6 +646,7 @@ object (self)
601646 let vnode = xmlElement " varinfos" in
602647 let vdnode = xmlElement " varinfodecls" in
603648 let gfunnode = xmlElement " gfuns" in
649+ let srcmapnode = xmlElement " srcmap" in
604650 begin
605651 self#write_xml_gtypes tnode;
606652 self#write_xml_gcomps cnode;
@@ -610,8 +656,9 @@ object (self)
610656 self#write_xml_gvars vnode;
611657 self#write_xml_gvardecls vdnode;
612658 self#write_xml_gfuns gfunnode;
659+ self#write_xml_srcmap srcmapnode;
613660 node#appendChildren[
614- tnode; cnode; cdnode; enode; ednode; vnode; vdnode; gfunnode]
661+ tnode; cnode; cdnode; enode; ednode; vnode; vdnode; gfunnode; srcmapnode ]
615662 end
616663
617664 method read_xml (node : xml_element_int ) =
@@ -624,9 +671,10 @@ object (self)
624671 self#read_xml_genumdecls (getc " enuminfodecls" );
625672 self#read_xml_gvars (getc " varinfos" );
626673 self#read_xml_gvardecls (getc " varinfodecls" );
627- self#read_xml_gfuns (getc " gfuns" )
674+ self#read_xml_gfuns (getc " gfuns" );
675+ self#read_xml_srcmap (getc " srcmap" )
628676 end
629-
677+
630678end
631679
632680
0 commit comments