nllvmtcon.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. {
  2. Copyright (c) 2014 by Jonas Maebe
  3. Generates code for typed constant declarations for the LLVM target
  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. unit nllvmtcon;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,constexp,globtype,
  22. aasmbase,aasmtai,aasmcnst,aasmllvm,
  23. symconst,symbase,symtype,symdef,symsym,
  24. ngtcon;
  25. type
  26. tllvmaggregateinformation = class(taggregateinformation)
  27. private
  28. faggai: tai_aggregatetypedconst;
  29. fanonrecalignpos: longint;
  30. { if this is a non-anonymous record, keep track of the current field at
  31. the llvm level that gets emitted, so we know when the data types of the
  32. Pascal and llvm representation don't match up (because of variant
  33. records, or because not all fields are defined at the Pascal level and
  34. the rest is zeroed) }
  35. fllvmnextfieldindex: longint;
  36. fdoesnotmatchllvmdef: boolean;
  37. public
  38. constructor create(_def: tdef; _typ: ttypedconstkind); override;
  39. function prepare_next_field(nextfielddef: tdef): asizeint; override;
  40. property aggai: tai_aggregatetypedconst read faggai write faggai;
  41. property anonrecalignpos: longint read fanonrecalignpos write fanonrecalignpos;
  42. property llvmnextfieldindex: longint read fllvmnextfieldindex write fllvmnextfieldindex;
  43. property doesnotmatchllvmdef: boolean read fdoesnotmatchllvmdef write fdoesnotmatchllvmdef;
  44. end;
  45. tllvmtypedconstplaceholder = class(ttypedconstplaceholder)
  46. agginfo: tllvmaggregateinformation;
  47. pos: longint;
  48. constructor create(info: tllvmaggregateinformation; p: longint; d: tdef);
  49. procedure replace(ai: tai; d: tdef); override;
  50. end;
  51. tllvmtai_typedconstbuilder = class(ttai_typedconstbuilder)
  52. public
  53. { set the default value for caggregateinformation (= tllvmaggregateinformation) }
  54. class constructor classcreate;
  55. protected
  56. foverriding_def: tdef;
  57. fappendingdef: boolean;
  58. fqueued_tai,
  59. flast_added_tai: tai;
  60. fqueued_tai_opidx: longint;
  61. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); override;
  62. { outerai: the ai that should become fqueued_tai in case it's still nil,
  63. or that should be filled in the fqueued_tai_opidx of the current
  64. fqueued_tai if it's not nil
  65. innerai: the innermost ai (possibly an operand of outerai) in which
  66. newindex indicates which operand is empty and can be filled with the
  67. next queued tai }
  68. procedure update_queued_tai(resdef: tdef; outerai, innerai: tai; newindex: longint);
  69. function wrap_with_type(p: tai; def: tdef): tai;
  70. procedure do_emit_tai(p: tai; def: tdef); override;
  71. procedure mark_anon_aggregate_alignment; override;
  72. procedure insert_marked_aggregate_alignment(def: tdef); override;
  73. procedure maybe_emit_tail_padding(def: tdef); override;
  74. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); override;
  75. procedure end_aggregate_internal(def: tdef; anonymous: boolean); override;
  76. function get_internal_data_section_start_label: tasmlabel; override;
  77. function get_internal_data_section_internal_label: tasmlabel; override;
  78. procedure do_emit_extended_in_aggregate(p: tai);
  79. { mark the current agginfo, and hence also all the ones higher up in ther
  80. aggregate hierarchy, as not matching our canonical llvm definition for
  81. their def }
  82. procedure mark_aggregate_hierarchy_llvmdef_mismatch(new_current_level_def: trecorddef);
  83. public
  84. destructor destroy; override;
  85. procedure emit_tai(p: tai; def: tdef); override;
  86. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); override;
  87. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef); override;
  88. procedure emit_dynarray_offset(const ll: tasmlabofs; const arrlength: asizeint; const arrdef: tarraydef; const arrconstdatadef: trecorddef); override;
  89. procedure queue_init(todef: tdef); override;
  90. procedure queue_vecn(def: tdef; const index: tconstexprint); override;
  91. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); override;
  92. procedure queue_typeconvn(fromdef, todef: tdef); override;
  93. procedure queue_emit_staticvar(vs: tstaticvarsym); override;
  94. procedure queue_emit_label(l: tlabelsym); override;
  95. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); override;
  96. procedure queue_emit_ordconst(value: int64; def: tdef); override;
  97. class function get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; out secname: TSymStr): boolean; override;
  98. function emit_placeholder(def: tdef): ttypedconstplaceholder; override;
  99. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; override;
  100. class function get_dynarray_symofs: pint; override;
  101. property appendingdef: boolean write fappendingdef;
  102. end;
  103. implementation
  104. uses
  105. verbose,systems,fmodule,
  106. aasmdata,
  107. procinfo,
  108. cpubase,cpuinfo,llvmbase,
  109. symtable,llvmdef,defutil,defcmp;
  110. { tllvmaggregateinformation }
  111. constructor tllvmaggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  112. begin
  113. inherited;
  114. fanonrecalignpos:=-1;
  115. fllvmnextfieldindex:=0;
  116. end;
  117. function tllvmaggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  118. begin
  119. result:=inherited;
  120. { in case we let LLVM align, don't add padding ourselves }
  121. if df_llvm_no_struct_packing in def.defoptions then
  122. result:=0;
  123. end;
  124. { tllvmtypedconstplaceholder }
  125. constructor tllvmtypedconstplaceholder.create(info: tllvmaggregateinformation; p: longint; d: tdef);
  126. begin
  127. inherited create(d);
  128. agginfo:=info;
  129. pos:=p;
  130. end;
  131. procedure tllvmtypedconstplaceholder.replace(ai: tai; d: tdef);
  132. var
  133. oldconst: tai_abstracttypedconst;
  134. begin
  135. if d<>def then
  136. internalerror(2015091002);
  137. oldconst:=agginfo.aggai.replacevalueatpos(
  138. tai_simpletypedconst.create(tck_simple,d,ai),pos
  139. );
  140. oldconst.free;
  141. end;
  142. { tllvmtai_typedconstbuilder }
  143. class constructor tllvmtai_typedconstbuilder.classcreate;
  144. begin
  145. caggregateinformation:=tllvmaggregateinformation;
  146. end;
  147. procedure tllvmtai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  148. var
  149. newasmlist: tasmlist;
  150. decl: taillvmdecl;
  151. begin
  152. finalize_asmlist_prepare(options,alignment);
  153. newasmlist:=tasmlist.create;
  154. if assigned(foverriding_def) then
  155. def:=foverriding_def;
  156. { llvm declaration with as initialisation data all the elements from the
  157. original asmlist }
  158. decl:=taillvmdecl.createdef(sym,def,fasmlist,section,alignment);
  159. if fappendingdef then
  160. include(decl.flags,ldf_appending);
  161. if section=sec_user then
  162. decl.setsecname(secname);
  163. if tcalo_is_lab in options then
  164. include(decl.flags,ldf_unnamed_addr);
  165. if ([tcalo_vectorized_dead_strip_start,
  166. tcalo_vectorized_dead_strip_item,
  167. tcalo_vectorized_dead_strip_end]*options)<>[] then
  168. include(decl.flags,ldf_vectorized);
  169. if tcalo_weak in options then
  170. include(decl.flags,ldf_weak);
  171. if tcalo_no_dead_strip in options then
  172. { Objective-C section declarations already contain "no_dead_strip"
  173. attributes if none of their symbols need to be stripped -> only
  174. add the symbols to llvm.compiler.used (only affects compiler
  175. optimisations) and not to llvm.used (also affects linker -- which in
  176. this case is already taken care of by the section attribute; not sure
  177. why it's done like this, but this is how Clang does it) }
  178. if (target_info.system in systems_darwin) and
  179. (section in [low(TObjCAsmSectionType)..high(TObjCAsmSectionType)]) then
  180. current_module.llvmcompilerusedsyms.add(decl)
  181. else
  182. current_module.llvmusedsyms.add(decl);
  183. newasmlist.concat(decl);
  184. fasmlist:=newasmlist;
  185. end;
  186. procedure tllvmtai_typedconstbuilder.update_queued_tai(resdef: tdef; outerai, innerai: tai; newindex: longint);
  187. begin
  188. { the outer tai must always be a typed constant (possibly a wrapper
  189. around a taillvm or so), in order for result type information to be
  190. available }
  191. if outerai.typ<>ait_typedconst then
  192. internalerror(2014060401);
  193. { is the result of the outermost expression different from the type of
  194. this typed const? -> insert type conversion }
  195. if not assigned(fqueued_tai) and
  196. (resdef<>fqueued_def) and
  197. (llvmencodetypename(resdef)<>llvmencodetypename(fqueued_def)) then
  198. queue_typeconvn(resdef,fqueued_def);
  199. if assigned(fqueued_tai) then
  200. begin
  201. taillvm(flast_added_tai).loadtai(fqueued_tai_opidx,outerai);
  202. { already flushed? }
  203. if fqueued_tai_opidx=-1 then
  204. internalerror(2014062201);
  205. end
  206. else
  207. begin
  208. fqueued_tai:=outerai;
  209. fqueued_def:=resdef;
  210. end;
  211. fqueued_tai_opidx:=newindex;
  212. flast_added_tai:=innerai;
  213. end;
  214. function tllvmtai_typedconstbuilder.wrap_with_type(p: tai; def: tdef): tai;
  215. begin
  216. result:=tai_simpletypedconst.create(tck_simple,def,p);
  217. end;
  218. destructor tllvmtai_typedconstbuilder.destroy;
  219. begin
  220. inherited destroy;
  221. end;
  222. procedure tllvmtai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  223. var
  224. arrdef: tdef;
  225. begin
  226. { inside an aggregate, an 80 bit floating point number must be
  227. emitted as an array of 10 bytes to prevent ABI alignment and
  228. padding to 16 bytes }
  229. if (def.typ=floatdef) and
  230. (tfloatdef(def).floattype=s80real) and
  231. assigned(curagginfo) then
  232. do_emit_extended_in_aggregate(p)
  233. else
  234. inherited;
  235. end;
  236. procedure tllvmtai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  237. var
  238. ai: tai;
  239. stc: tai_abstracttypedconst;
  240. kind: ttypedconstkind;
  241. info: tllvmaggregateinformation;
  242. begin
  243. if queue_is_active then
  244. begin
  245. kind:=tck_simple;
  246. { finalise the queued expression }
  247. ai:=tai_simpletypedconst.create(kind,def,p);
  248. { set the new index to -1, so we internalerror should we try to
  249. add anything further }
  250. update_queued_tai(def,ai,ai,-1);
  251. { and emit it }
  252. stc:=tai_abstracttypedconst(fqueued_tai);
  253. def:=fqueued_def;
  254. { ensure we don't try to emit this one again }
  255. fqueued_tai:=nil;
  256. end
  257. else
  258. stc:=tai_simpletypedconst.create(tck_simple,def,p);
  259. info:=tllvmaggregateinformation(curagginfo);
  260. { these elements can be aggregates themselves, e.g. a shortstring can
  261. be emitted as a series of bytes and string data arrays }
  262. kind:=aggregate_kind(def);
  263. if (kind<>tck_simple) then
  264. begin
  265. if not assigned(info) or
  266. (info.aggai.adetyp<>kind) then
  267. internalerror(2014052906);
  268. end;
  269. if assigned(info) then
  270. begin
  271. { are we emitting data that does not match the equivalent data in
  272. the llvm structure? If so, record this so that we know we have to
  273. use a custom recorddef to emit this data }
  274. if not(info.anonrecord) and
  275. (info.def.typ<>procvardef) and
  276. (aggregate_kind(info.def)=tck_record) and
  277. not info.doesnotmatchllvmdef then
  278. begin
  279. if (info.llvmnextfieldindex>=tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).llvmst.symdeflist.count) or
  280. not equal_defs(def,tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).llvmst.entries_by_llvm_index[info.llvmnextfieldindex].def) then
  281. info.doesnotmatchllvmdef:=true
  282. else
  283. info.llvmnextfieldindex:=info.llvmnextfieldindex+1;
  284. end;
  285. info.aggai.addvalue(stc);
  286. end
  287. else
  288. inherited do_emit_tai(stc,def);
  289. end;
  290. procedure tllvmtai_typedconstbuilder.mark_anon_aggregate_alignment;
  291. var
  292. info: tllvmaggregateinformation;
  293. begin
  294. info:=tllvmaggregateinformation(curagginfo);
  295. info.anonrecalignpos:=info.aggai.valuecount;
  296. end;
  297. procedure tllvmtai_typedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  298. var
  299. info: tllvmaggregateinformation;
  300. fillbytes: asizeint;
  301. begin
  302. info:=tllvmaggregateinformation(curagginfo);
  303. if info.anonrecalignpos=-1 then
  304. internalerror(2014091501);
  305. fillbytes:=info.prepare_next_field(def);
  306. while fillbytes>0 do
  307. begin
  308. info.aggai.insertvaluebeforepos(tai_simpletypedconst.create(tck_simple,u8inttype,tai_const.create_8bit(0)),info.anonrecalignpos);
  309. dec(fillbytes);
  310. end;
  311. end;
  312. procedure tllvmtai_typedconstbuilder.maybe_emit_tail_padding(def: tdef);
  313. var
  314. info: tllvmaggregateinformation;
  315. constdata: tai_abstracttypedconst;
  316. newdef: trecorddef;
  317. begin
  318. { in case we let LLVM align, don't add padding ourselves }
  319. if df_llvm_no_struct_packing in def.defoptions then
  320. exit;
  321. inherited;
  322. { we can only check here whether the aggregate does not match our
  323. cononical llvm definition, as the tail padding may cause a mismatch
  324. (in case not all fields have been defined), and we can't do it inside
  325. end_aggregate_internal as its inherited method (which calls this
  326. method) frees curagginfo before it returns }
  327. info:=tllvmaggregateinformation(curagginfo);
  328. if info.doesnotmatchllvmdef then
  329. begin
  330. { create a new recorddef representing this mismatched def; this can
  331. even replace an array in case it contains e.g. variant records }
  332. case info.def.typ of
  333. arraydef:
  334. { in an array, all elements come right after each other ->
  335. replace with a packed record }
  336. newdef:=crecorddef.create_global_internal('',1,1,1);
  337. recorddef,
  338. objectdef:
  339. newdef:=crecorddef.create_global_internal('',
  340. tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).recordalignment,
  341. tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).recordalignmin,
  342. tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).maxCrecordalign);
  343. else
  344. internalerror(2015122401);
  345. end;
  346. for constdata in tai_aggregatetypedconst(info.aggai) do
  347. newdef.add_field_by_def('',constdata.def);
  348. tai_aggregatetypedconst(info.aggai).changetorecord(newdef);
  349. mark_aggregate_hierarchy_llvmdef_mismatch(newdef);
  350. end;
  351. end;
  352. procedure tllvmtai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  353. begin
  354. if not pvdef.is_addressonly then
  355. pvdef:=cprocvardef.getreusableprocaddr(pvdef);
  356. emit_tai(p,pvdef);
  357. end;
  358. procedure tllvmtai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  359. var
  360. srsym : tsym;
  361. srsymtable: tsymtable;
  362. strrecdef : trecorddef;
  363. strdef: tdef;
  364. offset: pint;
  365. field: tfieldvarsym;
  366. dataptrdef: tdef;
  367. begin
  368. { nil pointer? }
  369. if not assigned(ll.lab) then
  370. begin
  371. if ll.ofs<>0 then
  372. internalerror(2015030701);
  373. inherited;
  374. exit;
  375. end;
  376. { if the returned offset is <> 0, then the string data
  377. starts at that offset -> translate to a field for the
  378. high level code generator }
  379. if ll.ofs<>0 then
  380. begin
  381. { get the recorddef for this string constant }
  382. if not searchsym_type(ctai_typedconstbuilder.get_dynstring_rec_name(st,winlikewidestring,strlength),srsym,srsymtable) then
  383. internalerror(2014080406);
  384. strrecdef:=trecorddef(ttypesym(srsym).typedef);
  385. { offset in the record of the the string data }
  386. offset:=ctai_typedconstbuilder.get_string_symofs(st,winlikewidestring);
  387. { field corresponding to this offset }
  388. field:=trecordsymtable(strrecdef.symtable).findfieldbyoffset(offset);
  389. { pointerdef to the string data array }
  390. dataptrdef:=cpointerdef.getreusable(field.vardef);
  391. { the fields of the resourcestring record are declared as ansistring }
  392. strdef:=get_dynstring_def_for_type(st,winlikewidestring);
  393. queue_init(strdef);
  394. queue_typeconvn(charptrdef,strdef);
  395. queue_subscriptn(strrecdef,field);
  396. queue_emit_asmsym(ll.lab,strrecdef);
  397. end
  398. else
  399. { since llvm doesn't support labels in the middle of structs, this
  400. offset should never be 0 }
  401. internalerror(2014080506);
  402. end;
  403. procedure tllvmtai_typedconstbuilder.emit_dynarray_offset(const ll: tasmlabofs; const arrlength: asizeint; const arrdef: tarraydef; const arrconstdatadef: trecorddef);
  404. var
  405. field: tfieldvarsym;
  406. dataptrdef: tdef;
  407. begin
  408. { nil pointer? }
  409. if not assigned(ll.lab) then
  410. begin
  411. if ll.ofs<>0 then
  412. internalerror(2015030701);
  413. inherited;
  414. exit;
  415. end;
  416. { if the returned offset is <> 0, then the string data
  417. starts at that offset -> translate to a field for the
  418. high level code generator }
  419. if ll.ofs<>0 then
  420. begin
  421. { field corresponding to this offset }
  422. field:=trecordsymtable(arrconstdatadef.symtable).findfieldbyoffset(ll.ofs);
  423. { pointerdef to the string data array }
  424. dataptrdef:=cpointerdef.getreusable(field.vardef);
  425. { the fields of the resourcestring record are declared as ansistring }
  426. queue_init(dataptrdef);
  427. queue_subscriptn(arrconstdatadef,field);
  428. queue_emit_asmsym(ll.lab,arrconstdatadef);
  429. end
  430. else
  431. { since llvm doesn't support labels in the middle of structs, this
  432. offset should never be 0 }
  433. internalerror(2018112401);
  434. end;
  435. procedure tllvmtai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  436. var
  437. agg: tai_aggregatetypedconst;
  438. tck: ttypedconstkind;
  439. curagg: tllvmaggregateinformation;
  440. begin
  441. tck:=aggregate_kind(def);
  442. if tck<>tck_simple then
  443. begin
  444. { create new typed const aggregate }
  445. agg:=tai_aggregatetypedconst.create(tck,def);
  446. { either add to the current typed const aggregate (if nested), or
  447. emit to the asmlist (if top level) }
  448. curagg:=tllvmaggregateinformation(curagginfo);
  449. { create aggregate information for this new aggregate }
  450. inherited;
  451. { only add the new aggregate to the previous aggregate now, because
  452. the inherited call may have had to add padding bytes first }
  453. if assigned(curagg) then
  454. curagg.aggai.addvalue(agg)
  455. else
  456. fasmlist.concat(agg);
  457. { set new current typed const aggregate }
  458. tllvmaggregateinformation(curagginfo).aggai:=agg
  459. end
  460. else
  461. inherited;
  462. end;
  463. procedure tllvmtai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  464. var
  465. info: tllvmaggregateinformation;
  466. was_aggregate: boolean;
  467. begin
  468. was_aggregate:=false;
  469. if aggregate_kind(def)<>tck_simple then
  470. begin
  471. was_aggregate:=true;
  472. info:=tllvmaggregateinformation(curagginfo);
  473. if not assigned(info) then
  474. internalerror(2014060101);
  475. info.aggai.finish;
  476. end;
  477. inherited;
  478. info:=tllvmaggregateinformation(curagginfo);
  479. if assigned(info) and
  480. was_aggregate and
  481. not info.doesnotmatchllvmdef then
  482. begin
  483. { are we emitting data that does not match the equivalent data in
  484. the llvm structure? If so, record this so that we know we have to
  485. use a custom recorddef to emit this data }
  486. if not info.anonrecord and
  487. (aggregate_kind(info.def)=tck_record) and
  488. ((info.llvmnextfieldindex>=tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).llvmst.symdeflist.count) or
  489. not equal_defs(def,tabstractrecordsymtable(tabstractrecorddef(info.def).symtable).llvmst.entries_by_llvm_index[info.llvmnextfieldindex].def)) then
  490. info.doesnotmatchllvmdef:=true
  491. else
  492. info.llvmnextfieldindex:=info.llvmnextfieldindex+1;
  493. end;
  494. end;
  495. function tllvmtai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  496. begin
  497. { let llvm take care of everything by creating internal nameless
  498. constants }
  499. current_asmdata.getlocaldatalabel(result);
  500. end;
  501. function tllvmtai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  502. begin
  503. current_asmdata.getlocaldatalabel(result);
  504. end;
  505. procedure tllvmtai_typedconstbuilder.do_emit_extended_in_aggregate(p: tai);
  506. type
  507. p80realval =^t80realval;
  508. t80realval = packed record
  509. case byte of
  510. 0: (v: ts80real);
  511. 1: (a: array[0..9] of byte);
  512. end;
  513. var
  514. arrdef: tdef;
  515. i: longint;
  516. realval: p80realval;
  517. begin
  518. { emit as an array of 10 bytes }
  519. arrdef:=carraydef.getreusable(u8inttype,10);
  520. maybe_begin_aggregate(arrdef);
  521. if (p.typ<>ait_realconst) then
  522. internalerror(2015062401);
  523. realval:=p80realval(@tai_realconst(p).value.s80val);
  524. if target_info.endian=source_info.endian then
  525. for i:=0 to 9 do
  526. emit_tai(tai_const.Create_8bit(realval^.a[i]),u8inttype)
  527. else
  528. for i:=9 downto 0 do
  529. emit_tai(tai_const.Create_8bit(realval^.a[i]),u8inttype);
  530. maybe_end_aggregate(arrdef);
  531. { free the original constant, since we didn't emit it }
  532. p.free;
  533. end;
  534. procedure tllvmtai_typedconstbuilder.mark_aggregate_hierarchy_llvmdef_mismatch(new_current_level_def: trecorddef);
  535. var
  536. aggregate_level,
  537. i: longint;
  538. info: tllvmaggregateinformation;
  539. begin
  540. if assigned(faggregateinformation) then
  541. begin
  542. aggregate_level:=faggregateinformation.count;
  543. { the top element, at aggregate_level-1, is already marked, since
  544. that's why we are marking the rest }
  545. for i:=aggregate_level-2 downto 0 do
  546. begin
  547. info:=tllvmaggregateinformation(faggregateinformation[i]);
  548. if info.doesnotmatchllvmdef then
  549. break;
  550. info.doesnotmatchllvmdef:=true;
  551. end;
  552. if aggregate_level=1 then
  553. foverriding_def:=new_current_level_def;
  554. end;
  555. end;
  556. procedure tllvmtai_typedconstbuilder.queue_init(todef: tdef);
  557. begin
  558. inherited;
  559. fqueued_tai:=nil;
  560. flast_added_tai:=nil;
  561. fqueued_tai_opidx:=-1;
  562. end;
  563. procedure tllvmtai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  564. var
  565. ai: taillvm;
  566. aityped: tai;
  567. eledef: tdef;
  568. vecindex: asizeint;
  569. begin
  570. { update range checking info }
  571. inherited;
  572. vecindex:=index.svalue;
  573. if def.typ=arraydef then
  574. dec(vecindex,tarraydef(def).lowrange);
  575. ai:=taillvm.getelementptr_reg_tai_size_const(NR_NO,nil,ptrsinttype,vecindex,true);
  576. case def.typ of
  577. arraydef:
  578. eledef:=tarraydef(def).elementdef;
  579. stringdef:
  580. case tstringdef(def).stringtype of
  581. st_shortstring,
  582. st_longstring,
  583. st_ansistring:
  584. eledef:=cansichartype;
  585. st_widestring,
  586. st_unicodestring:
  587. eledef:=cwidechartype;
  588. else
  589. internalerror(2014062202);
  590. end;
  591. else
  592. internalerror(2014062203);
  593. end;
  594. aityped:=wrap_with_type(ai,cpointerdef.getreusable(eledef));
  595. update_queued_tai(cpointerdef.getreusable(eledef),aityped,ai,1);
  596. end;
  597. procedure tllvmtai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  598. var
  599. getllvmfieldaddr,
  600. getpascalfieldaddr,
  601. getllvmfieldaddrtyped: tai;
  602. llvmfielddef: tdef;
  603. begin
  604. { update range checking info }
  605. inherited;
  606. llvmfielddef:=tabstractrecordsymtable(def.symtable).llvmst[vs].def;
  607. { get the address of the llvm-struct field that corresponds to this
  608. Pascal field }
  609. getllvmfieldaddr:=taillvm.getelementptr_reg_tai_size_const(NR_NO,nil,s32inttype,vs.llvmfieldnr,true);
  610. { getelementptr doesn't contain its own resultdef, so encode it via a
  611. tai_simpletypedconst tai }
  612. getllvmfieldaddrtyped:=wrap_with_type(getllvmfieldaddr,cpointerdef.getreusable(llvmfielddef));
  613. { if it doesn't match the requested field exactly (variant record),
  614. fixup the result }
  615. getpascalfieldaddr:=getllvmfieldaddrtyped;
  616. if (vs.offsetfromllvmfield<>0) or
  617. (llvmfielddef<>vs.vardef) then
  618. begin
  619. { offset of real field relative to llvm-struct field <> 0? }
  620. if vs.offsetfromllvmfield<>0 then
  621. begin
  622. { convert to a pointer to a 1-sized element }
  623. if llvmfielddef.size<>1 then
  624. begin
  625. getpascalfieldaddr:=taillvm.op_reg_tai_size(la_bitcast,NR_NO,getpascalfieldaddr,u8inttype);
  626. { update the current fielddef of the expression }
  627. llvmfielddef:=u8inttype;
  628. end;
  629. { add the offset }
  630. getpascalfieldaddr:=taillvm.getelementptr_reg_tai_size_const(NR_NO,getpascalfieldaddr,ptrsinttype,vs.offsetfromllvmfield,true);
  631. { ... and set the result type of the getelementptr }
  632. getpascalfieldaddr:=wrap_with_type(getpascalfieldaddr,cpointerdef.getreusable(u8inttype));
  633. llvmfielddef:=u8inttype;
  634. end;
  635. { bitcast the data at the final offset to the right type }
  636. if llvmfielddef<>vs.vardef then
  637. getpascalfieldaddr:=wrap_with_type(taillvm.op_reg_tai_size(la_bitcast,NR_NO,getpascalfieldaddr,cpointerdef.getreusable(vs.vardef)),cpointerdef.getreusable(vs.vardef));
  638. end;
  639. update_queued_tai(cpointerdef.getreusable(vs.vardef),getpascalfieldaddr,getllvmfieldaddr,1);
  640. end;
  641. procedure tllvmtai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  642. var
  643. ai: taillvm;
  644. typedai: tai;
  645. tmpintdef: tdef;
  646. op,
  647. firstop,
  648. secondop: tllvmop;
  649. begin
  650. inherited;
  651. { special case: procdef -> procvardef/pointerdef: must take address of
  652. the procdef }
  653. if (fromdef.typ=procdef) and
  654. (todef.typ<>procdef) then
  655. fromdef:=cprocvardef.getreusableprocaddr(tprocdef(fromdef));
  656. { typecasting a pointer-sized entity to a complex procvardef -> convert
  657. to the pointer-component of the complex procvardef (not always, because
  658. e.g. a tmethod to complex procvar initialises the entire complex
  659. procvar) }
  660. if (todef.typ=procvardef) and
  661. not tprocvardef(todef).is_addressonly and
  662. (fromdef.size<todef.size) then
  663. todef:=cprocvardef.getreusableprocaddr(tprocvardef(todef));
  664. op:=llvmconvop(fromdef,todef,false);
  665. case op of
  666. la_ptrtoint_to_x,
  667. la_x_to_inttoptr:
  668. begin
  669. { convert via an integer with the same size as "x" }
  670. if op=la_ptrtoint_to_x then
  671. begin
  672. tmpintdef:=cgsize_orddef(def_cgsize(todef));
  673. firstop:=la_ptrtoint;
  674. secondop:=la_bitcast
  675. end
  676. else
  677. begin
  678. tmpintdef:=cgsize_orddef(def_cgsize(fromdef));
  679. firstop:=la_bitcast;
  680. secondop:=la_inttoptr;
  681. end;
  682. { since we have to queue operations from outer to inner, first queue
  683. the conversion from the tempintdef to the todef }
  684. ai:=taillvm.op_reg_tai_size(secondop,NR_NO,nil,todef);
  685. typedai:=wrap_with_type(ai,todef);
  686. update_queued_tai(todef,typedai,ai,1);
  687. todef:=tmpintdef;
  688. op:=firstop
  689. end;
  690. end;
  691. ai:=taillvm.op_reg_tai_size(op,NR_NO,nil,todef);
  692. typedai:=wrap_with_type(ai,todef);
  693. update_queued_tai(todef,typedai,ai,1);
  694. end;
  695. procedure tllvmtai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  696. begin
  697. { we've already incorporated the offset via the inserted operations above,
  698. make sure it doesn't get emitted again as part of the tai_const for
  699. the tasmsymbol }
  700. fqueue_offset:=0;
  701. inherited;
  702. end;
  703. procedure tllvmtai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  704. var
  705. ai: taillvm;
  706. typedai: tai;
  707. tmpintdef: tdef;
  708. op,
  709. firstop,
  710. secondop: tllvmop;
  711. begin
  712. ai:=taillvm.blockaddress(voidcodepointertype,
  713. current_asmdata.RefAsmSymbol(current_procinfo.procdef.mangledname,AT_FUNCTION),
  714. current_asmdata.RefAsmSymbol(l.mangledname,AT_LABEL)
  715. );
  716. emit_tai(ai,voidcodepointertype);
  717. fqueue_offset:=low(fqueue_offset);
  718. end;
  719. procedure tllvmtai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  720. begin
  721. { we've already incorporated the offset via the inserted operations above,
  722. make sure it doesn't get emitted again as part of the tai_const for
  723. the tasmsymbol }
  724. fqueue_offset:=0;
  725. inherited;
  726. end;
  727. procedure tllvmtai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  728. var
  729. valuedef: tdef;
  730. begin
  731. { no offset into an ordinal constant }
  732. if fqueue_offset<>0 then
  733. internalerror(2015030702);
  734. if not is_ordinal(def) then
  735. begin
  736. { insert an ordinal -> non-ordinal (e.g. pointer) conversion, as you
  737. cannot have integer constants as pointer values in LLVM }
  738. int_to_type(value,valuedef);
  739. queue_typeconvn(valuedef,def);
  740. { and now emit the constant as an ordinal }
  741. def:=valuedef;
  742. end;
  743. inherited;
  744. end;
  745. class function tllvmtai_typedconstbuilder.get_vectorized_dead_strip_custom_section_name(const basename: TSymStr; st: tsymtable; out secname: TSymStr): boolean;
  746. begin
  747. result:=inherited;
  748. if result then
  749. exit;
  750. { put all of the resource strings in a single section: it doesn't hurt,
  751. and this avoids problems with Darwin/mach-o's limitation of 255
  752. sections }
  753. secname:=basename;
  754. { Darwin requires specifying a segment name too }
  755. if target_info.system in systems_darwin then
  756. secname:='__DATA,'+secname;
  757. result:=true;
  758. end;
  759. function tllvmtai_typedconstbuilder.emit_placeholder(def: tdef): ttypedconstplaceholder;
  760. var
  761. pos: longint;
  762. begin
  763. check_add_placeholder(def);
  764. { we can't support extended constants, because those are transformed into
  765. an array of bytes, so we can't easily replace them afterwards }
  766. if (def.typ=floatdef) and
  767. (tfloatdef(def).floattype=s80real) then
  768. internalerror(2015091003);
  769. pos:=tllvmaggregateinformation(curagginfo).aggai.valuecount;
  770. emit_tai(tai_marker.Create(mark_position),def);
  771. result:=tllvmtypedconstplaceholder.create(tllvmaggregateinformation(curagginfo),pos,def);
  772. end;
  773. class function tllvmtai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  774. begin
  775. { LLVM does not support labels in the middle of a declaration }
  776. result:=get_string_header_size(typ,winlikewidestring);
  777. end;
  778. class function tllvmtai_typedconstbuilder.get_dynarray_symofs: pint;
  779. begin
  780. { LLVM does not support labels in the middle of a declaration }
  781. result:=get_dynarray_header_size;
  782. end;
  783. begin
  784. ctai_typedconstbuilder:=tllvmtai_typedconstbuilder;
  785. end.