llvmtype.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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; redefine: boolean);
  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_classref(list: TAsmList; def: tclassrefdef);override;
  55. procedure appenddef_variant(list:TAsmList;def: tvariantdef);override;
  56. procedure appenddef_file(list:TasmList;def:tfiledef);override;
  57. procedure appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  58. procedure appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);override;
  59. procedure appendsym_paravar(list:TAsmList;sym:tparavarsym);override;
  60. procedure appendsym_localvar(list:TAsmList;sym:tlocalvarsym);override;
  61. procedure appendsym_fieldvar(list:TAsmList;sym:tfieldvarsym);override;
  62. procedure appendsym_const(list:TAsmList;sym:tconstsym);override;
  63. procedure appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);override;
  64. procedure enum_membersyms_callback(p:TObject;arg:pointer);
  65. procedure collect_llvmins_info(deftypelist: tasmlist; p: taillvm);
  66. procedure collect_tai_info(deftypelist: tasmlist; p: tai);
  67. procedure collect_asmlist_info(deftypelist, asmlist: tasmlist);
  68. procedure insert_llvmins_typeconversions(toplevellist: tasmlist; p: taillvm);
  69. procedure insert_typedconst_typeconversion(toplevellist: tasmlist; p: tai_abstracttypedconst);
  70. procedure insert_tai_typeconversions(toplevellist: tasmlist; p: tai);
  71. procedure insert_asmlist_typeconversions(toplevellist, list: tasmlist);
  72. procedure maybe_insert_extern_sym_decl(toplevellist: tasmlist; sym: tasmsymbol; def: tdef);
  73. procedure update_asmlist_alias_types(list: tasmlist);
  74. public
  75. constructor Create;override;
  76. destructor Destroy;override;
  77. procedure inserttypeinfo;override;
  78. end;
  79. implementation
  80. uses
  81. sysutils,cutils,cfileutl,constexp,
  82. version,globals,verbose,systems,
  83. cpubase,cgbase,paramgr,
  84. fmodule,nobj,
  85. defutil,defcmp,symconst,symtable,
  86. llvmbase,llvmdef
  87. ;
  88. {****************************************************************************
  89. TDebugInfoDwarf
  90. ****************************************************************************}
  91. procedure TLLVMTypeInfo.record_asmsym_def(sym: TAsmSymbol; def: tdef; redefine: boolean);
  92. var
  93. res: PHashSetItem;
  94. begin
  95. record_def(def);
  96. res:=asmsymtypes.FindOrAdd(@sym,sizeof(sym));
  97. { due to internal aliases with different signatures, we may end up with
  98. multiple defs for the same symbol -> use the one from the declaration,
  99. and insert typecasts as necessary elsewhere }
  100. if redefine or
  101. not assigned(res^.Data) then
  102. res^.Data:=def;
  103. end;
  104. function TLLVMTypeInfo.get_asmsym_def(sym: TAsmSymbol): tdef;
  105. var
  106. res: PHashSetItem;
  107. begin
  108. res:=asmsymtypes.Find(@sym,sizeof(sym));
  109. { we must have a def for every used asmsym }
  110. if not assigned(res) or
  111. not assigned(res^.data) then
  112. internalerror(2015042701);
  113. result:=tdef(res^.Data);
  114. end;
  115. function TLLVMTypeInfo.record_def(def:tdef): tdef;
  116. begin
  117. result:=def;
  118. if def.stab_number<>0 then
  119. exit;
  120. def.stab_number:=1;
  121. if def.dbg_state=dbg_state_unused then
  122. begin
  123. def.dbg_state:=dbg_state_used;
  124. deftowritelist.Add(def);
  125. end;
  126. defnumberlist.Add(def);
  127. end;
  128. constructor TLLVMTypeInfo.Create;
  129. begin
  130. inherited Create;
  131. asmsymtypes:=THashSet.Create(current_asmdata.AsmSymbolDict.Count,true,false);
  132. end;
  133. destructor TLLVMTypeInfo.Destroy;
  134. begin
  135. asmsymtypes.free;
  136. inherited destroy;
  137. end;
  138. procedure TLLVMTypeInfo.enum_membersyms_callback(p:TObject; arg: pointer);
  139. begin
  140. case tsym(p).typ of
  141. fieldvarsym:
  142. appendsym_fieldvar(TAsmList(arg),tfieldvarsym(p));
  143. else
  144. ;
  145. end;
  146. end;
  147. procedure TLLVMTypeInfo.collect_llvmins_info(deftypelist: tasmlist; p: taillvm);
  148. var
  149. opidx, paraidx: longint;
  150. callpara: pllvmcallpara;
  151. begin
  152. for opidx:=0 to p.ops-1 do
  153. case p.oper[opidx]^.typ of
  154. top_def:
  155. record_def(p.oper[opidx]^.def);
  156. top_tai:
  157. collect_tai_info(deftypelist,p.oper[opidx]^.ai);
  158. top_ref:
  159. begin
  160. if (p.llvmopcode<>la_br) and
  161. assigned(p.oper[opidx]^.ref^.symbol) and
  162. (p.oper[opidx]^.ref^.symbol.bind<>AB_TEMP) then
  163. begin
  164. if (opidx=3) and
  165. (p.llvmopcode=la_call) then
  166. record_asmsym_def(p.oper[opidx]^.ref^.symbol,tpointerdef(p.oper[2]^.def).pointeddef,false)
  167. { not a named register }
  168. else if (p.oper[opidx]^.ref^.refaddr<>addr_full) then
  169. record_asmsym_def(p.oper[opidx]^.ref^.symbol,p.spilling_get_reg_type(opidx),false);
  170. end;
  171. end;
  172. top_para:
  173. for paraidx:=0 to p.oper[opidx]^.paras.count-1 do
  174. begin
  175. callpara:=pllvmcallpara(p.oper[opidx]^.paras[paraidx]);
  176. record_def(callpara^.def);
  177. end;
  178. else
  179. ;
  180. end;
  181. end;
  182. procedure TLLVMTypeInfo.collect_tai_info(deftypelist: tasmlist; p: tai);
  183. var
  184. value: tai_abstracttypedconst;
  185. begin
  186. if not assigned(p) then
  187. exit;
  188. case p.typ of
  189. ait_llvmalias:
  190. begin
  191. record_asmsym_def(taillvmalias(p).newsym,taillvmalias(p).def,true);
  192. end;
  193. ait_llvmdecl:
  194. begin
  195. record_asmsym_def(taillvmdecl(p).namesym,taillvmdecl(p).def,true);
  196. collect_asmlist_info(deftypelist,taillvmdecl(p).initdata);
  197. end;
  198. ait_llvmins:
  199. collect_llvmins_info(deftypelist,taillvm(p));
  200. ait_typedconst:
  201. begin
  202. record_def(tai_abstracttypedconst(p).def);
  203. case tai_abstracttypedconst(p).adetyp of
  204. tck_simple:
  205. collect_tai_info(deftypelist,tai_simpletypedconst(p).val);
  206. tck_array,tck_record:
  207. for value in tai_aggregatetypedconst(p) do
  208. collect_tai_info(deftypelist,value);
  209. end;
  210. end;
  211. else
  212. ;
  213. end;
  214. end;
  215. procedure TLLVMTypeInfo.collect_asmlist_info(deftypelist, asmlist: tasmlist);
  216. var
  217. hp: tai;
  218. begin
  219. if not assigned(asmlist) then
  220. exit;
  221. hp:=tai(asmlist.first);
  222. while assigned(hp) do
  223. begin
  224. collect_tai_info(deftypelist,hp);
  225. hp:=tai(hp.next);
  226. end;
  227. end;
  228. function equal_llvm_defs(def1, def2: tdef): boolean;
  229. var
  230. def1str, def2str: TSymStr;
  231. begin
  232. if def1=def2 then
  233. exit(true);
  234. def1str:=llvmencodetypename(def1);
  235. def2str:=llvmencodetypename(def2);
  236. { normalise both type representations in case one is a procdef
  237. and the other is a procvardef}
  238. if def1.typ=procdef then
  239. def1str:=def1str+'*';
  240. if def2.typ=procdef then
  241. def2str:=def2str+'*';
  242. result:=def1str=def2str;
  243. end;
  244. procedure TLLVMTypeInfo.insert_llvmins_typeconversions(toplevellist: tasmlist; p: taillvm);
  245. var
  246. symdef,
  247. opdef,
  248. opcmpdef: tdef;
  249. cnv: taillvm;
  250. i: longint;
  251. begin
  252. case p.llvmopcode of
  253. la_call:
  254. if p.oper[3]^.typ=top_ref then
  255. begin
  256. maybe_insert_extern_sym_decl(toplevellist,p.oper[3]^.ref^.symbol,tpointerdef(p.oper[2]^.def).pointeddef);
  257. symdef:=get_asmsym_def(p.oper[3]^.ref^.symbol);
  258. { the type used in the call is different from the type used to
  259. declare the symbol -> insert a typecast }
  260. if not equal_llvm_defs(symdef,p.oper[2]^.def) then
  261. begin
  262. if symdef.typ=procdef then
  263. { ugly, but can't use getcopyas(procvardef) due to the
  264. symtablestack not being available here (cpointerdef.getreusable
  265. is hardcoded to put things in the current module's
  266. symtable) and "pointer to procedure" results in the
  267. correct llvm type }
  268. symdef:=cpointerdef.getreusable(tprocdef(symdef));
  269. cnv:=taillvm.op_reg_size_sym_size(la_bitcast,NR_NO,symdef,p.oper[3]^.ref^.symbol,p.oper[2]^.def);
  270. p.loadtai(3,cnv);
  271. end;
  272. end;
  273. else if p.llvmopcode<>la_br then
  274. begin
  275. { check the types of all symbolic operands }
  276. for i:=0 to p.ops-1 do
  277. case p.oper[i]^.typ of
  278. top_ref:
  279. if (p.oper[i]^.ref^.refaddr<>addr_full) and
  280. assigned(p.oper[i]^.ref^.symbol) and
  281. (p.oper[i]^.ref^.symbol.bind<>AB_TEMP) then
  282. begin
  283. opdef:=p.spilling_get_reg_type(i);
  284. case opdef.typ of
  285. pointerdef:
  286. opcmpdef:=tpointerdef(opdef).pointeddef;
  287. procvardef,
  288. procdef:
  289. opcmpdef:=opdef;
  290. else
  291. internalerror(2015073101);
  292. end;
  293. maybe_insert_extern_sym_decl(toplevellist,p.oper[i]^.ref^.symbol,opcmpdef);
  294. symdef:=get_asmsym_def(p.oper[i]^.ref^.symbol);
  295. if not equal_llvm_defs(symdef,opcmpdef) then
  296. begin
  297. if symdef.typ=procdef then
  298. symdef:=cpointerdef.getreusable(symdef);
  299. cnv:=taillvm.op_reg_size_sym_size(la_bitcast,NR_NO,cpointerdef.getreusable(symdef),p.oper[i]^.ref^.symbol,opdef);
  300. p.loadtai(i,cnv);
  301. end;
  302. end;
  303. top_tai:
  304. insert_tai_typeconversions(toplevellist,p.oper[i]^.ai);
  305. else
  306. ;
  307. end;
  308. end;
  309. end;
  310. end;
  311. procedure TLLVMTypeInfo.insert_typedconst_typeconversion(toplevellist: tasmlist; p: tai_abstracttypedconst);
  312. var
  313. symdef: tdef;
  314. cnv: taillvm;
  315. elementp: tai_abstracttypedconst;
  316. begin
  317. case p.adetyp of
  318. tck_simple:
  319. begin
  320. case tai_simpletypedconst(p).val.typ of
  321. ait_const:
  322. if assigned(tai_const(tai_simpletypedconst(p).val).sym) and
  323. not assigned(tai_const(tai_simpletypedconst(p).val).endsym) then
  324. begin
  325. maybe_insert_extern_sym_decl(toplevellist,tai_const(tai_simpletypedconst(p).val).sym,p.def);
  326. symdef:=get_asmsym_def(tai_const(tai_simpletypedconst(p).val).sym);
  327. { all references to symbols in typed constants are
  328. references to the address of a global symbol (you can't
  329. refer to the data itself, just like you can't initialise
  330. a Pascal (typed) constant with the contents of another
  331. typed constant) }
  332. symdef:=cpointerdef.getreusable(symdef);
  333. if not equal_llvm_defs(symdef,p.def) then
  334. begin
  335. cnv:=taillvm.op_reg_tai_size(la_bitcast,NR_NO,tai_simpletypedconst.create(symdef,tai_simpletypedconst(p).val),p.def);
  336. tai_simpletypedconst(p).val:=cnv;
  337. end;
  338. end;
  339. else
  340. insert_tai_typeconversions(toplevellist,tai_simpletypedconst(p).val);
  341. end;
  342. end;
  343. tck_array,
  344. tck_record:
  345. begin
  346. for elementp in tai_aggregatetypedconst(p) do
  347. insert_typedconst_typeconversion(toplevellist,elementp);
  348. end;
  349. end;
  350. end;
  351. procedure TLLVMTypeInfo.insert_tai_typeconversions(toplevellist: tasmlist; p: tai);
  352. begin
  353. if not assigned(p) then
  354. exit;
  355. case p.typ of
  356. ait_llvmins:
  357. insert_llvmins_typeconversions(toplevellist,taillvm(p));
  358. { can also be necessary in case someone initialises a typed const with
  359. the address of an external symbol aliasing one declared with a
  360. different type in the same mmodule. }
  361. ait_typedconst:
  362. insert_typedconst_typeconversion(toplevellist,tai_abstracttypedconst(p));
  363. ait_llvmdecl:
  364. insert_asmlist_typeconversions(toplevellist,taillvmdecl(p).initdata);
  365. else
  366. ;
  367. end;
  368. end;
  369. procedure TLLVMTypeInfo.insert_asmlist_typeconversions(toplevellist, list: tasmlist);
  370. var
  371. hp: tai;
  372. begin
  373. if not assigned(list) then
  374. exit;
  375. hp:=tai(list.first);
  376. while assigned(hp) do
  377. begin
  378. insert_tai_typeconversions(toplevellist,hp);
  379. hp:=tai(hp.next);
  380. end;
  381. end;
  382. procedure TLLVMTypeInfo.maybe_insert_extern_sym_decl(toplevellist: tasmlist; sym: tasmsymbol; def: tdef);
  383. var
  384. sec: tasmsectiontype;
  385. begin
  386. { Necessery for "external" declarations for symbols not declared in the
  387. current unit. We can't create these declarations when the alias is
  388. initially generated, because the symbol may still be defined later at
  389. that point.
  390. We also do it for all other external symbol references (e.g.
  391. references to symbols declared in other units), because then this
  392. handling is centralised in one place. }
  393. if not(sym.declared) then
  394. begin
  395. if def.typ=procdef then
  396. sec:=sec_code
  397. else
  398. sec:=sec_data;
  399. toplevellist.Concat(taillvmdecl.createdecl(sym,def,nil,sec,def.alignment));
  400. record_asmsym_def(sym,def,true);
  401. end;
  402. end;
  403. procedure TLLVMTypeInfo.update_asmlist_alias_types(list: tasmlist);
  404. var
  405. hp: tai;
  406. def: tdef;
  407. begin
  408. if not assigned(list) then
  409. exit;
  410. hp:=tai(list.first);
  411. while assigned(hp) do
  412. begin
  413. case hp.typ of
  414. ait_llvmalias:
  415. begin
  416. { replace the def of the alias declaration with the def of
  417. the aliased symbol -> we'll insert the appropriate type
  418. conversions for all uses of this symbol in the code (since
  419. every use also specifies the used type) }
  420. record_asmsym_def(taillvmalias(hp).oldsym,taillvmalias(hp).def,false);
  421. def:=get_asmsym_def(taillvmalias(hp).oldsym);
  422. if taillvmalias(hp).def<>def then
  423. begin
  424. taillvmalias(hp).def:=def;
  425. record_asmsym_def(taillvmalias(hp).newsym,def,true);
  426. end;
  427. end;
  428. ait_llvmdecl:
  429. update_asmlist_alias_types(taillvmdecl(hp).initdata);
  430. else
  431. ;
  432. end;
  433. hp:=tai(hp.next);
  434. end;
  435. end;
  436. procedure TLLVMTypeInfo.appenddef_array(list:TAsmList;def:tarraydef);
  437. begin
  438. record_def(def);
  439. appenddef(list,def.elementdef);
  440. end;
  441. procedure TLLVMTypeInfo.appenddef_abstractrecord(list:TAsmList;def:tabstractrecorddef);
  442. var
  443. symdeflist: tfpobjectlist;
  444. i: longint;
  445. begin
  446. record_def(def);
  447. symdeflist:=tabstractrecordsymtable(def.symtable).llvmst.symdeflist;
  448. for i:=0 to symdeflist.Count-1 do
  449. record_def(tllvmshadowsymtableentry(symdeflist[i]).def);
  450. if assigned(def.typesym) then
  451. list.concat(taillvm.op_size(LA_TYPE,record_def(def)));
  452. end;
  453. procedure TLLVMTypeInfo.appenddef_record(list:TAsmList;def:trecorddef);
  454. begin
  455. appenddef_abstractrecord(list,def);
  456. end;
  457. procedure TLLVMTypeInfo.appenddef_pointer(list:TAsmList;def:tpointerdef);
  458. begin
  459. record_def(def);
  460. appenddef(list,def.pointeddef);
  461. end;
  462. procedure TLLVMTypeInfo.appenddef_procvar(list:TAsmList;def:tprocvardef);
  463. var
  464. i: longint;
  465. begin
  466. record_def(def);
  467. { todo: handle mantis #25551; there is no way to create a symbolic
  468. la_type for a procvardef (unless it's a procedure of object/record),
  469. which means that recursive references should become plain "procedure"
  470. types that are then casted to the real type when they are used }
  471. def.init_paraloc_info(callerside);
  472. for i:=0 to def.paras.count-1 do
  473. appenddef(list,llvmgetcgparadef(tparavarsym(def.paras[i]).paraloc[callerside],true));
  474. appenddef(list,llvmgetcgparadef(def.funcretloc[callerside],true));
  475. if assigned(def.typesym) and
  476. not def.is_addressonly then
  477. list.concat(taillvm.op_size(LA_TYPE,record_def(def)));
  478. end;
  479. procedure TLLVMTypeInfo.appendprocdef(list:TAsmList;def:tprocdef);
  480. begin
  481. { the procdef itself is already written by appendprocdef_implicit }
  482. { last write the types from this procdef }
  483. if assigned(def.parast) then
  484. write_symtable_defs(current_asmdata.asmlists[al_start],def.parast);
  485. if assigned(def.localst) and
  486. (def.localst.symtabletype=localsymtable) then
  487. write_symtable_defs(current_asmdata.asmlists[al_start],def.localst);
  488. end;
  489. procedure TLLVMTypeInfo.appendsym_var(list:TAsmList;sym:tabstractnormalvarsym);
  490. begin
  491. appenddef(list,sym.vardef);
  492. end;
  493. procedure TLLVMTypeInfo.appendsym_staticvar(list:TAsmList;sym:tstaticvarsym);
  494. begin
  495. appendsym_var(list,sym);
  496. end;
  497. procedure TLLVMTypeInfo.appendsym_localvar(list:TAsmList;sym:tlocalvarsym);
  498. begin
  499. appendsym_var(list,sym);
  500. end;
  501. procedure TLLVMTypeInfo.appendsym_paravar(list:TAsmList;sym:tparavarsym);
  502. begin
  503. appendsym_var(list,sym);
  504. end;
  505. procedure TLLVMTypeInfo.appendsym_fieldvar(list:TAsmList;sym: tfieldvarsym);
  506. begin
  507. appenddef(list,sym.vardef);
  508. end;
  509. procedure TLLVMTypeInfo.appendsym_const(list:TAsmList;sym:tconstsym);
  510. begin
  511. appenddef(list,sym.constdef);
  512. end;
  513. procedure TLLVMTypeInfo.appendsym_absolute(list:TAsmList;sym:tabsolutevarsym);
  514. begin
  515. appenddef(list,sym.vardef);
  516. end;
  517. procedure TLLVMTypeInfo.inserttypeinfo;
  518. procedure write_defs_to_write;
  519. var
  520. n : integer;
  521. looplist,
  522. templist: TFPObjectList;
  523. def : tdef;
  524. begin
  525. templist := TFPObjectList.Create(False);
  526. looplist := deftowritelist;
  527. while looplist.count > 0 do
  528. begin
  529. deftowritelist := templist;
  530. for n := 0 to looplist.count - 1 do
  531. begin
  532. def := tdef(looplist[n]);
  533. case def.dbg_state of
  534. dbg_state_written:
  535. continue;
  536. dbg_state_writing:
  537. internalerror(200610052);
  538. dbg_state_unused:
  539. internalerror(200610053);
  540. dbg_state_used:
  541. appenddef(current_asmdata.asmlists[al_start],def)
  542. else
  543. internalerror(200610054);
  544. end;
  545. end;
  546. looplist.clear;
  547. templist := looplist;
  548. looplist := deftowritelist;
  549. end;
  550. templist.free;
  551. end;
  552. var
  553. storefilepos: tfileposinfo;
  554. def: tdef;
  555. i: longint;
  556. hal: tasmlisttype;
  557. begin
  558. if cs_no_regalloc in current_settings.globalswitches then
  559. exit;
  560. storefilepos:=current_filepos;
  561. current_filepos:=current_module.mainfilepos;
  562. defnumberlist:=TFPObjectList.create(false);
  563. deftowritelist:=TFPObjectList.create(false);
  564. { write all global/static variables, part of flaggin all required tdefs }
  565. if assigned(current_module.globalsymtable) then
  566. write_symtable_syms(current_asmdata.asmlists[al_start],current_module.globalsymtable);
  567. if assigned(current_module.localsymtable) then
  568. write_symtable_syms(current_asmdata.asmlists[al_start],current_module.localsymtable);
  569. { write all procedures and methods, part of flagging all required tdefs }
  570. if assigned(current_module.globalsymtable) then
  571. write_symtable_procdefs(current_asmdata.asmlists[al_start],current_module.globalsymtable);
  572. if assigned(current_module.localsymtable) then
  573. write_symtable_procdefs(current_asmdata.asmlists[al_start],current_module.localsymtable);
  574. { process all llvm instructions, part of flagging all required tdefs }
  575. for hal:=low(TasmlistType) to high(TasmlistType) do
  576. if hal<>al_start then
  577. collect_asmlist_info(current_asmdata.asmlists[al_start],current_asmdata.asmlists[hal]);
  578. { update the defs of all alias declarations so they match those of the
  579. declarations of the symbols they alias }
  580. for hal:=low(TasmlistType) to high(TasmlistType) do
  581. if hal<>al_start then
  582. update_asmlist_alias_types(current_asmdata.asmlists[hal]);
  583. { and insert the necessary type conversions }
  584. for hal:=low(TasmlistType) to high(TasmlistType) do
  585. if hal<>al_start then
  586. insert_asmlist_typeconversions(
  587. current_asmdata.asmlists[hal],
  588. current_asmdata.asmlists[hal]);
  589. { write all used defs }
  590. write_defs_to_write;
  591. { reset all def labels }
  592. for i:=0 to defnumberlist.count-1 do
  593. begin
  594. def:=tdef(defnumberlist[i]);
  595. def.dbg_state:=dbg_state_unused;
  596. def.stab_number:=0;
  597. end;
  598. defnumberlist.free;
  599. defnumberlist:=nil;
  600. deftowritelist.free;
  601. deftowritelist:=nil;
  602. current_filepos:=storefilepos;
  603. end;
  604. procedure TLLVMTypeInfo.appenddef_object(list:TAsmList;def: tobjectdef);
  605. begin
  606. if is_interface(def) then
  607. begin
  608. record_def(def);
  609. record_def(def.vmt_def);
  610. end
  611. else
  612. appenddef_abstractrecord(list,def);
  613. end;
  614. procedure TLLVMTypeInfo.appenddef_classref(list: TAsmList; def: tclassrefdef);
  615. begin
  616. record_def(def);
  617. { can also be an objcclass, which doesn't have a vmt }
  618. if is_class(tclassrefdef(def).pointeddef) then
  619. record_def(tobjectdef(tclassrefdef(def).pointeddef).vmt_def);
  620. end;
  621. procedure TLLVMTypeInfo.appenddef_variant(list:TAsmList;def: tvariantdef);
  622. begin
  623. record_def(def);
  624. appenddef(list,tabstractrecorddef(search_system_type('TVARDATA').typedef));
  625. end;
  626. procedure TLLVMTypeInfo.appenddef_file(list:TAsmList;def:tfiledef);
  627. begin
  628. record_def(def);
  629. case tfiledef(def).filetyp of
  630. ft_text :
  631. appenddef(list,tabstractrecorddef(search_system_type('TEXTREC').typedef));
  632. ft_typed,
  633. ft_untyped :
  634. appenddef(list,tabstractrecorddef(search_system_type('FILEREC').typedef));
  635. end;
  636. end;
  637. end.