llvmdef.pas 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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 def=llvm_metadatatype then
  357. encodedstr:=encodedstr+'metadata'
  358. else if is_voidpointer(def) then
  359. encodedstr:=encodedstr+'i8*'
  360. else
  361. begin
  362. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,[],encodedstr);
  363. encodedstr:=encodedstr+'*';
  364. end;
  365. end;
  366. floatdef :
  367. begin
  368. case tfloatdef(def).floattype of
  369. s32real:
  370. encodedstr:=encodedstr+'float';
  371. s64real:
  372. encodedstr:=encodedstr+'double';
  373. { necessary to be able to force our own size/alignment }
  374. s80real:
  375. { prevent llvm from allocating the standard ABI size for
  376. extended }
  377. if lef_inaggregate in flags then
  378. encodedstr:=encodedstr+'[10 x i8]'
  379. else
  380. encodedstr:=encodedstr+'x86_fp80';
  381. sc80real:
  382. encodedstr:=encodedstr+'x86_fp80';
  383. s64comp,
  384. s64currency:
  385. encodedstr:=encodedstr+'i64';
  386. s128real:
  387. {$if defined(powerpc) or defined(powerpc128)}
  388. encodedstr:=encodedstr+'ppc_fp128';
  389. {$else}
  390. encodedstr:=encodedstr+'fp128';
  391. {$endif}
  392. end;
  393. end;
  394. filedef :
  395. begin
  396. case tfiledef(def).filetyp of
  397. ft_text :
  398. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  399. ft_typed :
  400. begin
  401. { in case of ISO-like I/O, the typed file def includes a
  402. get/put buffer of the size of the file's elements }
  403. if (m_isolike_io in current_settings.modeswitches) and
  404. not is_void(tfiledef(def).typedfiledef) then
  405. encodedstr:=encodedstr+'<{';
  406. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  407. if (m_isolike_io in current_settings.modeswitches) and
  408. not is_void(tfiledef(def).typedfiledef) then
  409. begin
  410. encodedstr:=encodedstr+',[';
  411. encodedstr:=encodedstr+tostr(tfiledef(def).typedfiledef.size);
  412. encodedstr:=encodedstr+' x i8]}>'
  413. end;
  414. end;
  415. ft_untyped :
  416. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  417. end;
  418. end;
  419. recorddef :
  420. begin
  421. { avoid endlessly recursive definitions }
  422. if not(lef_typedecl in flags) then
  423. encodedstr:=encodedstr+llvmtypeidentifier(def)
  424. else
  425. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  426. end;
  427. variantdef :
  428. begin
  429. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,[lef_inaggregate]+[lef_typedecl]*flags,encodedstr);
  430. end;
  431. classrefdef :
  432. begin
  433. if is_class(tclassrefdef(def).pointeddef) then
  434. begin
  435. llvmaddencodedtype_intern(tobjectdef(tclassrefdef(def).pointeddef).vmt_def,flags,encodedstr);
  436. encodedstr:=encodedstr+'*';
  437. end
  438. else if is_objcclass(tclassrefdef(def).pointeddef) then
  439. llvmaddencodedtype_intern(objc_idtype,flags,encodedstr)
  440. else
  441. encodedstr:=encodedstr+'i8*'
  442. end;
  443. setdef :
  444. begin
  445. { just an array as far as llvm is concerned; don't use a "packed
  446. array of i1" or so, this requires special support in backends
  447. and guarantees nothing about the internal format }
  448. if is_smallset(def) then
  449. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),[lef_inaggregate],encodedstr)
  450. else
  451. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  452. end;
  453. formaldef :
  454. begin
  455. { var/const/out x (always treated as "pass by reference" -> don't
  456. add extra "*" here) }
  457. encodedstr:=encodedstr+'i8';
  458. end;
  459. arraydef :
  460. begin
  461. if tarraydef(def).is_hwvector then
  462. begin
  463. encodedstr:=encodedstr+'<'+tostr(tarraydef(def).elecount)+' x ';
  464. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  465. encodedstr:=encodedstr+'>';
  466. end
  467. else if is_array_of_const(def) then
  468. begin
  469. encodedstr:=encodedstr+'[0 x ';
  470. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,[lef_inaggregate],encodedstr);
  471. encodedstr:=encodedstr+']';
  472. end
  473. else if is_open_array(def) then
  474. begin
  475. encodedstr:=encodedstr+'[0 x ';
  476. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  477. encodedstr:=encodedstr+']';
  478. end
  479. else if is_dynamic_array(def) then
  480. begin
  481. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  482. encodedstr:=encodedstr+'*';
  483. end
  484. else if is_packed_array(def) and
  485. (tarraydef(def).elementdef.typ in [enumdef,orddef]) then
  486. begin
  487. { encode as an array of bytes rather than as an array of
  488. packedbitsloadsize(elesize), because even if the load size
  489. is e.g. 2 bytes, the array may only be 1 or 3 bytes long
  490. (and if this array is inside a record, it must not be
  491. encoded as a type that is too long) }
  492. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size)+' x ';
  493. llvmaddencodedtype_intern(u8inttype,[lef_inaggregate],encodedstr);
  494. encodedstr:=encodedstr+']';
  495. end
  496. else
  497. begin
  498. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  499. llvmaddencodedtype_intern(tarraydef(def).elementdef,[lef_inaggregate],encodedstr);
  500. encodedstr:=encodedstr+']';
  501. end;
  502. end;
  503. procdef,
  504. procvardef :
  505. begin
  506. if (def.typ=procdef) or
  507. tprocvardef(def).is_addressonly then
  508. begin
  509. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  510. if def.typ=procvardef then
  511. encodedstr:=encodedstr+'*';
  512. end
  513. else if not(lef_typedecl in flags) then
  514. begin
  515. { in case the procvardef recursively references itself, e.g.
  516. via a pointer }
  517. encodedstr:=encodedstr+llvmtypeidentifier(def);
  518. { blocks are implicit pointers }
  519. if is_block(def) then
  520. encodedstr:=encodedstr+'*'
  521. end
  522. else if is_block(def) then
  523. begin
  524. llvmaddencodedtype_intern(get_block_literal_type_for_proc(tabstractprocdef(def)),flags,encodedstr);
  525. end
  526. else
  527. begin
  528. encodedstr:=encodedstr+'<{';
  529. { code pointer }
  530. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  531. { data pointer (maybe todo: generate actual layout if
  532. available) }
  533. encodedstr:=encodedstr+'*, i8*}>';
  534. end;
  535. end;
  536. objectdef :
  537. case tobjectdef(def).objecttype of
  538. odt_class,
  539. odt_objcclass,
  540. odt_object,
  541. odt_cppclass:
  542. begin
  543. if not(lef_typedecl in flags) then
  544. encodedstr:=encodedstr+llvmtypeidentifier(def)
  545. else
  546. llvmaddencodedabstractrecordtype(tabstractrecorddef(def),encodedstr);
  547. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) and
  548. is_implicit_pointer_object_type(def) then
  549. encodedstr:=encodedstr+'*'
  550. end;
  551. odt_interfacecom,
  552. odt_interfacecorba,
  553. odt_dispinterface:
  554. begin
  555. { type is a pointer to a pointer to the vmt }
  556. llvmaddencodedtype_intern(tobjectdef(def).vmt_def,flags,encodedstr);
  557. if ([lef_typedecl,lef_noimplicitderef]*flags=[]) then
  558. encodedstr:=encodedstr+'**';
  559. end;
  560. odt_interfacecom_function,
  561. odt_interfacecom_property,
  562. odt_objcprotocol:
  563. begin
  564. { opaque for now }
  565. encodedstr:=encodedstr+'i8*'
  566. end;
  567. odt_helper:
  568. llvmaddencodedtype_intern(tobjectdef(def).extendeddef,flags,encodedstr);
  569. else
  570. internalerror(2013100601);
  571. end;
  572. undefineddef,
  573. errordef :
  574. internalerror(2013100604);
  575. else
  576. internalerror(2013100603);
  577. end;
  578. end;
  579. function llvmencodetypename(def: tdef): TSymStr;
  580. begin
  581. result:='';
  582. llvmaddencodedtype_intern(def,[],result);
  583. end;
  584. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  585. var
  586. flags: tllvmencodeflags;
  587. begin
  588. if inaggregate then
  589. flags:=[lef_inaggregate]
  590. else
  591. flags:=[];
  592. llvmaddencodedtype_intern(def,flags,encodedstr);
  593. end;
  594. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  595. var
  596. st: tllvmshadowsymtable;
  597. symdeflist: tfpobjectlist;
  598. i: longint;
  599. nopacked: boolean;
  600. begin
  601. st:=tabstractrecordsymtable(def.symtable).llvmst;
  602. symdeflist:=st.symdeflist;
  603. nopacked:=df_llvm_no_struct_packing in def.defoptions;
  604. if nopacked then
  605. encodedstr:=encodedstr+'{ '
  606. else
  607. encodedstr:=encodedstr+'<{ ';
  608. if symdeflist.count>0 then
  609. begin
  610. i:=0;
  611. if (def.typ=objectdef) and
  612. assigned(tobjectdef(def).childof) and
  613. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  614. begin
  615. { insert the struct for the class rather than a pointer to the struct }
  616. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  617. internalerror(2008070601);
  618. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,[lef_inaggregate,lef_noimplicitderef],encodedstr);
  619. inc(i);
  620. end;
  621. while i<symdeflist.count do
  622. begin
  623. if i<>0 then
  624. encodedstr:=encodedstr+', ';
  625. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,[lef_inaggregate],encodedstr);
  626. inc(i);
  627. end;
  628. end;
  629. if nopacked then
  630. encodedstr:=encodedstr+' }'
  631. else
  632. encodedstr:=encodedstr+' }>';
  633. end;
  634. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  635. begin
  636. { implicit zero/sign extension for ABI compliance? (yes, if the size
  637. of a paraloc is larger than the size of the entire parameter) }
  638. if is_ordinal(paradef) and
  639. is_ordinal(paralocdef) and
  640. (paradef.size<paralocdef.size) then
  641. begin
  642. paralocdef:=paradef;
  643. if is_signed(paradef) then
  644. signext:=lve_signext
  645. else
  646. signext:=lve_zeroext
  647. end
  648. else
  649. signext:=lve_none;
  650. end;
  651. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname, withattributes: boolean; var first: boolean; var encodedstr: TSymStr);
  652. var
  653. para: PCGPara;
  654. paraloc: PCGParaLocation;
  655. side: tcallercallee;
  656. signext: tllvmvalueextension;
  657. usedef: tdef;
  658. firstloc: boolean;
  659. begin
  660. if (proccalloption in cdecl_pocalls) and
  661. is_array_of_const(hp.vardef) then
  662. begin
  663. if not first then
  664. encodedstr:=encodedstr+', '
  665. else
  666. first:=false;
  667. encodedstr:=encodedstr+'...';
  668. exit
  669. end;
  670. if not withparaname then
  671. side:=callerside
  672. else
  673. side:=calleeside;
  674. { don't add parameters that don't take up registers or stack space;
  675. clang doesn't either and some LLVM backends don't support them }
  676. if hp.paraloc[side].isempty then
  677. exit;
  678. para:[email protected][side];
  679. paraloc:=para^.location;
  680. firstloc:=true;
  681. repeat
  682. usedef:=paraloc^.def;
  683. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  684. { implicit zero/sign extension for ABI compliance? }
  685. if not first then
  686. encodedstr:=encodedstr+', ';
  687. llvmaddencodedtype_intern(usedef,[],encodedstr);
  688. { in case signextstr<>'', there should be only one paraloc -> no need
  689. to clear (reason: it means that the paraloc is larger than the
  690. original parameter) }
  691. if withattributes then
  692. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  693. { sret: hidden pointer for structured function result }
  694. if vo_is_funcret in hp.varoptions then
  695. begin
  696. { "sret" is only valid for the firstparameter, while in FPC this
  697. can sometimes be second one (self comes before). In general,
  698. this is not a problem: we can just leave out sret, which means
  699. the result will be a bit less well optimised), but it is for
  700. AArch64: there, the sret parameter must be passed in a different
  701. register (-> paranr_result is smaller than paranr_self for that
  702. platform in symconst) }
  703. {$ifdef aarch64}
  704. if not first and
  705. not is_managed_type(hp.vardef) then
  706. internalerror(2015101404);
  707. {$endif aarch64}
  708. if withattributes then
  709. begin
  710. if first
  711. {$ifdef aarch64}
  712. and not is_managed_type(hp.vardef)
  713. {$endif aarch64}
  714. then
  715. encodedstr:=encodedstr+llvmparatypeattr(' sret',hp.vardef,false)+' noalias nocapture'
  716. else
  717. encodedstr:=encodedstr+' noalias nocapture';
  718. end;
  719. end
  720. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  721. llvmbyvalparaloc(paraloc) then
  722. begin
  723. encodedstr:=encodedstr+'*';
  724. if withattributes then
  725. begin
  726. encodedstr:=encodedstr+llvmparatypeattr(' byval',hp.vardef,false);
  727. if firstloc and
  728. (para^.alignment<>std_param_align) then
  729. begin
  730. encodedstr:=encodedstr+' align '+tostr(para^.alignment);
  731. end;
  732. end
  733. end
  734. else if withattributes and
  735. paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) then
  736. begin
  737. { it's not valid to take the address of a parameter and store it for
  738. use past the end of the function call (since the address can always
  739. be on the stack and become invalid later) }
  740. encodedstr:=encodedstr+' nocapture';
  741. { open array/array of const/variant array may be a valid pointer but empty }
  742. if not is_special_array(hp.vardef) and
  743. { e.g. empty records }
  744. (hp.vardef.size<>0) then
  745. begin
  746. case hp.varspez of
  747. vs_value,
  748. vs_const:
  749. begin
  750. encodedstr:=encodedstr+' readonly dereferenceable('
  751. end;
  752. vs_var,
  753. vs_out:
  754. begin
  755. { while normally these are not nil, it is technically possible
  756. to pass nil via ptrtype(nil)^ }
  757. encodedstr:=encodedstr+' dereferenceable_or_null(';
  758. end;
  759. vs_constref:
  760. begin
  761. encodedstr:=encodedstr+' readonly dereferenceable_or_null(';
  762. end;
  763. else
  764. internalerror(2018120801);
  765. end;
  766. if hp.vardef.typ<>formaldef then
  767. encodedstr:=encodedstr+tostr(hp.vardef.size)+')'
  768. else
  769. encodedstr:=encodedstr+'1)';
  770. end;
  771. end;
  772. if withparaname then
  773. begin
  774. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  775. internalerror(2014010803);
  776. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  777. end;
  778. paraloc:=paraloc^.next;
  779. firstloc:=false;
  780. first:=false;
  781. until not assigned(paraloc);
  782. end;
  783. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  784. begin
  785. result:='';
  786. llvmaddencodedproctype(def,customname,pddecltype,result);
  787. end;
  788. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  789. var
  790. callingconv: ansistring;
  791. usedef: tdef;
  792. paranr: longint;
  793. hp: tparavarsym;
  794. signext: tllvmvalueextension;
  795. useside: tcallercallee;
  796. first: boolean;
  797. begin
  798. if not(pddecltype in [lpd_alias,lpd_procvar]) then
  799. begin
  800. callingconv:=llvm_callingconvention_name(def.proccalloption);
  801. if callingconv<>'' then
  802. encodedstr:=encodedstr+' '+callingconv;
  803. end;
  804. { when writing a definition, we have to write the parameter names, and
  805. those are only available on the callee side. In all other cases,
  806. we are at the callerside }
  807. if pddecltype=lpd_def then
  808. useside:=calleeside
  809. else
  810. useside:=callerside;
  811. def.init_paraloc_info(useside);
  812. first:=true;
  813. { function result (return-by-ref is handled explicitly) }
  814. if not paramanager.ret_in_param(def.returndef,def) or
  815. def.generate_safecall_wrapper then
  816. begin
  817. if not def.generate_safecall_wrapper then
  818. usedef:=llvmgetcgparadef(def.funcretloc[useside],false,useside)
  819. else
  820. usedef:=ossinttype;
  821. llvmextractvalueextinfo(def.returndef,usedef,signext);
  822. { specifying result sign extention information for an alias causes
  823. an error for some reason }
  824. if pddecltype in [lpd_decl,lpd_def] then
  825. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  826. encodedstr:=encodedstr+' ';
  827. llvmaddencodedtype_intern(usedef,[],encodedstr);
  828. end
  829. else
  830. begin
  831. encodedstr:=encodedstr+' ';
  832. llvmaddencodedtype(voidtype,false,encodedstr);
  833. end;
  834. encodedstr:=encodedstr+' ';
  835. { add procname? }
  836. if (pddecltype in [lpd_decl,lpd_def]) and
  837. (def.typ=procdef) then
  838. if customname='' then
  839. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  840. else
  841. encodedstr:=encodedstr+llvmmangledname(customname);
  842. encodedstr:=encodedstr+'(';
  843. { parameters }
  844. first:=true;
  845. for paranr:=0 to def.paras.count-1 do
  846. begin
  847. hp:=tparavarsym(def.paras[paranr]);
  848. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_def],not(pddecltype in [lpd_procvar,lpd_alias]),first,encodedstr);
  849. end;
  850. if po_varargs in def.procoptions then
  851. begin
  852. if not first then
  853. encodedstr:=encodedstr+', ';
  854. encodedstr:=encodedstr+'...';
  855. end;
  856. encodedstr:=encodedstr+')'
  857. end;
  858. function llvmgettemprecorddef(const fieldtypes: array of tdef; packrecords, recordalignmin: shortint): trecorddef;
  859. procedure addtypename(var typename: TSymStr; hdef: tdef);
  860. begin
  861. case hdef.typ of
  862. orddef:
  863. case torddef(hdef).ordtype of
  864. s8bit,
  865. u8bit,
  866. pasbool1,
  867. pasbool8:
  868. typename:=typename+'i8';
  869. s16bit,
  870. u16bit:
  871. typename:=typename+'i16';
  872. s32bit,
  873. u32bit:
  874. typename:=typename+'i32';
  875. s64bit,
  876. u64bit:
  877. typename:=typename+'i64';
  878. customint:
  879. typename:=typename+'i'+tostr(torddef(hdef).packedbitsize);
  880. else
  881. { other types should not appear currently, add as needed }
  882. internalerror(2014012001);
  883. end;
  884. floatdef:
  885. case tfloatdef(hdef).floattype of
  886. s32real:
  887. typename:=typename+'f32';
  888. s64real:
  889. typename:=typename+'f64';
  890. else
  891. { other types should not appear currently, add as needed }
  892. internalerror(2014012008);
  893. end;
  894. arraydef:
  895. begin
  896. if not is_special_array(hdef) and
  897. not is_packed_array(hdef) then
  898. begin
  899. typename:=typename+'['+tostr(tarraydef(hdef).elecount)+'x';
  900. addtypename(typename,tarraydef(hdef).elementdef);
  901. typename:=typename+']';
  902. end
  903. else
  904. typename:=typename+'d'+hdef.unique_id_str;
  905. end
  906. else
  907. typename:=typename+'d'+hdef.unique_id_str;
  908. end;
  909. end;
  910. var
  911. i: longint;
  912. res: PHashSetItem;
  913. oldsymtablestack: tsymtablestack;
  914. hrecst: trecordsymtable;
  915. hrecdef: trecorddef;
  916. sym: tfieldvarsym;
  917. typename: TSymStr;
  918. begin
  919. typename:=internaltypeprefixName[itp_llvmstruct];
  920. for i:=low(fieldtypes) to high(fieldtypes) do
  921. begin
  922. addtypename(typename,fieldtypes[i]);
  923. end;
  924. if not assigned(current_module) then
  925. internalerror(2014012002);
  926. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  927. if not assigned(res^.Data) then
  928. begin
  929. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  930. recordalignmin);
  931. for i:=low(fieldtypes) to high(fieldtypes) do
  932. trecorddef(res^.Data).add_field_by_def('F'+tostr(i),fieldtypes[i]);
  933. end;
  934. trecordsymtable(trecorddef(res^.Data).symtable).addalignmentpadding;
  935. result:=trecorddef(res^.Data);
  936. end;
  937. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean; callercallee: tcallercallee): tdef;
  938. var
  939. retdeflist: array[0..9] of tdef;
  940. retloc: pcgparalocation;
  941. usedef: tdef;
  942. valueext: tllvmvalueextension;
  943. paraslots,
  944. i: longint;
  945. sizeleft: asizeint;
  946. begin
  947. { single location }
  948. if not assigned(cgpara.location^.next) then
  949. begin
  950. { def of the location, except in case of zero/sign-extension and
  951. zero-sized records }
  952. if not is_special_array(cgpara.def) and
  953. (cgpara.def.size=0) then
  954. usedef:=cgpara.def
  955. else
  956. usedef:=cgpara.location^.def;
  957. if beforevalueext then
  958. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  959. { comp and currency are handled by the x87 in this case. They cannot
  960. be represented directly in llvm, and llvmdef translates them into
  961. i64 (since that's their storage size and internally they also are
  962. int64). Solve this by changing the type to s80real in the
  963. returndef/parameter declaration. }
  964. if (usedef.typ=floatdef) and
  965. (tfloatdef(usedef).floattype in [s64comp,s64currency]) then
  966. usedef:=s80floattype;
  967. result:=usedef;
  968. exit
  969. end;
  970. { multiple locations -> create temp record }
  971. retloc:=cgpara.location;
  972. i:=0;
  973. sizeleft:=cgpara.Def.size;
  974. repeat
  975. if i>high(retdeflist) then
  976. internalerror(2016121801);
  977. if assigned(retloc^.next) then
  978. begin
  979. retdeflist[i]:=retloc^.def;
  980. dec(sizeleft,retloc^.def.size);
  981. end
  982. { on the callerside, "byval" parameter locations have the implicit
  983. pointer in their type -> remove if we wish to create a record
  984. containing all actual parameter data }
  985. else if (callercallee=callerside) and
  986. not retloc^.llvmvalueloc then
  987. begin
  988. if retloc^.def.typ<>pointerdef then
  989. internalerror(2019020201);
  990. retdeflist[i]:=tpointerdef(retloc^.def).pointeddef
  991. end
  992. else if retloc^.def.size<>sizeleft then
  993. begin
  994. case sizeleft of
  995. 1:
  996. retdeflist[i]:=u8inttype;
  997. 2:
  998. retdeflist[i]:=u16inttype;
  999. 3:
  1000. retdeflist[i]:=u24inttype;
  1001. 4:
  1002. retdeflist[i]:=u32inttype;
  1003. 5:
  1004. retdeflist[i]:=u40inttype;
  1005. 6:
  1006. retdeflist[i]:=u48inttype;
  1007. 7:
  1008. retdeflist[i]:=u56inttype;
  1009. else
  1010. retdeflist[i]:=retloc^.def;
  1011. end
  1012. end
  1013. else
  1014. begin
  1015. if retloc^.def.typ<>floatdef then
  1016. begin
  1017. paraslots:=sizeleft div cgpara.Alignment;
  1018. if (paraslots>1) and
  1019. ((paraslots*cgpara.Alignment)=sizeleft) then
  1020. retdeflist[i]:=carraydef.getreusable(cgsize_orddef(int_cgsize(cgpara.Alignment)),paraslots)
  1021. else
  1022. retdeflist[i]:=retloc^.def;
  1023. end
  1024. else
  1025. retdeflist[i]:=retloc^.def;
  1026. end;
  1027. inc(i);
  1028. retloc:=retloc^.next;
  1029. until not assigned(retloc);
  1030. result:=llvmgettemprecorddef(slice(retdeflist,i),C_alignment,
  1031. targetinfos[target_info.system]^.alignment.recordalignmin);
  1032. include(result.defoptions,df_llvm_no_struct_packing);
  1033. end;
  1034. function llvmencodetypedecl(def: tdef): TSymStr;
  1035. begin
  1036. result:='';
  1037. llvmaddencodedtype_intern(def,[lef_typedecl],result);
  1038. end;
  1039. end.