llvmtype.pas 29 KB

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