llvmdef.pas 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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. symconst,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 tabstractrecorddef
  41. 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(const fieldtypes: array of tdef; packrecords, recordalignmin: 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; callercallee: tcallercallee): 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. { convert a parameter attribute to a string. Depending on the target
  89. LLVM version, we may have to add the dereferenced parameter type as well }
  90. function llvmparatypeattr(const attr: TSymStr; paradef: tdef; strippointer: boolean): TSymStr;
  91. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  92. function llvmfloatintrinsicsuffix(def: tfloatdef): TIDString;
  93. implementation
  94. uses
  95. globals,cutils,constexp,
  96. verbose,systems,
  97. fmodule,
  98. symtable,symsym,
  99. llvmsym,hlcgobj,
  100. defutil,blockutl,cgbase,paramgr,
  101. llvminfo,cpubase;
  102. {******************************************************************
  103. Type encoding
  104. *******************************************************************}
  105. function llvmtypeidentifier(def: tdef): TSymStr;
  106. begin
  107. if assigned(def.typesym) then
  108. result:='%"typ.'+def.fullownerhierarchyname(false)+def.typesym.realname+'"'
  109. else
  110. result:='%"typ.'+def.fullownerhierarchyname(false)+def.unique_id_str+'"';
  111. end;
  112. function llvmaggregatetype(def: tdef): boolean;
  113. begin
  114. result:=
  115. (def.typ in [recorddef,filedef,variantdef]) or
  116. ((def.typ=arraydef) and
  117. not is_dynamic_array(def)) or
  118. ((def.typ=setdef) and
  119. not is_smallset(def)) or
  120. is_shortstring(def) or
  121. is_object(def) or
  122. ((def.typ=procvardef) and
  123. not tprocvardef(def).is_addressonly)
  124. end;
  125. function llvmconvop(var fromsize, tosize: tdef; inregs: boolean): tllvmop;
  126. var
  127. fromregtyp,
  128. toregtyp: tregistertype;
  129. frombytesize,
  130. tobytesize: asizeint;
  131. begin
  132. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  133. toregtyp:=chlcgobj.def2regtyp(tosize);
  134. { int to pointer or vice versa }
  135. if fromregtyp=R_ADDRESSREGISTER then
  136. begin
  137. case toregtyp of
  138. R_INTREGISTER:
  139. result:=la_ptrtoint;
  140. R_ADDRESSREGISTER:
  141. result:=la_bitcast;
  142. else
  143. result:=la_ptrtoint_to_x;
  144. end;
  145. end
  146. else if toregtyp=R_ADDRESSREGISTER then
  147. begin
  148. case fromregtyp of
  149. R_INTREGISTER:
  150. result:=la_inttoptr;
  151. R_ADDRESSREGISTER:
  152. result:=la_bitcast;
  153. else
  154. result:=la_x_to_inttoptr;
  155. end;
  156. end
  157. else
  158. begin
  159. { treat comp and currency as extended in registers (see comment at start
  160. of thlgcobj.a_loadfpu_ref_reg) }
  161. if inregs and
  162. (fromsize.typ=floatdef) then
  163. begin
  164. if tfloatdef(fromsize).floattype in [s64comp,s64currency] then
  165. fromsize:=sc80floattype;
  166. { at the value level, s80real and sc80real are the same }
  167. if tfloatdef(fromsize).floattype<>s80real then
  168. frombytesize:=fromsize.size
  169. else
  170. frombytesize:=sc80floattype.size;
  171. end
  172. else
  173. frombytesize:=fromsize.size;
  174. if inregs and
  175. (tosize.typ=floatdef) then
  176. begin
  177. if tfloatdef(tosize).floattype in [s64comp,s64currency] then
  178. tosize:=sc80floattype;
  179. if tfloatdef(tosize).floattype<>s80real then
  180. tobytesize:=tosize.size
  181. else
  182. tobytesize:=sc80floattype.size;
  183. end
  184. else
  185. tobytesize:=tosize.size;
  186. { need zero/sign extension, float truncation or plain bitcast? }
  187. if tobytesize<>frombytesize then
  188. begin
  189. case fromregtyp of
  190. R_FPUREGISTER,
  191. R_MMREGISTER:
  192. begin
  193. { todo: update once we support vectors }
  194. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  195. internalerror(2014062202);
  196. if tobytesize<frombytesize then
  197. result:=la_fptrunc
  198. else
  199. result:=la_fpext
  200. end;
  201. else
  202. begin
  203. if tobytesize<frombytesize then
  204. result:=la_trunc
  205. else if is_signed(fromsize) then
  206. { fromsize is signed -> sign extension }
  207. result:=la_sext
  208. else
  209. result:=la_zext;
  210. end;
  211. end;
  212. end
  213. else if (fromsize=llvmbool1type) and
  214. (tosize<>llvmbool1type) then
  215. begin
  216. if is_cbool(tosize) then
  217. result:=la_sext
  218. else
  219. result:=la_zext
  220. end
  221. else if (tosize=llvmbool1type) and
  222. (fromsize<>llvmbool1type) then
  223. begin
  224. { would have to compare with 0, can't just take the lowest bit }
  225. if is_cbool(fromsize) then
  226. internalerror(2016052001)
  227. else
  228. result:=la_trunc
  229. end
  230. else
  231. result:=la_bitcast;
  232. end;
  233. end;
  234. function llvmmangledname(const s: TSymStr): TSymStr;
  235. begin
  236. if copy(s,1,length('llvm.'))<>'llvm.' then
  237. if s[1]<>'"' then
  238. result:='@"\01'+s+'"'
  239. else
  240. begin
  241. { already quoted -> insert \01 and prepend @ }
  242. result:='@'+s;
  243. insert('\01',result,3);
  244. end
  245. else
  246. result:='@'+s
  247. end;
  248. function llvmparatypeattr(const attr: TSymStr; paradef: tdef; strippointer: boolean): TSymStr;
  249. begin
  250. result:=attr;
  251. if llvmflag_para_attr_type in llvmversion_properties[current_settings.llvmversion] then
  252. begin
  253. if not strippointer then
  254. result:=result+'('+llvmencodetypename(paradef)+')'
  255. else
  256. begin
  257. if paradef.typ<>pointerdef then
  258. internalerror(2022060310);
  259. if not is_void(tpointerdef(paradef).pointeddef) then
  260. result:=result+'('+llvmencodetypename(tpointerdef(paradef).pointeddef)+')'
  261. else
  262. result:=result+'(i8)'
  263. end;
  264. end;
  265. end;
  266. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  267. begin
  268. { AT_ADDR and AT_LABEL represent labels in the code, which have
  269. a different type in llvm compared to (global) data labels }
  270. if sym.bind=AB_TEMP then
  271. result:='%'+sym.name
  272. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  273. result:=llvmmangledname(sym.name)
  274. else
  275. result:='label %'+sym.name;
  276. end;
  277. function llvmfloatintrinsicsuffix(def: tfloatdef): TIDString;
  278. begin
  279. case def.floattype of
  280. s32real:
  281. result:='_f32';
  282. s64real:
  283. result:='_f64';
  284. s80real,sc80real:
  285. result:='_f80';
  286. s128real:
  287. result:='_f128';
  288. else
  289. { comp/currency need to be converted to s(c)80real first }
  290. internalerror(2019122902);
  291. end;
  292. end;
  293. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  294. begin
  295. { "byval" is broken for register paras on several platforms in llvm
  296. (search for "byval" in llvm's bug tracker). Additionally, it should only
  297. be used to pass aggregate parameters on the stack, because it reportedly
  298. inhibits llvm's midlevel optimizers.
  299. Exception (for now?): parameters that have special shifting
  300. requirements, because modelling those in llvm is not easy (and clang
  301. nor llvm-gcc seem to do so either) }
  302. result:=
  303. ((paraloc^.loc=LOC_REFERENCE) and
  304. llvmaggregatetype(paraloc^.def)) or
  305. ((paraloc^.loc in [LOC_REGISTER,LOC_CREGISTER]) and
  306. (paraloc^.shiftval<>0))
  307. end;
  308. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  309. type
  310. tllvmencodeflag = (lef_inaggregate, lef_noimplicitderef, lef_typedecl);
  311. tllvmencodeflags = set of tllvmencodeflag;
  312. procedure llvmaddencodedtype_intern(def: tdef; const flags: tllvmencodeflags; var encodedstr: TSymStr);
  313. begin
  314. case def.typ of
  315. stringdef :
  316. begin
  317. case tstringdef(def).stringtype of
  318. st_widestring,
  319. st_unicodestring:
  320. { the variable does not point to the header, but to a
  321. null-terminated string/array with undefined bounds }
  322. encodedstr:=encodedstr+'i16*';
  323. st_ansistring:
  324. encodedstr:=encodedstr+'i8*';
  325. st_shortstring:
  326. { length byte followed by string bytes }
  327. if tstringdef(def).len>0 then
  328. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  329. else
  330. encodedstr:=encodedstr+'[0 x i8]';
  331. else
  332. internalerror(2013100201);
  333. end;
  334. end;
  335. enumdef:
  336. begin
  337. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  338. end;
  339. orddef :
  340. begin
  341. if is_void(def) then
  342. encodedstr:=encodedstr+'void'
  343. { mainly required because comparison operations return i1, and
  344. we need a way to represent the i1 type in Pascal. We don't
  345. reuse pasbool1type, because putting an i1 in a record or
  346. passing it as a parameter may result in unexpected behaviour }
  347. else if def=llvmbool1type then
  348. encodedstr:=encodedstr+'i1'
  349. else if torddef(def).ordtype<>customint then
  350. encodedstr:=encodedstr+'i'+tostr(def.size*8)
  351. else
  352. encodedstr:=encodedstr+'i'+tostr(def.packedbitsize);
  353. end;
  354. pointerdef :
  355. begin
  356. if is_voidpointer(def) then
  357. encodedstr:=encodedstr+'i8*'
  358. else
  359. begin
  360. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  361. encodedstr:=encodedstr+'*';
  362. end;
  363. end;
  364. floatdef :
  365. begin
  366. case tfloatdef(def).floattype of
  367. s32real:
  368. encodedstr:=encodedstr+'float';
  369. s64real:
  370. encodedstr:=encodedstr+'double';
  371. { necessary to be able to force our own size/alignment }
  372. s80real:
  373. { prevent llvm from allocating the standard ABI size for
  374. extended }
  375. if lef_inaggregate in flags then
  376. encodedstr:=encodedstr+'[10 x i8]'
  377. else
  378. encodedstr:=encodedstr+'x86_fp80';
  379. sc80real:
  380. encodedstr:=encodedstr+'x86_fp80';
  381. s64comp,
  382. s64currency:
  383. encodedstr:=encodedstr+'i64';
  384. s128real:
  385. {$if defined(powerpc) or defined(powerpc128)}
  386. encodedstr:=encodedstr+'ppc_fp128';
  387. {$else}
  388. encodedstr:=encodedstr+'fp128';
  389. {$endif}
  390. end;
  391. end;
  392. filedef :
  393. begin
  394. case tfiledef(def).filetyp of
  395. ft_text :
  396. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  397. ft_typed :
  398. begin
  399. { in case of ISO-like I/O, the typed file def includes a
  400. get/put buffer of the size of the file's elements }
  401. if (m_isolike_io in current_settings.modeswitches) and
  402. not is_void(tfiledef(def).typedfiledef) then
  403. encodedstr:=encodedstr+'<{';
  404. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  405. if (m_isolike_io in current_settings.modeswitches) and
  406. not is_void(tfiledef(def).typedfiledef) then
  407. begin
  408. encodedstr:=encodedstr+',[';
  409. encodedstr:=encodedstr+tostr(tfiledef(def).typedfiledef.size);
  410. encodedstr:=encodedstr+' x i8]}>'
  411. end;
  412. end;
  413. ft_untyped :
  414. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  415. end;
  416. end;
  417. recorddef :
  418. begin
  419. { avoid endlessly recursive definitions }
  420. if not(lef_typedecl in flags) then
  421. encodedstr:=encodedstr+llvmtypeidentifier(def)
  422. else
  423. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  424. end;
  425. variantdef :
  426. begin
  427. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  428. end;
  429. classrefdef :
  430. begin
  431. if is_class(tclassrefdef(def).pointeddef) then
  432. begin
  433. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags,encodedstr);
  434. encodedstr:=encodedstr+'*';
  435. end
  436. else if is_objcclass(tclassrefdef(def).pointeddef) then
  437. llvmaddencodedtype_intern(objc_idtype,flags,encodedstr)
  438. else
  439. encodedstr:=encodedstr+'i8*'
  440. end;
  441. setdef :
  442. begin
  443. { just an array as far as llvm is concerned; don't use a "packed
  444. array of i1" or so, this requires special support in backends
  445. and guarantees nothing about the internal format }
  446. if is_smallset(def) then
  447. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  448. else
  449. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  450. end;
  451. formaldef :
  452. begin
  453. { var/const/out x (always treated as "pass by reference" -> don't
  454. add extra "*" here) }
  455. encodedstr:=encodedstr+'i8';
  456. end;
  457. arraydef :
  458. begin
  459. if tarraydef(def).is_hwvector then
  460. begin
  461. encodedstr:=encodedstr+'<'+tostr(tarraydef(def).elecount)+' x ';
  462. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  463. encodedstr:=encodedstr+'>';
  464. end
  465. else if is_array_of_const(def) then
  466. begin
  467. encodedstr:=encodedstr+'[0 x ';
  468. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  469. encodedstr:=encodedstr+']';
  470. end
  471. else if is_open_array(def) then
  472. begin
  473. encodedstr:=encodedstr+'[0 x ';
  474. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  475. encodedstr:=encodedstr+']';
  476. end
  477. else if is_dynamic_array(def) then
  478. begin
  479. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  480. encodedstr:=encodedstr+'*';
  481. end
  482. else if is_packed_array(def) and
  483. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  484. begin
  485. { encode as an array of bytes rather than as an array of
  486. packedbitsloadsize(elesize), because even if the load size
  487. is e.g. 2 bytes, the array may only be 1 or 3 bytes long
  488. (and if this array is inside a record, it must not be
  489. encoded as a type that is too long) }
  490. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size)+' x ';
  491. llvmaddencodedtype_intern(u8inttype,[lef_inaggregate],encodedstr);
  492. encodedstr:=encodedstr+']';
  493. end
  494. else
  495. begin
  496. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  497. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  498. encodedstr:=encodedstr+']';
  499. end;
  500. end;
  501. procdef,
  502. procvardef :
  503. begin
  504. if (def.typ=procdef) or
  505. tprocvardef(def).is_addressonly then
  506. begin
  507. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  508. if def.typ=procvardef then
  509. encodedstr:=encodedstr+'*';
  510. end
  511. else if not(lef_typedecl in flags) then
  512. begin
  513. { in case the procvardef recursively references itself, e.g.
  514. via a pointer }
  515. encodedstr:=encodedstr+llvmtypeidentifier(def);
  516. { blocks are implicit pointers }
  517. if is_block(def) then
  518. encodedstr:=encodedstr+'*'
  519. end
  520. else if is_block(def) then
  521. begin
  522. llvmaddencodedtype_intern(get_block_literal_type_for_proc(tabstractprocdef(def)),flags,encodedstr);
  523. end
  524. else
  525. begin
  526. encodedstr:=encodedstr+'<{';
  527. { code pointer }
  528. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  529. { data pointer (maybe todo: generate actual layout if
  530. available) }
  531. encodedstr:=encodedstr+'*, i8*}>';
  532. end;
  533. end;
  534. objectdef :
  535. case tobjectdef(def).objecttype of
  536. odt_class,
  537. odt_objcclass,
  538. odt_object,
  539. odt_cppclass:
  540. begin
  541. if not(lef_typedecl in flags) then
  542. encodedstr:=encodedstr+llvmtypeidentifier(def)
  543. else
  544. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  545. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  546. is_implicit_pointer_object_type(def) then
  547. encodedstr:=encodedstr+'*'
  548. end;
  549. odt_interfacecom,
  550. odt_interfacecorba,
  551. odt_dispinterface:
  552. begin
  553. { type is a pointer to a pointer to the vmt }
  554. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  555. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  556. encodedstr:=encodedstr+'**';
  557. end;
  558. odt_interfacecom_function,
  559. odt_interfacecom_property,
  560. odt_objcprotocol:
  561. begin
  562. { opaque for now }
  563. encodedstr:=encodedstr+'i8*'
  564. end;
  565. odt_helper:
  566. llvmaddencodedtype_intern(tobjectdef(def).extendeddef,flags,encodedstr);
  567. else
  568. internalerror(2013100601);
  569. end;
  570. undefineddef:
  571. begin
  572. if def=llvm_metadatatype then
  573. encodedstr:=encodedstr+'metadata'
  574. else
  575. internalerror(2022052301);
  576. end;
  577. errordef :
  578. internalerror(2013100604);
  579. else
  580. internalerror(2013100603);
  581. end;
  582. end;
  583. function llvmencodetypename(def: tdef): TSymStr;
  584. begin
  585. result:='';
  586. llvmaddencodedtype_intern(def,[],result);
  587. end;
  588. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  589. var
  590. flags: tllvmencodeflags;
  591. begin
  592. if inaggregate then
  593. flags:=[lef_inaggregate]
  594. else
  595. flags:=[];
  596. llvmaddencodedtype_intern(def,flags,encodedstr);
  597. end;
  598. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  599. var
  600. st: tllvmshadowsymtable;
  601. symdeflist: tfpobjectlist;
  602. i: longint;
  603. nopacked: boolean;
  604. begin
  605. st:=tabstractrecordsymtable(def.symtable).llvmst;
  606. symdeflist:=st.symdeflist;
  607. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  608. if nopacked then
  609. encodedstr:=encodedstr+'{ '
  610. else
  611. encodedstr:=encodedstr+'<{ ';
  612. if symdeflist.count>0 then
  613. begin
  614. i:=0;
  615. if (def.typ=objectdef) and
  616. assigned(tobjectdef(def).childof) and
  617. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  618. begin
  619. { insert the struct for the class rather than a pointer to the struct }
  620. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  621. internalerror(2008070601);
  622. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  623. inc(i);
  624. end;
  625. while i<symdeflist.count do
  626. begin
  627. if i<>0 then
  628. encodedstr:=encodedstr+', ';
  629. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  630. inc(i);
  631. end;
  632. end;
  633. if nopacked then
  634. encodedstr:=encodedstr+' }'
  635. else
  636. encodedstr:=encodedstr+' }>';
  637. end;
  638. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  639. begin
  640. { implicit zero/sign extension for ABI compliance? (yes, if the size
  641. of a paraloc is larger than the size of the entire parameter) }
  642. if is_ordinal(paradef) and
  643. is_ordinal(paralocdef) and
  644. (paradef.size<paralocdef.size) then
  645. begin
  646. paralocdef:=paradef;
  647. if is_signed(paradef) then
  648. signext:=lve_signext
  649. else
  650. signext:=lve_zeroext
  651. end
  652. else
  653. signext:=lve_none;
  654. end;
  655. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  656. var
  657. para: PCGPara;
  658. paraloc: PCGParaLocation;
  659. side: tcallercallee;
  660. signext: tllvmvalueextension;
  661. usedef: tdef;
  662. firstloc: boolean;
  663. begin
  664. if (proccalloption in cdecl_pocalls) and
  665. is_array_of_const(hp.vardef) then
  666. begin
  667. if not first then
  668. encodedstr:=encodedstr+', '
  669. else
  670. first:=false;
  671. encodedstr:=encodedstr+'...';
  672. exit
  673. end;
  674. if not withparaname then
  675. side:=callerside
  676. else
  677. side:=calleeside;
  678. { don't add parameters that don't take up registers or stack space;
  679. clang doesn't either and some LLVM backends don't support them }
  680. if hp.paraloc[side].isempty then
  681. exit;
  682. para:[email protected][side];
  683. paraloc:=para^.location;
  684. firstloc:=true;
  685. repeat
  686. usedef:=paraloc^.def;
  687. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  688. { implicit zero/sign extension for ABI compliance? }
  689. if not first then
  690. encodedstr:=encodedstr+', ';
  691. llvmaddencodedtype_intern(usedef,[],encodedstr);
  692. { in case signextstr<>'', there should be only one paraloc -> no need
  693. to clear (reason: it means that the paraloc is larger than the
  694. original parameter) }
  695. if withattributes then
  696. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  697. { sret: hidden pointer for structured function result }
  698. if vo_is_funcret in hp.varoptions then
  699. begin
  700. { "sret" is only valid for the firstparameter, while in FPC this
  701. can sometimes be second one (self comes before). In general,
  702. this is not a problem: we can just leave out sret, which means
  703. the result will be a bit less well optimised), but it is for
  704. AArch64: there, the sret parameter must be passed in a different
  705. register (-> paranr_result is smaller than paranr_self for that
  706. platform in symconst) }
  707. {$ifdef aarch64}
  708. if not first and
  709. not is_managed_type(hp.vardef) then
  710. internalerror(2015101404);
  711. {$endif aarch64}
  712. if withattributes then
  713. begin
  714. if first
  715. {$ifdef aarch64}
  716. and not is_managed_type(hp.vardef)
  717. {$endif aarch64}
  718. then
  719. encodedstr:=encodedstr+llvmparatypeattr(' sret',hp.vardef,false)+' noalias nocapture'
  720. else
  721. encodedstr:=encodedstr+' noalias nocapture';
  722. end;
  723. end
  724. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  725. llvmbyvalparaloc(paraloc) then
  726. begin
  727. encodedstr:=encodedstr+'*';
  728. if withattributes then
  729. begin
  730. encodedstr:=encodedstr+llvmparatypeattr(' byval',hp.vardef,false);
  731. if firstloc and
  732. (para^.alignment<>std_param_align) then
  733. begin
  734. encodedstr:=encodedstr+' align '+tostr(para^.alignment);
  735. end;
  736. end
  737. end
  738. else if withattributes and
  739. paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) then
  740. begin
  741. { it's not valid to take the address of a parameter and store it for
  742. use past the end of the function call (since the address can always
  743. be on the stack and become invalid later) }
  744. encodedstr:=encodedstr+' nocapture';
  745. { open array/array of const/variant array may be a valid pointer but empty }
  746. if not is_special_array(hp.vardef) and
  747. { e.g. empty records }
  748. (hp.vardef.size<>0) then
  749. begin
  750. case hp.varspez of
  751. vs_value,
  752. vs_const:
  753. begin
  754. encodedstr:=encodedstr+' readonly dereferenceable('
  755. end;
  756. vs_var,
  757. vs_out:
  758. begin
  759. { while normally these are not nil, it is technically possible
  760. to pass nil via ptrtype(nil)^ }
  761. encodedstr:=encodedstr+' dereferenceable_or_null(';
  762. end;
  763. vs_constref:
  764. begin
  765. encodedstr:=encodedstr+' readonly dereferenceable_or_null(';
  766. end;
  767. else
  768. internalerror(2018120801);
  769. end;
  770. if hp.vardef.typ<>formaldef then
  771. encodedstr:=encodedstr+tostr(hp.vardef.size)+')'
  772. else
  773. encodedstr:=encodedstr+'1)';
  774. end;
  775. end;
  776. if withparaname then
  777. begin
  778. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  779. internalerror(2014010803);
  780. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  781. end;
  782. paraloc:=paraloc^.next;
  783. firstloc:=false;
  784. first:=false;
  785. until not assigned(paraloc);
  786. end;
  787. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  788. begin
  789. result:='';
  790. llvmaddencodedproctype(def,customname,pddecltype,result);
  791. end;
  792. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  793. var
  794. callingconv: ansistring;
  795. usedef: tdef;
  796. paranr: longint;
  797. hp: tparavarsym;
  798. signext: tllvmvalueextension;
  799. useside: tcallercallee;
  800. first: boolean;
  801. begin
  802. if not(pddecltype in [lpd_alias,lpd_procvar]) then
  803. begin
  804. callingconv:=llvm_callingconvention_name(def.proccalloption);
  805. if callingconv<>'' then
  806. encodedstr:=encodedstr+' '+callingconv;
  807. end;
  808. { when writing a definition, we have to write the parameter names, and
  809. those are only available on the callee side. In all other cases,
  810. we are at the callerside }
  811. if pddecltype=lpd_def then
  812. useside:=calleeside
  813. else
  814. useside:=callerside;
  815. def.init_paraloc_info(useside);
  816. first:=true;
  817. { function result (return-by-ref is handled explicitly) }
  818. if not paramanager.ret_in_param(def.returndef,def) or
  819. def.generate_safecall_wrapper then
  820. begin
  821. if not def.generate_safecall_wrapper then
  822. usedef:=llvmgetcgparadef(def.funcretloc[useside],false,useside)
  823. else
  824. usedef:=ossinttype;
  825. llvmextractvalueextinfo(def.returndef,usedef,signext);
  826. { specifying result sign extention information for an alias causes
  827. an error for some reason }
  828. if pddecltype in [lpd_decl,lpd_def] then
  829. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  830. encodedstr:=encodedstr+' ';
  831. llvmaddencodedtype_intern(usedef,[],encodedstr);
  832. end
  833. else
  834. begin
  835. encodedstr:=encodedstr+' ';
  836. llvmaddencodedtype(voidtype,false,encodedstr);
  837. end;
  838. encodedstr:=encodedstr+' ';
  839. { add procname? }
  840. if (pddecltype in [lpd_decl,lpd_def]) and
  841. (def.typ=procdef) then
  842. if customname='' then
  843. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  844. else
  845. encodedstr:=encodedstr+llvmmangledname(customname);
  846. encodedstr:=encodedstr+'(';
  847. { parameters }
  848. first:=true;
  849. for paranr:=0 to def.paras.count-1 do
  850. begin
  851. hp:=tparavarsym(def.paras[paranr]);
  852. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  853. end;
  854. if po_varargs in def.procoptions then
  855. begin
  856. if not first then
  857. encodedstr:=encodedstr+', ';
  858. encodedstr:=encodedstr+'...';
  859. end;
  860. encodedstr:=encodedstr+')'
  861. end;
  862. function llvmgettemprecorddef(const fieldtypes: array of tdef; packrecords, recordalignmin: shortint): trecorddef;
  863. procedure addtypename(var typename: TSymStr; hdef: tdef);
  864. begin
  865. case hdef.typ of
  866. orddef:
  867. case torddef(hdef).ordtype of
  868. s8bit,
  869. u8bit,
  870. pasbool1,
  871. pasbool8:
  872. typename:=typename+'i8';
  873. s16bit,
  874. u16bit:
  875. typename:=typename+'i16';
  876. s32bit,
  877. u32bit:
  878. typename:=typename+'i32';
  879. s64bit,
  880. u64bit:
  881. typename:=typename+'i64';
  882. customint:
  883. typename:=typename+'i'+tostr(torddef(hdef).packedbitsize);
  884. else
  885. { other types should not appear currently, add as needed }
  886. internalerror(2014012001);
  887. end;
  888. floatdef:
  889. case tfloatdef(hdef).floattype of
  890. s32real:
  891. typename:=typename+'f32';
  892. s64real:
  893. typename:=typename+'f64';
  894. else
  895. { other types should not appear currently, add as needed }
  896. internalerror(2014012008);
  897. end;
  898. arraydef:
  899. begin
  900. if not is_special_array(hdef) and
  901. not is_packed_array(hdef) then
  902. begin
  903. typename:=typename+'['+tostr(tarraydef(hdef).elecount)+'x';
  904. addtypename(typename,tarraydef(hdef).elementdef);
  905. typename:=typename+']';
  906. end
  907. else
  908. typename:=typename+'d'+hdef.unique_id_str;
  909. end
  910. else
  911. typename:=typename+'d'+hdef.unique_id_str;
  912. end;
  913. end;
  914. var
  915. i: longint;
  916. res: PHashSetItem;
  917. oldsymtablestack: tsymtablestack;
  918. hrecst: trecordsymtable;
  919. hrecdef: trecorddef;
  920. sym: tfieldvarsym;
  921. typename: TSymStr;
  922. begin
  923. typename:=internaltypeprefixName[itp_llvmstruct];
  924. for i:=low(fieldtypes) to high(fieldtypes) do
  925. begin
  926. addtypename(typename,fieldtypes[i]);
  927. end;
  928. if not assigned(current_module) then
  929. internalerror(2014012002);
  930. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  931. if not assigned(res^.Data) then
  932. begin
  933. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  934. recordalignmin);
  935. for i:=low(fieldtypes) to high(fieldtypes) do
  936. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),fieldtypes[i]);
  937. end;
  938. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  939. result:=trecorddef(res^.Data);
  940. end;
  941. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean; callercallee: tcallercallee): tdef;
  942. var
  943. retdeflist: array[0..9] of tdef;
  944. retloc: pcgparalocation;
  945. usedef: tdef;
  946. valueext: tllvmvalueextension;
  947. paraslots,
  948. i: longint;
  949. sizeleft: asizeint;
  950. begin
  951. { single location }
  952. if not assigned(cgpara.location^.next) then
  953. begin
  954. { def of the location, except in case of zero/sign-extension and
  955. zero-sized records }
  956. if not is_special_array(cgpara.def) and
  957. (cgpara.def.size=0) then
  958. usedef:=cgpara.def
  959. else
  960. usedef:=cgpara.location^.def;
  961. if beforevalueext then
  962. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  963. { comp and currency are handled by the x87 in this case. They cannot
  964. be represented directly in llvm, and llvmdef translates them into
  965. i64 (since that's their storage size and internally they also are
  966. int64). Solve this by changing the type to s80real in the
  967. returndef/parameter declaration. }
  968. if (usedef.typ=floatdef) and
  969. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  970. usedef:=s80floattype;
  971. result:=usedef;
  972. exit
  973. end;
  974. { multiple locations -> create temp record }
  975. retloc:=cgpara.location;
  976. i:=0;
  977. sizeleft:=cgpara.Def.size;
  978. repeat
  979. if i>high(retdeflist) then
  980. internalerror(2016121801);
  981. if assigned(retloc^.next) then
  982. begin
  983. retdeflist[i]:=retloc^.def;
  984. dec(sizeleft,retloc^.def.size);
  985. end
  986. { on the callerside, "byval" parameter locations have the implicit
  987. pointer in their type -> remove if we wish to create a record
  988. containing all actual parameter data }
  989. else if (callercallee=callerside) and
  990. not retloc^.llvmvalueloc then
  991. begin
  992. if retloc^.def.typ<>pointerdef then
  993. internalerror(2019020201);
  994. retdeflist[i]:=tpointerdef(retloc^.def).pointeddef
  995. end
  996. else if retloc^.def.size<>sizeleft then
  997. begin
  998. case sizeleft of
  999. 1:
  1000. retdeflist[i]:=u8inttype;
  1001. 2:
  1002. retdeflist[i]:=u16inttype;
  1003. 3:
  1004. retdeflist[i]:=u24inttype;
  1005. 4:
  1006. retdeflist[i]:=u32inttype;
  1007. 5:
  1008. retdeflist[i]:=u40inttype;
  1009. 6:
  1010. retdeflist[i]:=u48inttype;
  1011. 7:
  1012. retdeflist[i]:=u56inttype;
  1013. else
  1014. retdeflist[i]:=retloc^.def;
  1015. end
  1016. end
  1017. else
  1018. begin
  1019. if retloc^.def.typ<>floatdef then
  1020. begin
  1021. paraslots:=sizeleft div cgpara.Alignment;
  1022. if (paraslots>1) and
  1023. ((paraslots*cgpara.Alignment)=sizeleft) then
  1024. retdeflist[i]:=carraydef.getreusable(cgsize_orddef(int_cgsize(cgpara.Alignment)),paraslots)
  1025. else
  1026. retdeflist[i]:=retloc^.def;
  1027. end
  1028. else
  1029. retdeflist[i]:=retloc^.def;
  1030. end;
  1031. inc(i);
  1032. retloc:=retloc^.next;
  1033. until not assigned(retloc);
  1034. result:=llvmgettemprecorddef(slice(retdeflist,i),C_alignment,
  1035. targetinfos[target_info.system]^.alignment.recordalignmin);
  1036. include(result.defoptions,df_llvm_no_struct_packing);
  1037. end;
  1038. function llvmencodetypedecl(def: tdef): TSymStr;
  1039. begin
  1040. result:='';
  1041. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  1042. end;
  1043. end.