llvmdef.pas 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. This unit implements some LLVM type helper routines.
  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. {$i fpcdefs.inc}
  18. unit llvmdef;
  19. interface
  20. uses
  21. cclasses,globtype,
  22. aasmbase,
  23. parabase,
  24. symbase,symtype,symdef,
  25. llvmbase;
  26. type
  27. { there are three different circumstances in which procdefs are used:
  28. a) definition of a procdef that's implemented in the current module
  29. b) declaration of an external routine that's called in the current one
  30. c) alias declaration of a procdef implemented in the current module
  31. d) defining a procvar type
  32. The main differences between the contexts are:
  33. a) information about sign extension of result type, proc name, parameter names & sign-extension info & types
  34. b) information about sign extension of result type, proc name, no parameter names, with parameter sign-extension info & types
  35. c) no information about sign extension of result type, proc name, no parameter names, no information about sign extension of parameters, parameter types
  36. d) no information about sign extension of result type, no proc name, no parameter names, no information about sign extension of parameters, parameter types
  37. }
  38. tllvmprocdefdecltype = (lpd_def,lpd_decl,lpd_alias,lpd_procvar);
  39. { returns the identifier to use as typename for a def in llvm (llvm only
  40. allows naming struct types) -- only supported for defs with a typesym, and
  41. only for tabstractrecorddef descendantds and complex procvars }
  42. function llvmtypeidentifier(def: tdef): TSymStr;
  43. { encode a type into the internal format used by LLVM (for a type
  44. declaration) }
  45. function llvmencodetypedecl(def: tdef): TSymStr;
  46. { same as above, but use a type name if possible (for any use) }
  47. function llvmencodetypename(def: tdef): TSymStr;
  48. { encode a procdef/procvardef into the internal format used by LLVM }
  49. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  50. { incremental version of the above }
  51. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  52. { function result types may have to be represented differently, e.g. a
  53. record consisting of 4 longints must be returned as a record consisting of
  54. two int64's on x86-64. This function is used to create (and reuse)
  55. temporary recorddefs for such purposes.}
  56. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  57. { get the llvm type corresponding to a parameter, e.g. a record containing
  58. two integer int64 for an arbitrary record split over two individual int64
  59. parameters, or an int32 for an int16 parameter on a platform that requires
  60. such parameters to be zero/sign extended. The second parameter can be used
  61. to get the type before zero/sign extension, as e.g. required to generate
  62. function declarations. }
  63. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  64. { can be used to extract the value extension info from acgpara. Pass in
  65. the def of the cgpara as first parameter and a local variable holding
  66. a copy of the def of the location (value extension only makes sense for
  67. ordinal parameters that are smaller than a single location). The routine
  68. will return the def of the location without sign extension (if applicable)
  69. and the kind of sign extension that was originally performed in the
  70. signext parameter }
  71. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  72. { returns whether a paraloc should be translated into an llvm "byval"
  73. parameter. These are declared as pointers to a particular type, but
  74. usually turned into copies onto the stack. The exact behaviour for
  75. parameters that should be passed in registers is undefined and depends on
  76. the platform, and furthermore this modifier sometimes inhibits
  77. optimizations. As a result,we only use it for aggregate parameters of
  78. which we know that they should be passed on the stack }
  79. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  80. { returns whether a def is representated by an aggregate type in llvm
  81. (struct, array) }
  82. function llvmaggregatetype(def: tdef): boolean;
  83. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  84. { mangle a global identifier so that it's recognised by LLVM as a global
  85. (in the sense of module-global) label and so that it won't mangle the
  86. name further according to platform conventions (we already did that) }
  87. function llvmmangledname(const s: TSymStr): TSymStr;
  88. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  89. implementation
  90. uses
  91. cutils,constexp,
  92. verbose,systems,
  93. fmodule,
  94. symtable,symconst,symsym,
  95. llvmsym,hlcgobj,
  96. defutil,blockutl,cgbase,paramgr;
  97. {******************************************************************
  98. Type encoding
  99. *******************************************************************}
  100. function llvmtypeidentifier(def: tdef): TSymStr;
  101. begin
  102. if not assigned(def.typesym) then
  103. internalerror(2015041901);
  104. result:='%"typ.'+def.fullownerhierarchyname+def.typesym.realname+'"'
  105. end;
  106. function llvmaggregatetype(def: tdef): boolean;
  107. begin
  108. result:=
  109. (def.typ in [recorddef,filedef,variantdef]) or
  110. ((def.typ=arraydef) and
  111. not is_dynamic_array(def)) or
  112. ((def.typ=setdef) and
  113. not is_smallset(def)) or
  114. is_shortstring(def) or
  115. is_object(def) or
  116. ((def.typ=procvardef) and
  117. not tprocvardef(def).is_addressonly)
  118. end;
  119. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  120. var
  121. fromregtyp,
  122. toregtyp: tregistertype;
  123. frombytesize,
  124. tobytesize: asizeint;
  125. begin
  126. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  127. toregtyp:=chlcgobj.def2regtyp(tosize);
  128. { int to pointer or vice versa }
  129. if fromregtyp=R_ADDRESSREGISTER then
  130. begin
  131. case toregtyp of
  132. R_INTREGISTER:
  133. result:=la_ptrtoint;
  134. R_ADDRESSREGISTER:
  135. result:=la_bitcast;
  136. else
  137. result:=la_ptrtoint_to_x;
  138. end;
  139. end
  140. else if toregtyp=R_ADDRESSREGISTER then
  141. begin
  142. case fromregtyp of
  143. R_INTREGISTER:
  144. result:=la_inttoptr;
  145. R_ADDRESSREGISTER:
  146. result:=la_bitcast;
  147. else
  148. result:=la_x_to_inttoptr;
  149. end;
  150. end
  151. else
  152. begin
  153. { treat comp and currency as extended in registers (see comment at start
  154. of thlgcobj.a_loadfpu_ref_reg) }
  155. if inregs and
  156. (fromsize.typ=floatdef) then
  157. begin
  158. if tfloatdef(fromsize).floattype in [s64comp,s64currency] then
  159. fromsize:=sc80floattype;
  160. { at the value level, s80real and sc80real are the same }
  161. if tfloatdef(fromsize).floattype<>s80real then
  162. frombytesize:=fromsize.size
  163. else
  164. frombytesize:=sc80floattype.size;
  165. end
  166. else
  167. frombytesize:=fromsize.size;
  168. if inregs and
  169. (tosize.typ=floatdef) then
  170. begin
  171. if tfloatdef(tosize).floattype in [s64comp,s64currency] then
  172. tosize:=sc80floattype;
  173. if tfloatdef(tosize).floattype<>s80real then
  174. tobytesize:=tosize.size
  175. else
  176. tobytesize:=sc80floattype.size;
  177. end
  178. else
  179. tobytesize:=tosize.size;
  180. { need zero/sign extension, float truncation or plain bitcast? }
  181. if tobytesize<>frombytesize then
  182. begin
  183. case fromregtyp of
  184. R_FPUREGISTER,
  185. R_MMREGISTER:
  186. begin
  187. { todo: update once we support vectors }
  188. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  189. internalerror(2014062203);
  190. if tobytesize<frombytesize then
  191. result:=la_fptrunc
  192. else
  193. result:=la_fpext
  194. end;
  195. else
  196. begin
  197. if tobytesize<frombytesize then
  198. result:=la_trunc
  199. else if is_signed(fromsize) then
  200. { fromsize is signed -> sign extension }
  201. result:=la_sext
  202. else
  203. result:=la_zext;
  204. end;
  205. end;
  206. end
  207. else if (fromsize=llvmbool1type) and
  208. (tosize<>llvmbool1type) then
  209. begin
  210. if is_cbool(tosize) then
  211. result:=la_sext
  212. else
  213. result:=la_zext
  214. end
  215. else if (tosize=llvmbool1type) and
  216. (fromsize<>llvmbool1type) then
  217. begin
  218. { would have to compare with 0, can't just take the lowest bit }
  219. if is_cbool(fromsize) then
  220. internalerror(2016052001)
  221. else
  222. result:=la_trunc
  223. end
  224. else
  225. result:=la_bitcast;
  226. end;
  227. end;
  228. function llvmmangledname(const s: TSymStr): TSymStr;
  229. begin
  230. if copy(s,1,length('llvm.'))<>'llvm.' then
  231. result:='@"\01'+s+'"'
  232. else
  233. result:='@'+s
  234. end;
  235. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  236. begin
  237. { AT_ADDR and AT_LABEL represent labels in the code, which have
  238. a different type in llvm compared to (global) data labels }
  239. if sym.bind=AB_TEMP then
  240. result:='%'+sym.name
  241. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  242. result:=llvmmangledname(sym.name)
  243. else
  244. result:='label %'+sym.name;
  245. end;
  246. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  247. begin
  248. { "byval" is broken for register paras on several platforms in llvm
  249. (search for "byval" in llvm's bug tracker). Additionally, it should only
  250. be used to pass aggregate parameters on the stack, because it reportedly
  251. inhibits llvm's midlevel optimizers.
  252. Exception (for now?): parameters that have special shifting
  253. requirements, because modelling those in llvm is not easy (and clang
  254. nor llvm-gcc seem to do so either) }
  255. result:=
  256. ((paraloc^.loc=LOC_REFERENCE) and
  257. llvmaggregatetype(paraloc^.def)) or
  258. ((paraloc^.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  259. (paraloc^.shiftval<>0))
  260. end;
  261. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  262. type
  263. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl);
  264. tllvmencodeflags = set of tllvmencodeflag;
  265. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  266. var
  267. elesize: asizeint;
  268. begin
  269. case def.typ of
  270. stringdef :
  271. begin
  272. case tstringdef(def).stringtype of
  273. st_widestring,
  274. st_unicodestring:
  275. { the variable does not point to the header, but to a
  276. null-terminated string/array with undefined bounds }
  277. encodedstr:=encodedstr+'i16*';
  278. st_ansistring:
  279. encodedstr:=encodedstr+'i8*';
  280. st_shortstring:
  281. { length byte followed by string bytes }
  282. if tstringdef(def).len>0 then
  283. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  284. else
  285. encodedstr:=encodedstr+'[0 x i8]';
  286. else
  287. internalerror(2013100201);
  288. end;
  289. end;
  290. enumdef:
  291. begin
  292. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  293. end;
  294. orddef :
  295. begin
  296. if is_void(def) then
  297. encodedstr:=encodedstr+'void'
  298. { mainly required because comparison operations return i1, and
  299. we need a way to represent the i1 type in Pascal. We don't
  300. reuse pasbool8type, because putting an i1 in a record or
  301. passing it as a parameter may result in unexpected behaviour }
  302. else if def=llvmbool1type then
  303. encodedstr:=encodedstr+'i1'
  304. else
  305. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  306. end;
  307. pointerdef :
  308. begin
  309. if is_voidpointer(def) then
  310. encodedstr:=encodedstr+'i8*'
  311. else
  312. begin
  313. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  314. encodedstr:=encodedstr+'*';
  315. end;
  316. end;
  317. floatdef :
  318. begin
  319. case tfloatdef(def).floattype of
  320. s32real:
  321. encodedstr:=encodedstr+'float';
  322. s64real:
  323. encodedstr:=encodedstr+'double';
  324. { necessary to be able to force our own size/alignment }
  325. s80real:
  326. { prevent llvm from allocating the standard ABI size for
  327. extended }
  328. if lef_inaggregate in flags then
  329. encodedstr:=encodedstr+'[10 x i8]'
  330. else
  331. encodedstr:=encodedstr+'x86_fp80';
  332. sc80real:
  333. encodedstr:=encodedstr+'x86_fp80';
  334. s64comp,
  335. s64currency:
  336. encodedstr:=encodedstr+'i64';
  337. s128real:
  338. {$if defined(powerpc) or defined(powerpc128)}
  339. encodedstr:=encodedstr+'ppc_fp128';
  340. {$else}
  341. encodedstr:=encodedstr+'fp128';
  342. {$endif}
  343. else
  344. internalerror(2013100202);
  345. end;
  346. end;
  347. filedef :
  348. begin
  349. case tfiledef(def).filetyp of
  350. ft_text :
  351. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  352. ft_typed,
  353. ft_untyped :
  354. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  355. else
  356. internalerror(2013100203);
  357. end;
  358. end;
  359. recorddef :
  360. begin
  361. { avoid endlessly recursive definitions }
  362. if assigned(def.typesym) and
  363. ((lef_inaggregate in flags) or
  364. not(lef_typedecl in flags)) then
  365. encodedstr:=encodedstr+llvmtypeidentifier(def)
  366. else
  367. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  368. end;
  369. variantdef :
  370. begin
  371. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  372. end;
  373. classrefdef :
  374. begin
  375. if is_class(tclassrefdef(def).pointeddef) then
  376. begin
  377. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags,encodedstr);
  378. encodedstr:=encodedstr+'*';
  379. end
  380. else if is_objcclass(tclassrefdef(def).pointeddef) then
  381. llvmaddencodedtype_intern(objc_idtype,flags,encodedstr)
  382. else
  383. encodedstr:=encodedstr+'i8*'
  384. end;
  385. setdef :
  386. begin
  387. { just an array as far as llvm is concerned; don't use a "packed
  388. array of i1" or so, this requires special support in backends
  389. and guarantees nothing about the internal format }
  390. if is_smallset(def) then
  391. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  392. else
  393. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  394. end;
  395. formaldef :
  396. begin
  397. { var/const/out x (always treated as "pass by reference" -> don't
  398. add extra "*" here) }
  399. encodedstr:=encodedstr+'i8';
  400. end;
  401. arraydef :
  402. begin
  403. if is_array_of_const(def) then
  404. begin
  405. encodedstr:=encodedstr+'[0 x ';
  406. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  407. encodedstr:=encodedstr+']';
  408. end
  409. else if is_open_array(def) then
  410. begin
  411. encodedstr:=encodedstr+'[0 x ';
  412. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  413. encodedstr:=encodedstr+']';
  414. end
  415. else if is_dynamic_array(def) then
  416. begin
  417. llvmaddencodedtype_intern(tarraydef(def).elementdef,[],encodedstr);
  418. encodedstr:=encodedstr+'*';
  419. end
  420. else if is_packed_array(def) and
  421. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  422. begin
  423. elesize:=packedbitsloadsize(tarraydef(def).elementdef.packedbitsize);
  424. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size div elesize)+' x ';
  425. { encode as an array of integers with the size on which we
  426. perform the packedbits operations }
  427. llvmaddencodedtype_intern(cgsize_orddef(int_cgsize(elesize)),[lef_inaggregate],encodedstr);
  428. encodedstr:=encodedstr+']';
  429. end
  430. else
  431. begin
  432. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  433. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  434. encodedstr:=encodedstr+']';
  435. end;
  436. end;
  437. procdef,
  438. procvardef :
  439. begin
  440. if (def.typ=procdef) or
  441. tprocvardef(def).is_addressonly then
  442. begin
  443. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  444. if def.typ=procvardef then
  445. encodedstr:=encodedstr+'*';
  446. end
  447. else if ((lef_inaggregate in flags) or
  448. not(lef_typedecl in flags)) and
  449. assigned(tprocvardef(def).typesym) then
  450. begin
  451. { in case the procvardef recursively references itself, e.g.
  452. via a pointer }
  453. encodedstr:=encodedstr+llvmtypeidentifier(def);
  454. { blocks are implicit pointers }
  455. if is_block(def) then
  456. encodedstr:=encodedstr+'*'
  457. end
  458. else if is_block(def) then
  459. begin
  460. llvmaddencodedtype_intern(get_block_literal_type_for_proc(tabstractprocdef(def)),flags,encodedstr);
  461. end
  462. else
  463. begin
  464. encodedstr:=encodedstr+'<{';
  465. { code pointer }
  466. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  467. { data pointer (maybe todo: generate actual layout if
  468. available) }
  469. encodedstr:=encodedstr+'*, i8*}>';
  470. end;
  471. end;
  472. objectdef :
  473. case tobjectdef(def).objecttype of
  474. odt_class,
  475. odt_objcclass,
  476. odt_object,
  477. odt_cppclass:
  478. begin
  479. if not(lef_typedecl in flags) and
  480. assigned(def.typesym) then
  481. encodedstr:=encodedstr+llvmtypeidentifier(def)
  482. else
  483. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  484. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  485. is_implicit_pointer_object_type(def) then
  486. encodedstr:=encodedstr+'*'
  487. end;
  488. odt_interfacecom,
  489. odt_interfacecorba,
  490. odt_dispinterface:
  491. begin
  492. { type is a pointer to a pointer to the vmt }
  493. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  494. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  495. encodedstr:=encodedstr+'**';
  496. end;
  497. odt_interfacecom_function,
  498. odt_interfacecom_property,
  499. odt_objcprotocol:
  500. begin
  501. { opaque for now }
  502. encodedstr:=encodedstr+'i8*'
  503. end;
  504. else
  505. internalerror(2013100601);
  506. end;
  507. undefineddef,
  508. errordef :
  509. internalerror(2013100604);
  510. else
  511. internalerror(2013100603);
  512. end;
  513. end;
  514. function llvmencodetypename(def: tdef): TSymStr;
  515. begin
  516. result:='';
  517. llvmaddencodedtype_intern(def,[],result);
  518. end;
  519. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  520. var
  521. flags: tllvmencodeflags;
  522. begin
  523. if inaggregate then
  524. flags:=[lef_inaggregate]
  525. else
  526. flags:=[];
  527. llvmaddencodedtype_intern(def,flags,encodedstr);
  528. end;
  529. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  530. var
  531. st: tllvmshadowsymtable;
  532. symdeflist: tfpobjectlist;
  533. i: longint;
  534. nopacked: boolean;
  535. begin
  536. st:=tabstractrecordsymtable(def.symtable).llvmst;
  537. symdeflist:=st.symdeflist;
  538. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  539. if nopacked then
  540. encodedstr:=encodedstr+'{ '
  541. else
  542. encodedstr:=encodedstr+'<{ ';
  543. if symdeflist.count>0 then
  544. begin
  545. i:=0;
  546. if (def.typ=objectdef) and
  547. assigned(tobjectdef(def).childof) and
  548. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  549. begin
  550. { insert the struct for the class rather than a pointer to the struct }
  551. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  552. internalerror(2008070601);
  553. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  554. inc(i);
  555. end;
  556. while i<symdeflist.count do
  557. begin
  558. if i<>0 then
  559. encodedstr:=encodedstr+', ';
  560. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  561. inc(i);
  562. end;
  563. end;
  564. if nopacked then
  565. encodedstr:=encodedstr+' }'
  566. else
  567. encodedstr:=encodedstr+' }>';
  568. end;
  569. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  570. begin
  571. { implicit zero/sign extension for ABI compliance? (yes, if the size
  572. of a paraloc is larger than the size of the entire parameter) }
  573. if is_ordinal(paradef) and
  574. is_ordinal(paralocdef) and
  575. (paradef.size<paralocdef.size) then
  576. begin
  577. paralocdef:=paradef;
  578. if is_signed(paradef) then
  579. signext:=lve_signext
  580. else
  581. signext:=lve_zeroext
  582. end
  583. else
  584. signext:=lve_none;
  585. end;
  586. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  587. var
  588. paraloc: PCGParaLocation;
  589. signext: tllvmvalueextension;
  590. usedef: tdef;
  591. begin
  592. if (proccalloption in cdecl_pocalls) and
  593. is_array_of_const(hp.vardef) then
  594. begin
  595. if not first then
  596. encodedstr:=encodedstr+', '
  597. else
  598. first:=false;
  599. encodedstr:=encodedstr+'...';
  600. exit
  601. end;
  602. if withparaname then
  603. paraloc:=hp.paraloc[calleeside].location
  604. else
  605. paraloc:=hp.paraloc[callerside].location;
  606. repeat
  607. usedef:=paraloc^.def;
  608. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  609. { implicit zero/sign extension for ABI compliance? }
  610. if not first then
  611. encodedstr:=encodedstr+', '
  612. else
  613. first:=false;
  614. llvmaddencodedtype_intern(usedef,[],encodedstr);
  615. { in case signextstr<>'', there should be only one paraloc -> no need
  616. to clear (reason: it means that the paraloc is larger than the
  617. original parameter) }
  618. if withattributes then
  619. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  620. { sret: hidden pointer for structured function result }
  621. if vo_is_funcret in hp.varoptions then
  622. begin
  623. { "sret" is only valid for the firstparameter, while in FPC this
  624. can sometimes be second one (self comes before). In general,
  625. this is not a problem: we can just leave out sret, which means
  626. the result will be a bit less well optimised), but it is for
  627. AArch64: there, the sret parameter must be passed in a different
  628. register (-> paranr_result is smaller than paranr_self for that
  629. platform in symconst) }
  630. {$ifdef aarch64}
  631. if not first then
  632. internalerror(2015101404);
  633. {$endif aarch64}
  634. if withattributes then
  635. if first then
  636. encodedstr:=encodedstr+' sret'
  637. else { we can add some other attributes to optimise things,}
  638. encodedstr:=encodedstr+' noalias nocapture';
  639. end
  640. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  641. llvmbyvalparaloc(paraloc) then
  642. begin
  643. if withattributes then
  644. encodedstr:=encodedstr+'* byval'
  645. else
  646. encodedstr:=encodedstr+'*';
  647. end;
  648. if withparaname then
  649. begin
  650. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  651. internalerror(2014010803);
  652. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  653. end;
  654. paraloc:=paraloc^.next;
  655. until not assigned(paraloc);
  656. end;
  657. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  658. begin
  659. result:='';
  660. llvmaddencodedproctype(def,customname,pddecltype,result);
  661. end;
  662. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  663. var
  664. usedef: tdef;
  665. paranr: longint;
  666. hp: tparavarsym;
  667. signext: tllvmvalueextension;
  668. useside: tcallercallee;
  669. first: boolean;
  670. begin
  671. { when writing a definition, we have to write the parameter names, and
  672. those are only available on the callee side. In all other cases,
  673. we are at the callerside }
  674. if pddecltype=lpd_def then
  675. useside:=calleeside
  676. else
  677. useside:=callerside;
  678. def.init_paraloc_info(useside);
  679. first:=true;
  680. { function result (return-by-ref is handled explicitly) }
  681. if not paramanager.ret_in_param(def.returndef,def) then
  682. begin
  683. usedef:=llvmgetcgparadef(def.funcretloc[useside],false);
  684. llvmextractvalueextinfo(def.returndef,usedef,signext);
  685. { specifying result sign extention information for an alias causes
  686. an error for some reason }
  687. if pddecltype in [lpd_decl,lpd_def] then
  688. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  689. encodedstr:=encodedstr+' ';
  690. llvmaddencodedtype_intern(usedef,[],encodedstr);
  691. end
  692. else
  693. begin
  694. encodedstr:=encodedstr+' ';
  695. llvmaddencodedtype(voidtype,false,encodedstr);
  696. end;
  697. encodedstr:=encodedstr+' ';
  698. { add procname? }
  699. if (pddecltype in [lpd_decl,lpd_def]) and
  700. (def.typ=procdef) then
  701. if customname='' then
  702. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  703. else
  704. encodedstr:=encodedstr+llvmmangledname(customname);
  705. encodedstr:=encodedstr+'(';
  706. { parameters }
  707. first:=true;
  708. for paranr:=0 to def.paras.count-1 do
  709. begin
  710. hp:=tparavarsym(def.paras[paranr]);
  711. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  712. end;
  713. if po_varargs in def.procoptions then
  714. begin
  715. if not first then
  716. encodedstr:=encodedstr+', ';
  717. encodedstr:=encodedstr+'...';
  718. end;
  719. encodedstr:=encodedstr+')'
  720. end;
  721. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  722. var
  723. i: longint;
  724. res: PHashSetItem;
  725. oldsymtablestack: tsymtablestack;
  726. hrecst: trecordsymtable;
  727. hdef: tdef;
  728. hrecdef: trecorddef;
  729. sym: tfieldvarsym;
  730. typename: string;
  731. begin
  732. typename:=internaltypeprefixName[itp_llvmstruct];
  733. for i:=0 to fieldtypes.count-1 do
  734. begin
  735. hdef:=tdef(fieldtypes[i]);
  736. case hdef.typ of
  737. orddef:
  738. case torddef(hdef).ordtype of
  739. s8bit,
  740. u8bit:
  741. typename:=typename+'i8';
  742. s16bit,
  743. u16bit:
  744. typename:=typename+'i16';
  745. s32bit,
  746. u32bit:
  747. typename:=typename+'i32';
  748. s64bit,
  749. u64bit:
  750. typename:=typename+'i64';
  751. else
  752. { other types should not appear currently, add as needed }
  753. internalerror(2014012001);
  754. end;
  755. floatdef:
  756. case tfloatdef(hdef).floattype of
  757. s32real:
  758. typename:=typename+'f32';
  759. s64real:
  760. typename:=typename+'f64';
  761. else
  762. { other types should not appear currently, add as needed }
  763. internalerror(2014012008);
  764. end;
  765. pointerdef:
  766. typename:='p'+tostr(hdef.deflist_index);
  767. else
  768. { other types should not appear currently, add as needed }
  769. internalerror(2014012009);
  770. end;
  771. end;
  772. if not assigned(current_module) then
  773. internalerror(2014012002);
  774. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  775. if not assigned(res^.Data) then
  776. begin
  777. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  778. recordalignmin,maxcrecordalign);
  779. for i:=0 to fieldtypes.count-1 do
  780. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),tdef(fieldtypes[i]));
  781. end;
  782. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  783. result:=trecorddef(res^.Data);
  784. end;
  785. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  786. var
  787. retdeflist: tfplist;
  788. retloc: pcgparalocation;
  789. usedef: tdef;
  790. valueext: tllvmvalueextension;
  791. begin
  792. { single location }
  793. if not assigned(cgpara.location^.next) then
  794. begin
  795. { def of the location, except in case of zero/sign-extension }
  796. usedef:=cgpara.location^.def;
  797. if beforevalueext then
  798. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  799. { comp and currency are handled by the x87 in this case. They cannot
  800. be represented directly in llvm, and llvmdef translates them into
  801. i64 (since that's their storage size and internally they also are
  802. int64). Solve this by changing the type to s80real in the
  803. returndef/parameter declaration. }
  804. if (usedef.typ=floatdef) and
  805. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  806. usedef:=s80floattype;
  807. result:=usedef;
  808. exit
  809. end;
  810. { multiple locations -> create temp record }
  811. retdeflist:=tfplist.create;
  812. retloc:=cgpara.location;
  813. repeat
  814. retdeflist.add(retloc^.def);
  815. retloc:=retloc^.next;
  816. until not assigned(retloc);
  817. result:=llvmgettemprecorddef(retdeflist,C_alignment,
  818. targetinfos[target_info.system]^.alignment.recordalignmin,
  819. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  820. include(result.defoptions,df_llvm_no_struct_packing);
  821. end;
  822. function llvmencodetypedecl(def: tdef): TSymStr;
  823. begin
  824. result:='';
  825. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  826. end;
  827. end.