llvmdef.pas 46 KB

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