nllvmtcon.pas 28 KB

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