llvmtype.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. aasmllvm,aasmcnst,
  30. finput,
  31. dbgbase;
  32. { TLLVMTypeInfo }
  33. type
  34. TLLVMTypeInfo = class(TDebugInfo)
  35. protected
  36. { using alias/external declarations it's possible to refer to the same
  37. assembler symbol using multiple types:
  38. function f(p: pointer): pointer; [public, alias: 'FPC_FUNC'];
  39. procedure test(p: pointer); external name 'FPC_FUNC';
  40. We have to insert the appropriate typecasts (per module) for LLVM in
  41. this case. That can only be done after all code for a module has been
  42. generated, as these alias declarations can appear anywhere }
  43. asmsymtypes: THashSet;
  44. procedure record_asmsym_def(sym: TAsmSymbol; def: tdef);
  45. function get_asmsym_def(sym: TAsmSymbol): tdef;
  46. function record_def(def:tdef): tdef;
  47. procedure appenddef_array(list:TAsmList;def:tarraydef);override;
  48. procedure appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
  49. procedure appenddef_record(list:TAsmList;def:trecorddef);override;
  50. procedure appenddef_pointer(list:TAsmList;def:tpointerdef);override;
  51. procedure appenddef_procvar(list:TAsmList;def:tprocvardef);override;
  52. procedure appendprocdef(list:TAsmList;def:tprocdef);override;
  53. procedure appenddef_object(list:TAsmList;def: tobjectdef);override;
  54. procedure appenddef_variant(list:TAsmList;def: tvariantdef);override;
  55. procedure appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  56. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
  57. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
  58. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
  59. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
  60. procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
  61. procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);override;
  62. procedure enum_membersyms_callback(p:TObject;arg:pointer);
  63. procedure collect_llvmins_info(deftypelist: tasmlist; p: taillvm);
  64. procedure collect_tai_info(deftypelist: tasmlist; p: tai);
  65. procedure collect_asmlist_info(deftypelist, asmlist: tasmlist);
  66. procedure insert_llvmins_typeconversions(p: taillvm);
  67. procedure insert_typedconst_typeconversion(p: tai_abstracttypedconst);
  68. procedure insert_tai_typeconversions(p: tai);
  69. procedure insert_asmlist_typeconversions(list: tasmlist);
  70. public
  71. constructor Create;override;
  72. destructor Destroy;override;
  73. procedure inserttypeinfo;override;
  74. end;
  75. implementation
  76. uses
  77. sysutils,cutils,cfileutl,constexp,
  78. version,globals,verbose,systems,
  79. cpubase,cgbase,paramgr,
  80. fmodule,nobj,
  81. defutil,defcmp,symconst,symtable,
  82. llvmbase,llvmdef
  83. ;
  84. {****************************************************************************
  85. TDebugInfoDwarf
  86. ****************************************************************************}
  87. procedure TLLVMTypeInfo.record_asmsym_def(sym: TAsmSymbol; def: tdef);
  88. var
  89. res: PHashSetItem;
  90. begin
  91. res:=asmsymtypes.FindOrAdd(@sym,sizeof(sym));
  92. { if there are multiple definitions of the same symbol, we're in
  93. trouble anyway, so don't bother checking whether data is already
  94. assigned }
  95. res^.Data:=def;
  96. end;
  97. function TLLVMTypeInfo.get_asmsym_def(sym: TAsmSymbol): tdef;
  98. var
  99. res: PHashSetItem;
  100. begin
  101. res:=asmsymtypes.Find(@sym,sizeof(sym));
  102. { we must have a def for every used asmsym }
  103. if not assigned(res) or
  104. not assigned(res^.data) then
  105. internalerror(2015042701);
  106. result:=tdef(res^.Data);
  107. end;
  108. function TLLVMTypeInfo.record_def(def:tdef): tdef;
  109. begin
  110. result:=def;
  111. if def.dbg_state<>dbg_state_unused then
  112. exit;
  113. def.dbg_state:=dbg_state_used;
  114. deftowritelist.Add(def);
  115. defnumberlist.Add(def);
  116. end;
  117. constructor TLLVMTypeInfo.Create;
  118. begin
  119. inherited Create;
  120. asmsymtypes:=THashSet.Create(current_asmdata.AsmSymbolDict.Count,true,false);
  121. end;
  122. destructor TLLVMTypeInfo.Destroy;
  123. begin
  124. asmsymtypes.free;
  125. inherited destroy;
  126. end;
  127. procedure TLLVMTypeInfo.enum_membersyms_callback(p:TObject; arg: pointer);
  128. begin
  129. case tsym(p).typ of
  130. fieldvarsym:
  131. appendsym_fieldvar(TAsmList(arg),tfieldvarsym(p));
  132. end;
  133. end;
  134. procedure TLLVMTypeInfo.collect_llvmins_info(deftypelist: tasmlist; p: taillvm);
  135. var
  136. opidx, paraidx: longint;
  137. callpara: pllvmcallpara;
  138. begin
  139. for opidx:=0 to p.ops-1 do
  140. case p.oper[opidx]^.typ of
  141. top_def:
  142. appenddef(deftypelist,p.oper[opidx]^.def);
  143. top_tai:
  144. collect_tai_info(deftypelist,p.oper[opidx]^.ai);
  145. top_para:
  146. for paraidx:=0 to p.oper[opidx]^.paras.count-1 do
  147. begin
  148. callpara:=pllvmcallpara(p.oper[opidx]^.paras[paraidx]);
  149. appenddef(deftypelist,callpara^.def);
  150. end;
  151. end;
  152. end;
  153. procedure TLLVMTypeInfo.collect_tai_info(deftypelist: tasmlist; p: tai);
  154. begin
  155. case p.typ of
  156. ait_llvmalias:
  157. begin
  158. appenddef(deftypelist,taillvmalias(p).def);
  159. record_asmsym_def(taillvmalias(p).newsym,taillvmalias(p).def);
  160. end;
  161. ait_llvmdecl:
  162. begin
  163. appenddef(deftypelist,taillvmdecl(p).def);
  164. record_asmsym_def(taillvmdecl(p).namesym,taillvmdecl(p).def);
  165. end;
  166. ait_llvmins:
  167. collect_llvmins_info(deftypelist,taillvm(p));
  168. ait_typedconst:
  169. appenddef(deftypelist,tai_abstracttypedconst(p).def);
  170. end;
  171. end;
  172. procedure TLLVMTypeInfo.collect_asmlist_info(deftypelist, asmlist: tasmlist);
  173. var
  174. hp: tai;
  175. begin
  176. if not assigned(asmlist) then
  177. exit;
  178. hp:=tai(asmlist.first);
  179. while assigned(hp) do
  180. begin
  181. collect_tai_info(deftypelist,hp);
  182. hp:=tai(hp.next);
  183. end;
  184. end;
  185. function equal_llvm_defs(def1, def2: tdef): boolean;
  186. var
  187. def1str, def2str: TSymStr;
  188. begin
  189. if def1=def2 then
  190. exit(true);
  191. def1str:=llvmencodetypename(def1);
  192. def2str:=llvmencodetypename(def2);
  193. { normalise both type representations in case one is a procdef
  194. and the other is a procvardef}
  195. if def1.typ=procdef then
  196. def1str:=def1str+'*';
  197. if def2.typ=procdef then
  198. def2str:=def2str+'*';
  199. result:=def1str=def2str;
  200. end;
  201. procedure TLLVMTypeInfo.insert_llvmins_typeconversions(p: taillvm);
  202. var
  203. symdef,
  204. opdef: tdef;
  205. cnv: taillvm;
  206. i: longint;
  207. begin
  208. case p.llvmopcode of
  209. la_call:
  210. if p.oper[3]^.typ=top_ref then
  211. begin
  212. symdef:=get_asmsym_def(p.oper[3]^.ref^.symbol);
  213. { the type used in the call is different from the type used to
  214. declare the symbol -> insert a typecast }
  215. if not equal_llvm_defs(symdef,p.oper[0]^.def) then
  216. begin
  217. if symdef.typ=procdef then
  218. { ugly, but can't use getcopyas(procvardef) due to the
  219. symtablestack not being available here (getpointerdef
  220. is hardcoded to put things in the current module's
  221. symtable) and "pointer to procedure" results in the
  222. correct llvm type }
  223. symdef:=getpointerdef(tprocdef(symdef));
  224. cnv:=taillvm.op_reg_size_sym_size(la_bitcast,NR_NO,symdef,p.oper[3]^.ref^.symbol,p.oper[0]^.def);
  225. p.loadtai(3,cnv);
  226. end;
  227. end;
  228. else if p.llvmopcode<>la_br then
  229. begin
  230. { check the types of all symbolic operands }
  231. for i:=0 to p.ops-1 do
  232. case p.oper[i]^.typ of
  233. top_ref:
  234. if (p.oper[i]^.ref^.refaddr=addr_full) and
  235. (p.oper[i]^.ref^.symbol.bind<>AB_TEMP) then
  236. begin
  237. symdef:=get_asmsym_def(p.oper[i]^.ref^.symbol);
  238. opdef:=p.spilling_get_reg_type(i);
  239. if not equal_llvm_defs(symdef,opdef) then
  240. begin
  241. cnv:=taillvm.op_reg_size_sym_size(la_bitcast,NR_NO,symdef,p.oper[i]^.ref^.symbol,opdef);
  242. p.loadtai(i,cnv);
  243. end;
  244. end;
  245. top_tai:
  246. insert_tai_typeconversions(p.oper[i]^.ai);
  247. end;
  248. end;
  249. end;
  250. end;
  251. procedure TLLVMTypeInfo.insert_typedconst_typeconversion(p: tai_abstracttypedconst);
  252. var
  253. symdef: tdef;
  254. cnv: taillvm;
  255. elementp: tai_abstracttypedconst;
  256. begin
  257. case p.adetyp of
  258. tck_simple:
  259. begin
  260. case tai_simpletypedconst(p).val.typ of
  261. ait_const:
  262. if assigned(tai_const(tai_simpletypedconst(p).val).sym) and
  263. not assigned(tai_const(tai_simpletypedconst(p).val).endsym) then
  264. begin
  265. symdef:=get_asmsym_def(tai_const(tai_simpletypedconst(p).val).sym);
  266. { all references to symbols in typed constants are
  267. references to the address of a global symbol (you can't
  268. refer to the data itself, just like you can't initialise
  269. a Pascal (typed) constant with the contents of another
  270. typed constant) }
  271. symdef:=getpointerdef(symdef);
  272. if not equal_llvm_defs(symdef,p.def) then
  273. begin
  274. cnv:=taillvm.op_reg_tai_size(la_bitcast,NR_NO,tai_simpletypedconst.create(tck_simple,symdef,tai_simpletypedconst(p).val),p.def);
  275. tai_simpletypedconst(p).val:=cnv;
  276. end;
  277. end;
  278. else
  279. insert_tai_typeconversions(tai_const(tai_simpletypedconst(p).val));
  280. end;
  281. end;
  282. tck_array,
  283. tck_record:
  284. begin
  285. for elementp in tai_aggregatetypedconst(p) do
  286. insert_typedconst_typeconversion(elementp);
  287. end;
  288. end;
  289. end;
  290. procedure TLLVMTypeInfo.insert_tai_typeconversions(p: tai);
  291. begin
  292. case p.typ of
  293. ait_llvmins:
  294. insert_llvmins_typeconversions(taillvm(p));
  295. { can also be necessary in case someone initialises a typed const with
  296. the address of an external symbol aliasing one declared with a
  297. different type in the same mmodule. }
  298. ait_typedconst:
  299. insert_typedconst_typeconversion(tai_abstracttypedconst(p));
  300. ait_llvmdecl:
  301. insert_asmlist_typeconversions(taillvmdecl(p).initdata);
  302. end;
  303. end;
  304. procedure TLLVMTypeInfo.insert_asmlist_typeconversions(list: tasmlist);
  305. var
  306. hp: tai;
  307. begin
  308. if not assigned(list) then
  309. exit;
  310. hp:=tai(list.first);
  311. while assigned(hp) do
  312. begin
  313. insert_tai_typeconversions(hp);
  314. hp:=tai(hp.next);
  315. end;
  316. end;
  317. procedure TLLVMTypeInfo.appenddef_array(list:TAsmList;def:tarraydef);
  318. begin
  319. appenddef(list,def.elementdef);
  320. end;
  321. procedure TLLVMTypeInfo.appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
  322. var
  323. symdeflist: tfpobjectlist;
  324. i: longint;
  325. begin
  326. symdeflist:=tabstractrecordsymtable(def.symtable).llvmst.symdeflist;
  327. for i:=0 to symdeflist.Count-1 do
  328. appenddef(list,tllvmshadowsymtableentry(symdeflist[i]).def);
  329. if assigned(def.typesym) then
  330. list.concat(taillvm.op_size(LA_TYPE,record_def(def)));
  331. end;
  332. procedure TLLVMTypeInfo.appenddef_record(list:TAsmList;def:trecorddef);
  333. begin
  334. appenddef_abstractrecord(list,def);
  335. end;
  336. procedure TLLVMTypeInfo.appenddef_pointer(list:TAsmList;def:tpointerdef);
  337. begin
  338. appenddef(list,def.pointeddef);
  339. end;
  340. procedure TLLVMTypeInfo.appenddef_procvar(list:TAsmList;def:tprocvardef);
  341. var
  342. i: longint;
  343. begin
  344. { todo: handle mantis #25551; there is no way to create a symbolic
  345. la_type for a procvardef (unless it's a procedure of object/record),
  346. which means that recursive references should become plain "procedure"
  347. types that are then casted to the real type when they are used }
  348. for i:=0 to def.paras.count-1 do
  349. appenddef(list,tparavarsym(def.paras[i]).vardef);
  350. appenddef(list,def.returndef);
  351. if assigned(def.typesym) and
  352. not def.is_addressonly then
  353. list.concat(taillvm.op_size(LA_TYPE,record_def(def)));
  354. end;
  355. procedure TLLVMTypeInfo.appendprocdef(list:TAsmList;def:tprocdef);
  356. begin
  357. { the procdef itself is already written by appendprocdef_implicit }
  358. { last write the types from this procdef }
  359. if assigned(def.parast) then
  360. write_symtable_defs(current_asmdata.asmlists[al_start],def.parast);
  361. if assigned(def.localst) and
  362. (def.localst.symtabletype=localsymtable) then
  363. write_symtable_defs(current_asmdata.asmlists[al_start],def.localst);
  364. end;
  365. procedure TLLVMTypeInfo.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  366. begin
  367. appenddef(list,sym.vardef);
  368. end;
  369. procedure TLLVMTypeInfo.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  370. begin
  371. appendsym_var(list,sym);
  372. end;
  373. procedure TLLVMTypeInfo.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  374. begin
  375. appendsym_var(list,sym);
  376. end;
  377. procedure TLLVMTypeInfo.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  378. begin
  379. appendsym_var(list,sym);
  380. end;
  381. procedure TLLVMTypeInfo.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  382. begin
  383. appenddef(list,sym.vardef);
  384. end;
  385. procedure TLLVMTypeInfo.appendsym_const(list:TAsmList;sym:tconstsym);
  386. begin
  387. appenddef(list,sym.constdef);
  388. end;
  389. procedure TLLVMTypeInfo.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  390. begin
  391. appenddef(list,sym.vardef);
  392. end;
  393. procedure TLLVMTypeInfo.inserttypeinfo;
  394. procedure write_defs_to_write;
  395. var
  396. n : integer;
  397. looplist,
  398. templist: TFPObjectList;
  399. def : tdef;
  400. begin
  401. templist := TFPObjectList.Create(False);
  402. looplist := deftowritelist;
  403. while looplist.count > 0 do
  404. begin
  405. deftowritelist := templist;
  406. for n := 0 to looplist.count - 1 do
  407. begin
  408. def := tdef(looplist[n]);
  409. case def.dbg_state of
  410. dbg_state_written:
  411. continue;
  412. dbg_state_writing:
  413. internalerror(200610052);
  414. dbg_state_unused:
  415. internalerror(200610053);
  416. dbg_state_used:
  417. appenddef(current_asmdata.asmlists[al_start],def)
  418. else
  419. internalerror(200610054);
  420. end;
  421. end;
  422. looplist.clear;
  423. templist := looplist;
  424. looplist := deftowritelist;
  425. end;
  426. templist.free;
  427. end;
  428. var
  429. storefilepos: tfileposinfo;
  430. def: tdef;
  431. i: longint;
  432. hal: tasmlisttype;
  433. begin
  434. storefilepos:=current_filepos;
  435. current_filepos:=current_module.mainfilepos;
  436. defnumberlist:=TFPObjectList.create(false);
  437. deftowritelist:=TFPObjectList.create(false);
  438. { write all global/static variables, part of flaggin all required tdefs }
  439. if assigned(current_module.globalsymtable) then
  440. write_symtable_syms(current_asmdata.asmlists[al_start],current_module.globalsymtable);
  441. if assigned(current_module.localsymtable) then
  442. write_symtable_syms(current_asmdata.asmlists[al_start],current_module.localsymtable);
  443. { write all procedures and methods, part of flagging all required tdefs }
  444. if assigned(current_module.globalsymtable) then
  445. write_symtable_procdefs(current_asmdata.asmlists[al_start],current_module.globalsymtable);
  446. if assigned(current_module.localsymtable) then
  447. write_symtable_procdefs(current_asmdata.asmlists[al_start],current_module.localsymtable);
  448. { process all llvm instructions, part of flagging all required tdefs }
  449. for hal:=low(TasmlistType) to high(TasmlistType) do
  450. if hal<>al_start then
  451. collect_asmlist_info(current_asmdata.asmlists[al_start],current_asmdata.asmlists[hal]);
  452. for hal:=low(TasmlistType) to high(TasmlistType) do
  453. if hal<>al_start then
  454. insert_asmlist_typeconversions(current_asmdata.asmlists[hal]);
  455. { write all used defs }
  456. write_defs_to_write;
  457. { reset all def labels }
  458. for i:=0 to defnumberlist.count-1 do
  459. begin
  460. def := tdef(defnumberlist[i]);
  461. if assigned(def) then
  462. begin
  463. def.dbg_state:=dbg_state_unused;
  464. end;
  465. end;
  466. defnumberlist.free;
  467. defnumberlist:=nil;
  468. deftowritelist.free;
  469. deftowritelist:=nil;
  470. current_filepos:=storefilepos;
  471. end;
  472. procedure TLLVMTypeInfo.appenddef_object(list:TAsmList;def: tobjectdef);
  473. begin
  474. appenddef_abstractrecord(list,def);
  475. end;
  476. procedure TLLVMTypeInfo.appenddef_variant(list:TAsmList;def: tvariantdef);
  477. begin
  478. appenddef(list,tabstractrecorddef(search_system_type('TVARDATA').typedef));
  479. end;
  480. end.