nllvmtcon.pas 33 KB

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