2
0

nllvmtcon.pas 30 KB

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