llvmtype.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. {
  2. Copyright (c) 2008,2015 by Peter Vreman, Florian Klaempfl and Jonas Maebe
  3. This units contains support for generating LLVM type info
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. This units contains support for LLVM type info generation.
  19. It's based on the debug info system, since it's quite similar
  20. }
  21. unit llvmtype;
  22. {$i fpcdefs.inc}
  23. {$h+}
  24. interface
  25. uses
  26. cclasses,globtype,
  27. aasmbase,aasmtai,aasmdata,
  28. symbase,symtype,symdef,symsym,
  29. finput,
  30. dbgbase;
  31. { TLLVMTypeInfo }
  32. type
  33. TLLVMTypeInfo = class(TDebugInfo)
  34. protected
  35. function record_def(def:tdef): tdef;
  36. procedure appenddef_array(list:TAsmList;def:tarraydef);override;
  37. procedure appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
  38. procedure appenddef_record(list:TAsmList;def:trecorddef);override;
  39. procedure appenddef_pointer(list:TAsmList;def:tpointerdef);override;
  40. procedure appenddef_procvar(list:TAsmList;def:tprocvardef);override;
  41. procedure appendprocdef(list:TAsmList;def:tprocdef);override;
  42. procedure appenddef_object(list:TAsmList;def: tobjectdef);override;
  43. procedure appenddef_variant(list:TAsmList;def: tvariantdef);override;
  44. procedure appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  45. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
  46. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
  47. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
  48. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
  49. procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
  50. procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);override;
  51. procedure enum_membersyms_callback(p:TObject;arg:pointer);
  52. procedure process_llvmins(deftypelist: tasmlist; p: tai);
  53. procedure process_tai(deftypelist: tasmlist; p: tai);
  54. procedure process_asmlist(deftypelist, asmlist: tasmlist);
  55. public
  56. constructor Create;override;
  57. destructor Destroy;override;
  58. procedure inserttypeinfo;override;
  59. end;
  60. implementation
  61. uses
  62. sysutils,cutils,cfileutl,constexp,
  63. version,globals,verbose,systems,
  64. cpubase,cgbase,paramgr,
  65. fmodule,nobj,
  66. defutil,symconst,symtable,
  67. llvmbase, aasmllvm, aasmcnst;
  68. {****************************************************************************
  69. TDebugInfoDwarf
  70. ****************************************************************************}
  71. function TLLVMTypeInfo.record_def(def:tdef): tdef;
  72. begin
  73. result:=def;
  74. if def.dbg_state<>dbg_state_unused then
  75. exit;
  76. def.dbg_state:=dbg_state_used;
  77. deftowritelist.Add(def);
  78. defnumberlist.Add(def);
  79. end;
  80. constructor TLLVMTypeInfo.Create;
  81. begin
  82. inherited Create;
  83. end;
  84. destructor TLLVMTypeInfo.Destroy;
  85. begin
  86. inherited destroy;
  87. end;
  88. procedure TLLVMTypeInfo.enum_membersyms_callback(p:TObject; arg: pointer);
  89. begin
  90. case tsym(p).typ of
  91. fieldvarsym:
  92. appendsym_fieldvar(TAsmList(arg),tfieldvarsym(p));
  93. end;
  94. end;
  95. procedure TLLVMTypeInfo.process_llvmins(deftypelist: tasmlist; p: tai);
  96. var
  97. opidx, paraidx: longint;
  98. callpara: pllvmcallpara;
  99. begin
  100. for opidx:=0 to taillvm(p).ops-1 do
  101. case taillvm(p).oper[opidx]^.typ of
  102. top_def:
  103. appenddef(deftypelist,taillvm(p).oper[opidx]^.def);
  104. top_tai:
  105. process_tai(deftypelist,taillvm(p).oper[opidx]^.ai);
  106. top_para:
  107. for paraidx:=0 to taillvm(p).oper[opidx]^.paras.count-1 do
  108. begin
  109. callpara:=pllvmcallpara(taillvm(p).oper[opidx]^.paras[paraidx]);
  110. appenddef(deftypelist,callpara^.def);
  111. end;
  112. end;
  113. end;
  114. procedure TLLVMTypeInfo.process_tai(deftypelist: tasmlist; p: tai);
  115. begin
  116. case p.typ of
  117. ait_llvmalias:
  118. appenddef(deftypelist,taillvmalias(p).def);
  119. ait_llvmdecl:
  120. appenddef(deftypelist,taillvmdecl(p).def);
  121. ait_llvmins:
  122. process_llvmins(deftypelist,p);
  123. ait_typedconst:
  124. appenddef(deftypelist,tai_abstracttypedconst(p).def);
  125. end;
  126. end;
  127. procedure TLLVMTypeInfo.process_asmlist(deftypelist, asmlist: tasmlist);
  128. var
  129. hp: tai;
  130. begin
  131. if not assigned(asmlist) then
  132. exit;
  133. hp:=tai(asmlist.first);
  134. while assigned(hp) do
  135. begin
  136. process_tai(deftypelist,hp);
  137. hp:=tai(hp.next);
  138. end;
  139. end;
  140. procedure TLLVMTypeInfo.appenddef_array(list:TAsmList;def:tarraydef);
  141. begin
  142. appenddef(list,def.elementdef);
  143. end;
  144. procedure TLLVMTypeInfo.appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
  145. var
  146. symdeflist: tfpobjectlist;
  147. i: longint;
  148. begin
  149. symdeflist:=tabstractrecordsymtable(def.symtable).llvmst.symdeflist;
  150. for i:=0 to symdeflist.Count-1 do
  151. appenddef(list,tllvmshadowsymtableentry(symdeflist[i]).def);
  152. if assigned(def.typesym) then
  153. list.concat(taillvm.op_size(LA_TYPE,record_def(def)));
  154. end;
  155. procedure TLLVMTypeInfo.appenddef_record(list:TAsmList;def:trecorddef);
  156. begin
  157. appenddef_abstractrecord(list,def);
  158. end;
  159. procedure TLLVMTypeInfo.appenddef_pointer(list:TAsmList;def:tpointerdef);
  160. begin
  161. appenddef(list,def.pointeddef);
  162. end;
  163. procedure TLLVMTypeInfo.appenddef_procvar(list:TAsmList;def:tprocvardef);
  164. var
  165. i: longint;
  166. begin
  167. { todo: handle mantis #25551; there is no way to create a symbolic
  168. la_type for a procvardef (unless it's a procedure of object/record),
  169. which means that recursive references should become plain "procedure"
  170. types that are then casted to the real type when they are used }
  171. for i:=0 to def.paras.count-1 do
  172. appenddef(list,tparavarsym(def.paras[i]).vardef);
  173. appenddef(list,def.returndef);
  174. if assigned(def.typesym) and
  175. not def.is_addressonly then
  176. list.concat(taillvm.op_size(LA_TYPE,record_def(def)));
  177. end;
  178. procedure TLLVMTypeInfo.appendprocdef(list:TAsmList;def:tprocdef);
  179. begin
  180. { the procdef itself is already written by appendprocdef_implicit }
  181. { last write the types from this procdef }
  182. if assigned(def.parast) then
  183. write_symtable_defs(current_asmdata.asmlists[al_start],def.parast);
  184. if assigned(def.localst) and
  185. (def.localst.symtabletype=localsymtable) then
  186. write_symtable_defs(current_asmdata.asmlists[al_start],def.localst);
  187. end;
  188. procedure TLLVMTypeInfo.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  189. begin
  190. appenddef(list,sym.vardef);
  191. end;
  192. procedure TLLVMTypeInfo.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  193. begin
  194. appendsym_var(list,sym);
  195. end;
  196. procedure TLLVMTypeInfo.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  197. begin
  198. appendsym_var(list,sym);
  199. end;
  200. procedure TLLVMTypeInfo.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  201. begin
  202. appendsym_var(list,sym);
  203. end;
  204. procedure TLLVMTypeInfo.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  205. begin
  206. appenddef(list,sym.vardef);
  207. end;
  208. procedure TLLVMTypeInfo.appendsym_const(list:TAsmList;sym:tconstsym);
  209. begin
  210. appenddef(list,sym.constdef);
  211. end;
  212. procedure TLLVMTypeInfo.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  213. begin
  214. appenddef(list,sym.vardef);
  215. end;
  216. procedure TLLVMTypeInfo.inserttypeinfo;
  217. procedure write_defs_to_write;
  218. var
  219. n : integer;
  220. looplist,
  221. templist: TFPObjectList;
  222. def : tdef;
  223. begin
  224. templist := TFPObjectList.Create(False);
  225. looplist := deftowritelist;
  226. while looplist.count > 0 do
  227. begin
  228. deftowritelist := templist;
  229. for n := 0 to looplist.count - 1 do
  230. begin
  231. def := tdef(looplist[n]);
  232. case def.dbg_state of
  233. dbg_state_written:
  234. continue;
  235. dbg_state_writing:
  236. internalerror(200610052);
  237. dbg_state_unused:
  238. internalerror(200610053);
  239. dbg_state_used:
  240. appenddef(current_asmdata.asmlists[al_start],def)
  241. else
  242. internalerror(200610054);
  243. end;
  244. end;
  245. looplist.clear;
  246. templist := looplist;
  247. looplist := deftowritelist;
  248. end;
  249. templist.free;
  250. end;
  251. var
  252. storefilepos: tfileposinfo;
  253. def: tdef;
  254. i: longint;
  255. hal: tasmlisttype;
  256. begin
  257. storefilepos:=current_filepos;
  258. current_filepos:=current_module.mainfilepos;
  259. defnumberlist:=TFPObjectList.create(false);
  260. deftowritelist:=TFPObjectList.create(false);
  261. { write all global/static variables, part of flaggin all required tdefs }
  262. if assigned(current_module.globalsymtable) then
  263. write_symtable_syms(current_asmdata.asmlists[al_start],current_module.globalsymtable);
  264. if assigned(current_module.localsymtable) then
  265. write_symtable_syms(current_asmdata.asmlists[al_start],current_module.localsymtable);
  266. { write all procedures and methods, part of flagging all required tdefs }
  267. if assigned(current_module.globalsymtable) then
  268. write_symtable_procdefs(current_asmdata.asmlists[al_start],current_module.globalsymtable);
  269. if assigned(current_module.localsymtable) then
  270. write_symtable_procdefs(current_asmdata.asmlists[al_start],current_module.localsymtable);
  271. { process all llvm instructions, part of flagging all required tdefs }
  272. for hal:=low(TasmlistType) to high(TasmlistType) do
  273. if hal<>al_start then
  274. process_asmlist(current_asmdata.asmlists[al_start],current_asmdata.asmlists[hal]);
  275. { write all used defs }
  276. write_defs_to_write;
  277. { reset all def labels }
  278. for i:=0 to defnumberlist.count-1 do
  279. begin
  280. def := tdef(defnumberlist[i]);
  281. if assigned(def) then
  282. begin
  283. def.dbg_state:=dbg_state_unused;
  284. end;
  285. end;
  286. defnumberlist.free;
  287. defnumberlist:=nil;
  288. deftowritelist.free;
  289. deftowritelist:=nil;
  290. current_filepos:=storefilepos;
  291. end;
  292. procedure TLLVMTypeInfo.appenddef_object(list:TAsmList;def: tobjectdef);
  293. begin
  294. appenddef_abstractrecord(list,def);
  295. end;
  296. procedure TLLVMTypeInfo.appenddef_variant(list:TAsmList;def: tvariantdef);
  297. begin
  298. appenddef(list,tabstractrecorddef(search_system_type('TVARDATA').typedef));
  299. end;
  300. end.