llvmtype.pas 25 KB

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